Deviations

Jun. 28th, 2005 10:41 am
plonq: (Creative mood)
[personal profile] plonq
For no apparent reason I decided that I wanted to calculate the standard deviation for a column of numbers I'd gathered for somebody.  It's been years since I took any statistics courses and I couldn't remember the formula.

I applied what I could remember of the subject, and I wrote the following snippet of code:

do eStem = 1 to Elements
    Variance = Variance + ( ( Element.eStem - Mean ) ^ 2 ) / Elements )
end

Standard_Deviation = Variance / 3
do until trunc( Standard_Deviation^2, 15 ) = Variance
    Standard_Deviation = .5 * ( Standard_Deviation + ( Variance / Standard_Deviation ) )
end


(Yes, sadly this stupid language doesn't have a square root function, so I had to derive it using Newton's method.)

Only after I'd done this did it occur to me that Excel has a StdDev function that might have worked.  I tried that... and it gave me a different answer.  Huh? 

My immediate assumption was that Excel must be wrong. 

Moments later reason set in, and I realized that I had most likely left something out of my equation.  After a quick trip to Mister Internet, I revised my program slightly to read:

do eStem = 1 to Elements
    Variance = Variance + ( ( Element.eStem - Mean ) ^ 2 ) / ( Elements - 1 ) )
end


Suddenly the world is a joyous place again.  I'm not sure why I'm happy that I managed to write a program to replicate what Excel does for me automatically, but it's kind of like whittling a working automobile out of a block of pine.  It may not be practical, but at least it's yours.

[Edit: Just for giggles I expanded it to calculate the Nth root, and turned it into a self-contained little module that I can import into any future programs that I write in this language.]

Date: 2005-06-28 08:28 pm (UTC)
From: [identity profile] fuzzytoedcollie.livejournal.com
Wonders what the standard deviation of DeviantArt is?

Deviations

Date: 2005-06-29 02:12 pm (UTC)
From: (Anonymous)
Square root is also the 1/2 power/.

Atara's papa

April 2024

S M T W T F S
 123456
78910111213
14 151617181920
21222324252627
282930    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 7th, 2025 03:52 am
Powered by Dreamwidth Studios