19524: Output PCA.
[lightning.git] / chisquare.go
1 // Copyright (C) The Lightning Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package lightning
6
7 import (
8         "golang.org/x/exp/rand"
9         "gonum.org/v1/gonum/stat/distuv"
10 )
11
12 var chisquared = distuv.ChiSquared{K: 1, Src: rand.NewSource(rand.Uint64())}
13
14 func pvalue(x, y []bool) float64 {
15         var (
16                 obs, exp [2]float64
17                 sum      float64
18                 sz       = float64(len(y))
19         )
20         for i, yi := range y {
21                 if x[i] {
22                         if yi {
23                                 obs[0]++
24                         } else {
25                                 obs[1]++
26                         }
27                 }
28                 if yi {
29                         exp[0]++
30                 } else {
31                         exp[1]++
32                 }
33         }
34         if exp[0] == 0 || exp[1] == 0 || obs[0]+obs[1] == 0 {
35                 return 1
36         }
37         exp[0] = (obs[0] + obs[1]) * exp[0] / sz
38         exp[1] = (obs[0] + obs[1]) * exp[1] / sz
39         for i := range exp {
40                 d := obs[i] - exp[i]
41                 sum += d * d / exp[i]
42         }
43         return chisquared.Survival(sum)
44 }