Bsoft  2.1.0
Bernard's software package
binomial.cpp
Go to the documentation of this file.
1 
9 #include "simplex.h"
10 #include "math_util.h"
11 #include "utilities.h"
12 
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15 
16 // Internal function
17 double Bnp_R(Bsimplex* simp);
18 
28 double Bnpk(int n, double p, int k, double w)
29 {
30  int i, nk(n-k);
31  double p_1 = 1 - p;
32  double Pnpk(1);
33 
34  double nc = number_of_combinations(n, k);
35 
36  for ( i=0; i<k; i++ ) Pnpk *= p;
37  for ( i=0; i<nk; i++ ) Pnpk *= p_1;
38  Pnpk *= nc * w;
39 
40  return Pnpk;
41 }
42 
43 double Bnp_R(Bsimplex& simp)
44 {
45  int i,j;
46  double R = 0; // normalized standard deviation
47  double df; // value of the minimization function
48  double fit, w;
49  vector<double>& f = simp.dependent_values();
50 
51  int n = (int) simp.constant(0);
52  int nfit = (int) simp.constant(1);
53 
54  // Normalize weights
55  for ( j=0, w=0; j<nfit; j++ ) w += simp.parameter(j);
56 
57  if ( w > 0 )
58  for ( j=0; j<nfit; j++ ) simp.parameter(j, simp.parameter(j) / w);
59  else
60  for ( j=0; j<nfit; j++ ) simp.parameter(j, 1.0/nfit);
61 
62  // Calculate residual
63  for ( i=0; i<simp.points(); i++ ) {
64  fit = 0;
65  for ( j=0; j<nfit; j++ )
66  fit += Bnpk(n, simp.parameter(nfit+j), i, simp.parameter(j));
67  df = f[i] - fit;
68  R += df*df;
69  }
70 
71  R = sqrt(R/i);
72 
73  return R;
74 }
75 
76 
89 double binomial_fit(vector<double>& distrib, int n, int nfit, vector<double>& coeff)
90 {
91  int i;
92  vector<double> x(n+1, 0);
93  Bsimplex simp(1, 2*nfit, 2, n+1, x, distrib);
94 
95  for ( i=0; i<2*nfit; i++ )
96  simp.limits(i, 0, 1); // probability/weight = [0,1]
97 
98  if ( nfit < 2 ) simp.limits(0, 1, 1);
99 
100  for ( i=0; i<nfit; i++ ) {
101  simp.parameter(i, 1.0/nfit); // initial weight
102  simp.parameter(i+nfit, (i + 1)*1.0/(nfit + 1)); // initial probability
103  }
104 
105  simp.constant(0, n); // number of x-points
106  simp.constant(1, nfit); // number of fitted B(n,p)
107 
108  // call the simplex function that minimizes Bnp_R
109  double R = simp.run(10000, 0.001, Bnp_R);
110 
111  for ( i=0; i<2*nfit; i++ ) coeff[i] = simp.parameter(i);
112 
113  return R;
114 }
115 
binomial_fit
double binomial_fit(vector< double > &distrib, int n, int nfit, vector< double > &coeff)
Fits a distribution to one or more binomial distributions.
Definition: binomial.cpp:89
Bsimplex::constant
double constant(long i)
Definition: simplex.h:61
simplex.h
Nelder and Mead downhill simplex method for generalized parameter fitting.
Bsimplex::dependent_values
vector< double > & dependent_values()
Definition: simplex.h:85
Bsimplex
Structure used in the downhill simplex method.
Definition: simplex.h:44
Bsimplex::points
long points()
Definition: simplex.h:66
Bnp_R
double Bnp_R(Bsimplex *simp)
number_of_combinations
double number_of_combinations(int n, int r)
Calculates the number of combinations of size r in set of size n.
Definition: math_util.cpp:97
Bsimplex::run
double run(long maxcycles, double tolerance, double(funk)(Bsimplex &))
Definition: simplex.cpp:76
Bsimplex::parameter
void parameter(long i, double p)
Definition: simplex.h:71
utilities.h
Header file for general utilities.
Bnpk
double Bnpk(int n, double p, int k, double w)
Calculates the probability of k occurrences in n cases.
Definition: binomial.cpp:28
math_util.h
Header file for general utilities.
verbose
int verbose
Bsimplex::limits
void limits(long i, double vlo, double vhi)
Definition: simplex.h:77