File: poisson.c

package info (click to toggle)
spd 1.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,572 kB
  • sloc: ansic: 25,938; fortran: 10,483; sh: 1,032; makefile: 75
file content (286 lines) | stat: -rw-r--r-- 6,770 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 *   Project: The SPD Image correction and azimuthal regrouping
 *                      http://forge.epn-campus.eu/projects/show/azimuthal
 *
 *   Copyright (C) 2005-2010 European Synchrotron Radiation Facility
 *                           Grenoble, France
 *
 *   Principal authors: P. Boesecke (boesecke@esrf.fr)
 *                      R. Wilcke (wilcke@esrf.fr)
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   and the GNU Lesser General Public License  along with this program.
 *   If not, see <http://www.gnu.org/licenses/>.
 */

# define POISSON_VERSION      "poisson : V1.1 Peter Boesecke 2007-02-21"
/*+++------------------------------------------------------------------------
NAME
   gauss --- routines for poissonian distributions

SYNOPSIS

   # include poisson.h

HISTORY
  2000-11-17 V0.0 Peter Boesecke creation, problem in SumPoisson20
                  All routines to approximate SumPoisson taken from
                  http://www.io.com/~ritter/JAVASCRP/BINOMPOI.HTM
  2000-11-18 V0.1 PB InvSumPoisson loop improved
  2000-11-18 V0.2 SumPoisson works now correctly 
  2000-11-22 V1.0 Generates now correct mean and sigma
  2007-02-21 V1.1 POISSON_VERSION updated and pi defined as constant. 
                  SaxsDefinition.h is not needed any more
----------------------------------------------------------------------------*/
# include "poisson.h"

#ifndef ROUND
# define ROUND( x ) floor( ( x ) + 0.5 )
#endif
#ifndef MAX2
# define MAX2( x, y ) (( x ) < ( y ))?( y ):( x )
#endif


long fac( long x )  // x!
{ 
  long t = 1l;
  while (x > 1l)
    t *= x--;
  return( t );
} /* fac */

double logfac( long x ) // log(x!)
{ // by Stirling's formula Knuth I: 111
  const double pi = 3.1415926535897932384626;
  double invx, invx2, invx3, invx5, invx7;
  double sum;

  if (x <= 1l)  x = 1l;

  if (x < 12l)
    return log( fac( x ) );
   else {
    invx = (double) 1.0 / (double) x;
    invx2 = invx * invx;
    invx3 = invx2 * invx;
    invx5 = invx3 * invx2;
    invx7 = invx5 * invx2;

    sum = ((x + 0.5) * log(x)) - x;
    sum += log(2*pi) * 0.5;
    sum += (invx / 12.0) - (invx3 / 360.0);
    sum += (invx5 / 1260.0) - (invx7 / 1680.0);

    return ( sum );
    }
} /* logfac */

double g( double x ) 
{ // Peizer & Pratt 1968, JASA 63: 1416-1456
  const double eps = 1e-10;
  const double  switchlev = 0.1;
  double z, d, di;
  long i;

  if (x == 0)  
    z = 1;
   else 
    if (fabs(x-1.0)<eps) z = 0;
     else {
      d = 1.0 - x;

      if (fabs(d) > switchlev)
        z = (1.0 - (x * x) + (2.0 * x * log(x))) / (d * d);
       else {
        z = d / 3.0;  // first term
        di = d;  // d**1

        for (i = 2l; i <= 7l; i++) {
          di *= d;  // d**i
          z += (2.0 * di) / ((i+1) * (i+2));
          }
        }
      }

  return ( z );

} /* g */

double IntGauss1( double x ) 
{ // Abramowitz & Stegun 26.2.19
  double 
    d1 = 0.0498673470,
    d2 = 0.0211410061,
    d3 = 0.0032776263,
    d4 = 0.0000380036,
    d5 = 0.0000488906,
    d6 = 0.0000053830;
  double a,t;

  a = fabs(x),

  t = 1.0 + a*(d1+a*(d2+a*(d3+a*(d4+a*(d5+a*d6)))));

  // to 16th power
  t *= t;  t *= t;  t *= t;  t *= t;
  t = 1.0 / (t+t);  // the MINUS 16th

  if (x >= 0)  t = 1-t;
  return( t );

} /* IntGauss1 */

