Question about variance and stddev

General Discussions about ND1

Question about variance and stddev

Postby pietvo » Wed Jul 25, 2012 2:46 pm

I have a question about the definition of variance and standard deviation. Are the population var and std (meaning that it is divided by n) or sample var and std (meaning divided by n-1). For serious statistics you need both and most statistical calculators have both, so you can choose the proper one. They are often indicated with a n or n-1 subscript. Of course I could find out with some examples, but I think it should be properly documented otherwise you may get the wrong answer.

By the way, is there a way to find out the code that defines a certain function?
Posts: 3
Joined: Tue Jul 24, 2012 8:46 pm

Re: Question about variance and stddev

Postby oliver » Wed Jul 25, 2012 3:12 pm

Hi pietvo,

Thanks for your comment. It looks like n-1 subscript would apply. I will double-check and clarify in the documentation.

If you have JavaScript skills, you can make code visible quite easily, but there's no user-sanctioned way to do that.

The code for these two functions looks like this:
Code: Select all
      "stat_standard_deviation": function() {
         var variances = this["stat_variance"]();
         var stddevs = [];
         for (var i=0; i<variances.length; i++)
         return stddevs;
      "stat_variance": function() {
         var m = this.stat_data();
         var v = [];
         for (var col=0; col<m[0].length; col++) {
            var squares = 0, sum = 0;
            for (var row=0; row<m.length; row++) {
               var x = m[row][col];
               squares += x * x;
               sum += x;
            var mean = sum / m.length;
            var variance = (squares - sum*mean) / (m.length-1);
         return v;

(Note that this refers to calculator.functions in these functions. A user function would have to say calculator.functions.stat_data() (or, using the ME alias, more compactly: ME.stat_data()), instead of this.stat_data().

If you'd be interested in that, it would be straight-forward to produce the n subscript version from this.
You can make it either a user function that lives in a folder, or you can inject code and make it available globally. Here's documentation about this I'm glad to help with questions, too.
Site Admin
Posts: 433
Joined: Sat May 01, 2010 2:11 pm

Return to General

Who is online

Users browsing this forum: No registered users and 1 guest