Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    May 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Looking for standard deviation function

    Hi all,

    I'm trying to find a js function that will calculate the standard deviation from a set of numbers. It doesn't need to be wildly accurate as it will be rounded to an integer anyway.

    Does anyone know of one that I could use?

    I did do a search on the forums but have come up with nothing. Surprisingly google hasn't turned up anything either.

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by jhob View Post
    Hi all,

    I'm trying to find a js function that will calculate the standard deviation from a set of numbers. It doesn't need to be wildly accurate as it will be rounded to an integer anyway.

    Does anyone know of one that I could use?

    I did do a search on the forums but have come up with nothing. Surprisingly google hasn't turned up anything either.
    I think I've got the algorithm right. The first parameter is the mean and you can add as many points as you want. The returned value is unrounded.
    Code:
    <script type='text/javascript'>
    
    function sigma()
    {
     var mean=arguments[0], squareSum=0;
         
     for(var i=1, len=arguments.length; i<len; i++)
      squareSum+=Math.pow(arguments[i]-mean, 2); 
     
     return Math.sqrt(squareSum/(i-1));
    }
    
    alert( sigma(6, 4,8) )
    
    </script>

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    A simple script:-

    <script type = "text/javascript">
    var data = new Array("73", "58", "67", "93", "33", "18", "147");
    var deviation = new Array();
    var sum = 0;
    var devnsum = 0;
    var stddevn = 0;
    var len = data.length;
    for (var i=0; i<len; i++) {
    sum = sum + (parseFloat(data[i],10)); // ensure number to base 10
    }
    var mean = (sum/len).toFixed(6); // 6 decimal places
    for (i=0; i<len; i++) {
    deviation[i] = data[i] - mean;
    deviation[i] = deviation[i] * deviation[i];
    devnsum = devnsum + deviation[i];
    }
    stddevn = Math.sqrt(devnsum/(len-1)).toFixed(6); // 6 decimal places

    alert ("The mean is: " + mean + " The standard deviation is: " + stddevn)
    </script>
    Last edited by Philip M; 07-12-2007 at 07:54 AM. Reason: Improved version

  • #4
    New to the CF scene
    Join Date
    May 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks ever so much for your help with this folks, I really appreciate it as I have a work deadline looming of which this calculation is an integral part. It would have taken me a while to get my head around standard deviation and then write a function myself. I also appear to be developing RSI which makes typing rather uncomfortable so you've saved me a fair few keystrokes there too.

    Thanks again,
    John

  • #5
    New to the CF scene
    Join Date
    May 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    A simple script:-

    <script type = "text/javascript">
    var data = new Array("73", "58", "67", "93", "33", "18", "147");
    var deviation = new Array();
    var sum = 0;
    var devnsum = 0;
    var stddevn = 0;
    var len = data.length;
    for (var i=0; i<len; i++) {
    sum = sum + (parseFloat(data[i],10)); // ensure number to base 10
    }
    var mean = (sum/len).toFixed(6); // 6 decimal places
    for (i=0; i<len; i++) {
    deviation[i] = data[i] - mean;
    deviation[i] = deviation[i] * deviation[i];
    devnsum = devnsum + deviation[i];
    }
    stddevn = Math.sqrt(devnsum/(len-1)).toFixed(6); // 6 decimal places

    alert ("The mean is: " + mean + " The standard deviation is: " + stddevn)
    </script>
    Script wasn't quite right due to the length and for loops counting from 0. Made a couple of small changes as follows:

    <script type = "text/javascript">
    var data = new Array("73", "58", "67", "93", "33", "18", "147");
    var deviation = new Array();
    var sum = 0;
    var devnsum = 0;
    var stddevn = 0;
    var len = data.length - 1;
    alert(len);
    for (var i=1; i<len; i++)
    {
    sum = sum + (parseFloat(data[i],10)); // ensure number to base 10
    }
    var mean = (sum/len).toFixed(2); // 2 decimal places
    for (i=1; i<len; i++)
    {
    deviation[i] = data[i] - mean;
    deviation[i] = deviation[i] * deviation[i];
    devnsum = devnsum + deviation[i];
    }
    stddevn = Math.sqrt(devnsum/(len-1)).toFixed(2); // 2 decimal places
    alert ("The mean is: " + mean + " The standard deviation is: " + stddevn)
    }
    </script>

    Works fine now, thanks all for your help.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Sorry, you have messed it up.

    The array data() contains 7 elements, indexed 0 - 6.
    So var len = data.length - 1; (i.e. 6) and for (var i=1....) means that you only include five values 1-5 in the calculation.

    Suggest you put it back as it was and study the script more carefully.
    The correct answers are mean = 69.85 and standard deviation = 42.21 on the sample values given.

    Why have you added a } here? It stops the script working.
    alert ("The mean is: " + mean + " The standard deviation is: " + stddevn)
    }

    Obviously
    toFixed(2); // 2 decimal places
    is fine.
    Last edited by Philip M; 07-12-2007 at 04:42 PM.

  • #7
    New to the CF scene
    Join Date
    May 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Sorry, you have messed it up.

    The array data() contains 7 elements, indexed 0 - 6.
    So var len = data.length - 1; (i.e. 6) and for (i=1....) means that you only include five values 1-5 in the calculation.

    Suggest you put it back as it was and study the script more carefully.

    Obviously
    toFixed(2); // 2 decimal places
    is fine.
    Yeah sorry, my mistake.

    I do appreciate the help but you could have replied in a slightly less patronising fashion. Not everyone is born a **** hot coder.

  • #8
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by Philip M View Post
    Sorry, you have messed it up.

    The array data() contains 7 elements, indexed 0 - 6.
    So var len = data.length - 1; (i.e. 6) and for (var i=1....) means that you only include five values 1-5 in the calculation.

    Suggest you put it back as it was and study the script more carefully.
    The correct answers are mean = 69.85 and standard deviation = 42.21 on the sample values given.
    I don't agree, the sd is 39. Your calculation divides by the wrong value:
    Code:
    Math.sqrt(devnsum/(len-1))
    I assumed that the mean was known already, but that's easily fixed:
    Code:
    <script type='text/javascript'>
    
    function sigma()
    {
     for(var i=0, mean=0, len=arguments.length; i<len; i++)
      mean+=arguments[i];
     
     mean/=i;    
     
     for(var i=0, squareSum=0, len=arguments.length; i<len; i++)
      squareSum+=Math.pow(arguments[i]-mean, 2); 
     
     return {sigma:Math.sqrt(squareSum/i), mean:mean}
    }
    
    var data=sigma(73, 58, 67, 93, 33, 18, 147);
    
    alert( 'sd:' + data.sigma +' Mean:'+ data.mean )
    
    </script>

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by Arty Effem View Post
    I don't agree, the sd is 39. Your calculation divides by the wrong value:
    Math.sqrt(devnsum/(len-1))
    I am afraid that I do not agree with you. len = 7 and len - 1 = 6, which is one less than the number of data items.

    When I was at school the standard deviation was calculated as follows:

    A. Compute the mean for the data set.
    B. Compute the deviation by subtracting the mean from each value.
    C. Square each individual deviation.
    D. Add up the squared deviations.
    E. Divide by one less than the sample size.
    F. Take the square root.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •