forked from mathnet/mathnet-yttrium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PoissonDistribution.cs
234 lines (214 loc) · 7.5 KB
/
PoissonDistribution.cs
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
//-----------------------------------------------------------------------
// <copyright file="PoissonDistribution.cs" company="Math.NET Project">
// Copyright (c) 2002-2009, Christoph Rüegg.
// All Right Reserved.
// </copyright>
// <author>
// Christoph Rüegg, http://christoph.ruegg.name
// </author>
// <product>
// Math.NET Iridium, part of the Math.NET Project.
// http://mathnet.opensourcedotnet.info
// </product>
// <license type="opensource" name="LGPL" version="2 or later">
// 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 2 of the License, or
// 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 Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
// </license>
// <contribution>
// Troschuetz.Random Class Library, Stefan Troschütz (stefan@troschuetz.de)
// CNClass, Aachen University of Technology, Dirk Grunwald (grunwald@cs.uiuc.edu)
// </contribution>
//-----------------------------------------------------------------------
using System;
namespace MathNet.Numerics.Distributions
{
using MathNet.Numerics.RandomSources;
/// <summary>
/// Pseudo-random generation of poisson distributed deviates.
/// </summary>
/// <remarks>
/// <para>
/// For details about this distribution, see
/// <a href="http://en.wikipedia.org/wiki/Poisson_distribution">
/// Wikipedia - Poisson distribution</a>.
/// </para>
/// <para>
/// For details of the algorithm, see
/// <a href="http://www.lkn.ei.tum.de/lehre/scn/cncl/doc/html/cncl_toc.html">
/// Communication Networks Class Library (TU München)</a>
/// </para>
/// <para>
/// pdf: f(x) = exp(-l)*l^x/x!; l = lambda
/// </para>
/// </remarks>
public sealed class PoissonDistribution : DiscreteDistribution
{
double _lambda;
double _helper1;
#region construction
/// <summary>
/// Initializes a new instance of the PoissonDistribution class,
/// using a <see cref="SystemRandomSource"/> as underlying random number generator.
/// </summary>
public
PoissonDistribution()
: base()
{
SetDistributionParameters(1.0);
}
/// <summary>
/// Initializes a new instance of the PoissonDistribution class,
/// using the specified <see cref="RandomSource"/> as underlying random number generator.
/// </summary>
/// <param name="random">A <see cref="RandomSource"/> object.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="random"/> is NULL (<see langword="Nothing"/> in Visual Basic).
/// </exception>
public
PoissonDistribution(RandomSource random)
: base(random)
{
SetDistributionParameters(1.0);
}
/// <summary>
/// Initializes a new instance of the PoissonDistribution class,
/// using a <see cref="SystemRandomSource"/> as underlying random number generator.
/// </summary>
public
PoissonDistribution(double lambda)
: base()
{
SetDistributionParameters(lambda);
}
#endregion
#region Distribution Parameters
/// <summary>
/// Gets or sets the lambda parameter.
/// </summary>
public double Lambda
{
get { return _lambda; }
set { SetDistributionParameters(value); }
}
/// <summary>
/// Configure all distribution parameters.
/// </summary>
public
void
SetDistributionParameters(double lambda)
{
if(!IsValidParameterSet(lambda))
{
throw new ArgumentException(Properties.LocalStrings.ArgumentParameterSetInvalid, "lambda");
}
_lambda = lambda;
_helper1 = Math.Exp(-_lambda);
}
/// <summary>
/// Determines whether the specified parameters are valid.
/// </summary>
/// <returns>
/// <see langword="true"/> if value is greater than 0.0; otherwise, <see langword="false"/>.
/// </returns>
public static
bool
IsValidParameterSet(double lambda)
{
return lambda > 0.0;
}
#endregion
#region Distribution Properties
/// <summary>
/// Gets the minimum possible value of generated random numbers.
/// </summary>
public override int Minimum
{
get { return 0; }
}
/// <summary>
/// Gets the maximum possible value of generated random numbers.
/// </summary>
public override int Maximum
{
get { return int.MaxValue; }
}
/// <summary>
/// Gets the mean value of generated random numbers.
/// </summary>
public override double Mean
{
get { return _lambda; }
}
/// <summary>
/// Gets the median of generated random numbers.
/// </summary>
public override int Median
{ // approximation, see Wikipedia
get { return (int)Math.Floor(_lambda + (1.0 / 3.0) - (0.2 / _lambda)); }
}
/// <summary>
/// Gets the variance of generated random numbers.
/// </summary>
public override double Variance
{
get { return _lambda; }
}
/// <summary>
/// Gets the skewness of generated random numbers.
/// </summary>
public override double Skewness
{
get { return Math.Sqrt(_lambda); }
}
/// <summary>
/// Discrete probability mass function (pmf) of this probability distribution.
/// </summary>
public override
double
ProbabilityMass(int x)
{
return Math.Exp(
-_lambda
+ (x * Math.Log(_lambda))
- Fn.FactorialLn(x));
}
/// <summary>
/// Continuous cumulative distribution function (cdf) of this probability distribution.
/// </summary>
public override
double
CumulativeDistribution(double x)
{
return 1.0 - Fn.GammaRegularized(x + 1, _lambda);
}
#endregion
#region Generator
/// <summary>
/// Returns a poisson distributed random number.
/// </summary>
/// <returns>A poisson distributed 32-bit signed integer.</returns>
public override
int
NextInt32()
{
int count = 0;
for(double product = this.RandomSource.NextDouble(); product >= _helper1; product *= this.RandomSource.NextDouble())
{
count++;
}
return count;
}
#endregion
}
}