title: Julia Variance Q
body: <p>Ok have been wresting with this for a little while now. </p>
  
  <p>I am calculating a rolling variance. I am using a manual variance calculation and then cross checking with the <code>var()</code></p>
  
  <p>To follow along will use my variance calculation adjusting for n-1</p>
  
  <p>Calculate the mean of the sample. ...
  Subtract the mean from each data point. ...
  Square each result. ...
  Find the sum of the squared values. ...
  Divide by n - 1, where n is the number of data points.</p>
  
  <pre><code>    xi = [1.0,2.0,3.0,4.0,5.0,6.0]
      # initalize arrays
      xi_sum = zeros(xi)
      xi_mean = zeros(xi)
      xi_mean_dev = zeros(xi)
      xi_mean_dev_sq = zeros(xi)
      xi_sum_mean_dev_sq = zeros(xi)
      xi_var_corrected = zeros(xi)
      # loop
      i=1
      for i = 1:size(xi,1)
      xi_sum[i] = sum(xi[1:i])
      xi_mean[i] = xi_sum[i] / i
      xi_mean_dev[i] = xi[i] - xi_mean[i]
      xi_mean_dev_sq[i] = xi_mean_dev[i]^2
      xi_sum_mean_dev_sq[i] = sum(xi_mean_dev_sq[1:i])
      xi_var_corrected[i] = xi_sum_mean_dev_sq[i] / (i-1) # n-1
  end
  </code></pre>
  
  <p>With output: </p>
  
  <pre><code>julia&gt; xi_var_corrected
  6-element Array{Float64,1}:
   NaN
     0.25
     0.625
     1.16667
     1.875
     2.75
  </code></pre>
  
  <p>If verify with var() </p>
  
  <pre><code>var_verify = zeros(xi)
      for i = 1:size(xi,1)
          var_verify[i] = var(xi[1:i],corrected=true)
      end
  
  julia&gt; var_verify
  6-element Array{Float64,1}:
   NaN
     0.5
     1.0
     1.66667
     2.5
     3.5
  </code></pre>
  
  <p>Also with: </p>
  
  <pre><code>julia&gt; var(xi)
  3.5
  </code></pre>
  
  <p>If I validate in R: </p>
  
  <pre><code>&gt; test = c(1,2,3,4,5,6)
  &gt; var(test)
  [1] 3.5
  </code></pre>
  
  <p>Final is 3.5 vs my 2.75.</p>
  
  <p>My question is - im at a loss as to why my manual variance calculation is not providing the same answer as var(), R var(). </p>
  
  <p>Reason i'm breaking down variance is that in calculating the regression slope, there is a lot of cross over calculations in the stdev, correlation - so if i can break it down, i can cut down processing time + if i calculate on every [i] instead over the ranges as above. I can make it faster. Anyway anyone see where im going wrong with my variance calculation? </p>
  
  <p>Thanks</p>
  
  <h1>Edit</h1>
  
  <p>I see it now - for each 1:i </p>
  
  <p>I need the final mean at each i, then subtract that from the previous values to center it correctly.</p>