vnl_chi_squared.hxx
Go to the documentation of this file.
1 // This is core/vnl/algo/vnl_chi_squared.hxx
2 #ifndef vnl_chi_squared_hxx_
3 #define vnl_chi_squared_hxx_
4 //:
5 
6 #include "vnl_chi_squared.h"
7 
8 //------------------------------------------------------------
9 
10 // FORTRAN routine
11 #include <vnl/algo/vnl_netlib.h> // for dchscdf_()
12 
13 template <class T>
14 double vnl_chi_squared_cumulative(T chisq, long dof)
15 {
16  double cdf, chisqr = chisq;
17  v3p_netlib_dchscdf_(&chisqr,&dof,&cdf);
18  return cdf;
19 }
20 
21 //------------------------------------------------------------
22 
23 template <class T>
24 double vnl_chi_squared_statistic_1 (T const *A, T const *B, int n, bool normalize)
25 {
26  double sum = 0;
27 
28  if (normalize)
29  {
30  T sumA = 0;
31  T sumB = 0;
32  for (int i=0; i<n; ++i) {
33  sumA += A[i];
34  sumB += B[i];
35  }
36 
37  for (int i=0; i<n; ++i)
38  if (A[i]) {
39  double a = double(A[i])/sumA;
40  double b = double(B[i])/sumB;
41  double tmp = a - b;
42  sum += tmp*tmp/a;
43  }
44  }
45  else
46  {
47  for (int i=0; i<n; ++i)
48  if (A[i]) {
49  double tmp = A[i] - B[i];
50  sum += tmp*tmp/A[i];
51  }
52  }
53 
54  return sum;
55 }
56 
57 template <class T>
58 double vnl_chi_squared_statistic_2 (T const *A, T const *B, int n, bool normalize)
59 {
60  return vnl_chi_squared_statistic_1(B, A, n, normalize);
61 }
62 
63 template <class T>
64 double vnl_chi_squared_statistic_12(T const *A, T const *B, int n, bool normalize)
65 {
66  double sum = 0;
67 
68  if (normalize)
69  {
70  T sumA = 0;
71  T sumB = 0;
72  for (int i=0; i<n; ++i) {
73  sumA += A[i];
74  sumB += B[i];
75  }
76 
77  for (int i=0; i<n; ++i)
78  if (A[i] || B[i]) {
79  double a = double(A[i])/sumA;
80  double b = double(B[i])/sumB;
81  double tmp = a - b;
82  sum += tmp*tmp/(a + b);
83  }
84  }
85  else
86  {
87  for (int i=0; i<n; ++i)
88  if (A[i] || B[i]) {
89  double tmp = A[i] - B[i];
90  sum += tmp*tmp/(A[i] + B[i]);
91  }
92  }
93 
94  return sum;
95 }
96 
97 #undef VNL_CHI_SQUARED_INSTANTIATE
98 #define VNL_CHI_SQUARED_INSTANTIATE(T) \
99 template VNL_ALGO_EXPORT double vnl_chi_squared_cumulative (T chisq, long dof); \
100 template VNL_ALGO_EXPORT double vnl_chi_squared_statistic_1 (T const *, T const *, int, bool); \
101 template VNL_ALGO_EXPORT double vnl_chi_squared_statistic_2 (T const *, T const *, int, bool); \
102 template VNL_ALGO_EXPORT double vnl_chi_squared_statistic_12(T const *, T const *, int, bool)
103 
104 #endif // vnl_chi_squared_hxx_
double vnl_chi_squared_statistic_1(T const *A, T const *B, int n, bool normalize)
Name space for various chi-squared distribution functions.
double vnl_chi_squared_statistic_12(T const *A, T const *B, int n, bool normalize)
Data normalize(const vnl_bignum &b1, const vnl_bignum &b2, vnl_bignum &u, vnl_bignum &v)
normalize two vnl_bignums.
Declare in a central place the list of symbols from netlib.
double vnl_chi_squared_cumulative(T chisq, long dof)
Compute cumulative distribution function value for chi-squared distribution.
Name space for various (mostly templated) chi-squared distribution functions.
double vnl_chi_squared_statistic_2(T const *A, T const *B, int n, bool normalize)