double SumPoisson20( long k, double u ) // Integral(0,k,Poisson(k,u)) for k>20
{ // Peizer & Pratt 1968, JASA 63: 1416-1456
 
  double s;
  double d1, d2;
  double z;

  s = (double) k + (double) (1.0/2.0); 

  d1 = (double) k - u + (double) (2.0/3.0);
  d2 = d1 + (double) 0.02/(double) (k+1l);

  z = (1.0 + g(s/u)) / u;
  z = d2 * sqrt(z);
  z = IntGauss1( z );

  return( z );

} /* SumPoisson20 */

double Poisson( long k, double ny )   // poisson distribution
{
  double value = 1.0;
  long i;
  double logsum = 0.0;

  for (i=1;i<=k;i++) {
   logsum += log(ny/i);
   }

  value *= exp(-ny+logsum);

  return ( value );

} /* Poisson */

double Poisson1( long k, double ny )   // poisson distribution Sterling
{ 
  return ( exp(-ny+log(ny)*k-logfac(k)) );
} /* Poisson */

double SumPoisson( long k, double ny ) // Sum(0,k,Poisson(k,ny))
/* cumulative sum of the poisson distribution */
{ 
  double sum;
  long j;

  if (k >= 20) 
    sum = SumPoisson20( k, ny );
   else 
  {
    sum = 0.0; j = 0;
    while (j <= k)
      if (j<12) sum += Poisson( j++, ny ); else sum += Poisson1( j++, ny );
    if (sum > 1.0) sum = 1.0;
  }
  return( sum );
} /* SumPoisson */

double IntPoisson( double k, double ny ) // Integral(0,k,Poisson(k,ny))
{ // interpolation of SumPoisson
  long k1, k2;
  double y1;
  double value;

  k1    = floor(k); k2 = k1+1;
  y1    = SumPoisson( k1 , ny );
  value =  y1 + Poisson( k2 , ny ) * (k-k1);

  return( value );

} /* IntPoisson */

long InvSumPoisson ( double y, double ny ) // Inverted SumPoisson
{ // Newton tangential approximation
  const int imax = 200;
  const double diffeps = 1e-14;
  const double amin = 1e-16;
  double k, kold;
  double yn;
  double a, b;
  double eps;
  int i=0;

  if (ny<1e-6) return( 0.0 );

  if (ny>1) eps = sqrt(ny)/10.0;
   else eps = ny/10.0;

  k = ny;
  for (i=1;i<imax;i++) {
    yn = IntPoisson( k, ny );

    if ( ny < 100 ) a = Poisson( ceil(k), ny );
      else a = Poisson( floor(k), ny );


    if (fabs(a)<amin) break;
    if ((a>0) && ( 1.0-yn < diffeps )) break;
    if ((a<0) && (     yn < diffeps )) break;

    b = yn-a*k;
    kold =k; k = (y - b)/a; if (k<0) k=0;

    if (fabs(k-kold)<eps) break;
     
    }

  return( ceil(k) );

} /* InvSumPoisson */

void PoissonNoiseSeed( unsigned int seed ) // set random number seed
{  srand( seed );
} /* PoissonNoiseSeed */

long PoissonNoise( double ny ) // create poissonian distributed noise
{
  int rannum = rand();
  double p;
  long value;

  /* create random numbers between 0 and 1 */
  p = rannum/(RAND_MAX+1.0);

  /* project the range 0 to 1 to the x-range of the poisson distribution */
  value =  InvSumPoisson ( p, ny );
  return ( value );

} /* PoissonNoise */

double RandomNoise( void ) // create random noise between 0.0 and 1.0 
{
  int rannum = rand();
  double value;

  /* create random numbers between 0 and 1 */
  value = rannum/(RAND_MAX+1.0);

  return ( value );

} /* PoissonNoise */