Essential Statistics with JavaScript
Simple, Useful and Slow: Assorted Reference Statistics Functions (utf-8)

        
          
          // Simple, Useful and Slow
        
        
          
          // Assorted Reference Statistics Functions
        
        
          
          // UTF-8 Edition
        
        
          
          

        
        
          
          // Shorthands
        
        
          
          

        
        
          
          pow = Math.pow
        
        
          
          sqrt = Math.sqrt
        
        
          
          pi = Math.PI
        
        
          
          

        
        
          
          // Variables named:
        
        
          
          // x, x1, x2, ..., xn: always a sample (array of numbers)
        
        
          
          // v is always a number
        
        
          
          

        
        
          
          // Length of a sample
        
        
          
          n = x => x.length
        
        
          
          

        
        
          
          // Mean
        
        
          
          μ = x => x.reduce((acc, v) => acc+v, 0)/n(x)
        
        
          
          

        
        
          
          // Variance
        
        
          
          σ2 = x => x.reduce((acc, v) => acc+pow(v-μ(x), 2), 0)/(n(x)-1)
        
        
          
          

        
        
          
          // Standard Deviation
        
        
          
          σ = x => sqrt(σ2(x))
        
        
          
          

        
        
          
          //
        
        
          
          // Student's t-test
        
        
          
          //
        
        
          
          

        
        
          
          // Degrees of freedom
        
        
          
          df = (x1, x2) => n(x1)+n(x2) - 2
        
        
          
          

        
        
          
          // t-test
        
        
          
          t = (x1, x2) => (μ(x1)-μ(x2))/sqrt(σ2(a)/n(a)+σ2(b)/n(b))
        
        
          
          

        
        
          
          // z-test (same as t, just uses different distribution when applying the score)
        
        
          
          z = t
        
        
          
          

        
        
          
          //
        
        
          
          // Normal Distribution
        
        
          
          //
        
        
          
          

        
        
          
          // Euler
        
        
          
          e = 2.7182818284
        
        
          
          

        
        
          
          // Normal Distribution
        
        
          
          _N = (v, μ, σ2) => 1/sqrt(2*pi*σ2)*pow(e, -pow(v-μ, 2)/2*σ2)
        
        
          
          

        
        
          
          // Standard Normal Distribution
        
        
          
          N = v => 1/sqrt(2*pi)*pow(e, -0.5*v*v)
        
        
          
          

        
        
          
          //
        
        
          
          // Distribution Sampling
        
        
          
          //
        
        
          
          

        
        
          
          // Sample a distribution
        
        
          
          sample = (distribution, from, to, delta) => {
        
        
          
            let array = [];
        
        
          
          

        
        
          
            for (;from<to;from+=delta) {
        
        
          
              array.push(distribution(from))
        
        
          
            }
        
        
          
          

        
        
          
            return array;
        
        
          
          }
        
        
          
          

        
        
          
          ε = 0.0001
        
        
          
          Ꝏ  = 5
        
        
          
          

        
        
          
          // Integral
        
        
          
          ʃ = (f, a, b) => sample(f, a, b, ε).reduce((acc, v) => acc+v, 0)*ε
        
        
          
          

        
        
          
          // p-value
        
        
          
          

        
        
          
          // One tailed
        
        
          
          p_value_1 = (score, distribution) => ʃ(distribution || N, score, +Ꝏ )
        
        
          
          

        
        
          
          // Two tailed
        
        
          
          p_value_2 = (score, distribution) => ʃ(distribution || N, score, +Ꝏ )+ʃ(distribution || N, -Ꝏ , -score)
        
        
          
          

        
        
          
          p_value = p_value_2
        
        
          
          

        
        
          
          // Example: statistical significance:
        
        
          
          

        
        
          
          a = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
        
        
          
          b = [ 10, 8, 7, 5, 3, 6, 1, 9, 4, 3 ]
        
        
          
          

        
        
          
          p_value(z(a, b)) < 0.05
        
        
          
          

        
        
          
          a = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 ]
        
        
          
          b = [ 0, 0, 0, 1, 0, 0, 1, 0, 0, 1 ]
        
        
          
          

        
        
          
          p_value(z(a, b)) < 0.05
        
  