Standard deviation javascript

后端 未结 7 1136
感情败类
感情败类 2021-01-11 10:24

I am trying to get the standard deviation of a user input string. I have as follows, but it returns the wrong value for SD. The calculation should go as follows: Sum values/

7条回答
  •  迷失自我
    2021-01-11 10:57

    Quick implementation of the standard deviation function:

    const sd = numbers => {
      const mean = numbers.reduce((acc, n) => acc + n) / numbers.length;
      return Math.sqrt(
        numbers.reduce((acc, n) => (n - mean) ** 2) / numbers.length
      );
    };
    
    

    Corrected SD version:

    const correctedSd = numbers => {
      const mean = numbers.reduce((acc, n) => acc + n) / numbers.length;
      return Math.sqrt(
        numbers.reduce((acc, n) => (n - mean) ** 2) / (numbers.length - 1)
      );
    };
    
    

提交回复
热议问题