Skip to content

Commit 7e0b98c

Browse files
committed
Statistics: TruncatedNormal minor formatting tweaks #344
1 parent 701027f commit 7e0b98c

File tree

3 files changed

+71
-71
lines changed

3 files changed

+71
-71
lines changed

src/Numerics/Distributions/TruncatedNormal.cs

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// http://github.com/mathnet/mathnet-numerics
55
// http://mathnetnumerics.codeplex.com
66
//
7-
// Copyright (c) 2009-2013 Math.NET
7+
// Copyright (c) 2009-2015 Math.NET
88
//
99
// Permission is hereby granted, free of charge, to any person
1010
// obtaining a copy of this software and associated documentation
@@ -42,24 +42,27 @@ namespace MathNet.Numerics.Distributions {
4242
/// </summary>
4343
public class TruncatedNormal : IContinuousDistribution
4444
{
45-
4645
System.Random _random;
4746

4847
/// <summary>
4948
/// Mean of the untruncated normal distribution.
5049
/// </summary>
51-
readonly double _mu;
50+
readonly double _mu;
51+
5252
/// <summary>
5353
/// Standard deviation of the uncorrected normal distribution.
5454
/// </summary>
5555
readonly double _sigma;
56+
5657
readonly double _lowerBound;
5758
readonly double _upperBound;
5859
readonly Normal _standardNormal = new Normal(0.0, 1.0);
60+
5961
/// <summary>
6062
/// Position in the standard normal distribution of the lower bound.
6163
/// </summary>
6264
readonly double _alpha;
65+
6366
/// <summary>
6467
/// Position in the standard normal distribution of the upper bound.
6568
/// </summary>
@@ -73,18 +76,17 @@ public class TruncatedNormal : IContinuousDistribution
7376

7477
/// <summary>
7578
/// Initializes a new instance of the TruncatedNormal class. The distribution will
76-
/// be initialized with the default <seealso cref="System.Random"/> random number generator. The mean
79+
/// be initialized with the default <seealso cref="System.Random"/> random number generator. The mean
7780
/// and standard deviation are that of the untruncated normal distribution.
7881
/// </summary>
7982
/// <param name="mean">The mean (μ) of the untruncated distribution.</param>
8083
/// <param name="stddev">The standard deviation (σ) of the untruncated distribution. Range: σ > 0.</param>
8184
/// <param name="lowerBound">The inclusive lower bound of the truncated distribution. Default is double.NegativeInfinity.</param>
8285
/// <param name="upperBound">The inclusive upper bound of the truncated distribution. Must be larger than <paramref name="lowerBound"/>.
8386
/// Default is double.PositiveInfinity.</param>
84-
public TruncatedNormal(double mean, double stddev, double lowerBound = double.NegativeInfinity, double upperBound = double.PositiveInfinity)
85-
:this(mean, stddev, SystemRandomSource.Default, lowerBound, upperBound)
87+
public TruncatedNormal(double mean, double stddev, double lowerBound = double.NegativeInfinity, double upperBound = double.PositiveInfinity)
88+
: this(mean, stddev, SystemRandomSource.Default, lowerBound, upperBound)
8689
{
87-
8890
}
8991

9092
/// <summary>
@@ -98,9 +100,9 @@ public TruncatedNormal(double mean, double stddev, double lowerBound = double.Ne
98100
/// <param name="upperBound">The inclusive upper bound of the truncated distribution. Must be larger than <paramref name="lowerBound"/>.
99101
/// Default is double.PositiveInfinity.</param>
100102

101-
public TruncatedNormal(double untruncatedMean, double untruncatedStdDev, System.Random randomSource, double lowerBound = double.NegativeInfinity, double upperBound = double.PositiveInfinity)
103+
public TruncatedNormal(double untruncatedMean, double untruncatedStdDev, System.Random randomSource, double lowerBound = double.NegativeInfinity, double upperBound = double.PositiveInfinity)
102104
{
103-
if (!IsValidParameterSet(untruncatedMean, untruncatedStdDev, lowerBound, upperBound))
105+
if (!IsValidParameterSet(untruncatedMean, untruncatedStdDev, lowerBound, upperBound))
104106
{
105107
throw new ArgumentException(Resources.InvalidDistributionParameters);
106108
}
@@ -121,23 +123,24 @@ public TruncatedNormal(double untruncatedMean, double untruncatedStdDev, System.
121123
/// </summary>
122124
/// <param name="mean">The mean (μ) of the normal distribution.</param>
123125
/// <param name="stddev">The standard deviation (σ) of the normal distribution. Range: σ > 0.</param>
124-
public static bool IsValidParameterSet(double mean, double stddev, double lowerBound, double upperBound)
126+
public static bool IsValidParameterSet(double mean, double stddev, double lowerBound, double upperBound)
125127
{
126128
bool normalRequirements = Normal.IsValidParameterSet(mean, stddev) && stddev > 0;
127129
bool boundsAreOrdered = lowerBound < upperBound;
128130
return normalRequirements && boundsAreOrdered;
129131
}
130132

131-
public override string ToString() {
133+
public override string ToString()
134+
{
132135
return "TruncatedNormal(μ = " + _mu + ", σ = " + _sigma +", LowerBound = " + _lowerBound + ", UpperBound = " + _upperBound + ")";
133136
}
134137

135-
/// <summary>
136-
/// Gets the mode of the normal distribution.
137-
/// </summary>
138-
public double Mode
138+
/// <summary>
139+
/// Gets the mode of the truncated normal distribution.
140+
/// </summary>
141+
public double Mode
139142
{
140-
get
143+
get
141144
{
142145
if (_mu < _lowerBound)
143146
return _lowerBound;
@@ -150,25 +153,25 @@ public double Mode
150153
/// <summary>
151154
/// Gets the minimum of the truncated normal distribution.
152155
/// </summary>
153-
public double Minimum
156+
public double Minimum
154157
{
155158
get { return _lowerBound; }
156159
}
157160

158161
/// <summary>
159162
/// Gets the maximum of the truncated normal distribution.
160163
/// </summary>
161-
public double Maximum
164+
public double Maximum
162165
{
163166
get { return _upperBound; }
164167
}
165168

166169
/// <summary>
167170
/// Gets the mean (μ) of the truncated normal distribution.
168171
/// </summary>
169-
public double Mean
172+
public double Mean
170173
{
171-
get
174+
get
172175
{
173176
var pdfDifference = _standardNormal.Density(_alpha) - _standardNormal.Density(_beta);
174177
var diffFromUncorrected = pdfDifference * _sigma / _cumulativeDensityWithinBounds;
@@ -177,11 +180,11 @@ public double Mean
177180
}
178181

179182
/// <summary>
180-
/// Gets the variance of the truncated normal distribution.
183+
/// Gets the variance of the truncated normal distribution.
181184
/// </summary>
182-
public double Variance
185+
public double Variance
183186
{
184-
get
187+
get
185188
{
186189
//Apparently "Barr and Sherrill (1999)" has a simpler expression for one sided truncations, if anyone has access...
187190

@@ -209,17 +212,17 @@ public double Variance
209212
/// <summary>
210213
/// Gets the standard deviation (σ) of the truncated normal distribution. Range: σ > 0.
211214
/// </summary>
212-
public double StdDev
215+
public double StdDev
213216
{
214217
get { return Math.Sqrt(Variance); }
215218
}
216219

217220
/// <summary>
218221
/// Gets the entropy of the truncated normal distribution.
219222
/// </summary>
220-
public double Entropy
223+
public double Entropy
221224
{
222-
get
225+
get
223226
{
224227
var firstTerm = Constants.LogSqrt2PiE + Math.Log(_sigma + _cumulativeDensityWithinBounds);
225228

@@ -232,7 +235,7 @@ public double Entropy
232235

233236
public double Skewness
234237
{
235-
get
238+
get
236239
{
237240
throw new NotImplementedException();
238241
}
@@ -241,9 +244,9 @@ public double Skewness
241244
/// <summary>
242245
/// Gets the median of the truncated distribution.
243246
/// </summary>
244-
public double Median
247+
public double Median
245248
{
246-
get
249+
get
247250
{
248251
return InverseCumulativeDistribution(0.5);
249252
}
@@ -252,7 +255,7 @@ public double Median
252255
/// <summary>
253256
/// Gets or sets the random number generator which is used to draw random samples.
254257
/// </summary>
255-
public System.Random RandomSource
258+
public System.Random RandomSource
256259
{
257260
get { return _random; }
258261
set { _random = value ?? SystemRandomSource.Default; }
@@ -264,7 +267,7 @@ public System.Random RandomSource
264267
/// <param name="x">The location at which to compute the density.</param>
265268
/// <returns>the density at <paramref name="x"/>.</returns>
266269
/// <seealso cref="PDF"/>
267-
public double Density(double x)
270+
public double Density(double x)
268271
{
269272
if (x < _lowerBound || _upperBound < x)
270273
return 0d;
@@ -278,29 +281,30 @@ public double Density(double x)
278281
/// <param name="x">The location at which to compute the log density.</param>
279282
/// <returns>the log density at <paramref name="x"/>.</returns>
280283
/// <seealso cref="PDFLn"/>
281-
public double DensityLn(double x)
284+
public double DensityLn(double x)
282285
{
283286
return _standardNormal.DensityLn((x - _mu) / _sigma) - Math.Log(_sigma) - Math.Log(_cumulativeDensityWithinBounds);
284-
}
287+
}
285288

286-
287-
public double Sample()
289+
public double Sample()
288290
{
289291
//TODO: implement sampling more efficiently/accurately, use method described by Mazet here: http://miv.u-strasbg.fr/mazet/rtnorm/
290292
// see implementations listed on that page for examples.
291293
return InverseCumulativeDistribution(RandomSource.NextDouble());
292294
}
293295

294-
public void Samples(double[] values)
296+
public void Samples(double[] values)
295297
{
296-
for(int i = 0; i < values.Length; i++) {
298+
for(int i = 0; i < values.Length; i++)
299+
{
297300
values[i] = Sample();
298301
}
299302
}
300303

301-
public IEnumerable<double> Samples()
304+
public IEnumerable<double> Samples()
302305
{
303-
while (true) {
306+
while (true)
307+
{
304308
yield return Sample();
305309
}
306310
}
@@ -311,7 +315,7 @@ public IEnumerable<double> Samples()
311315
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
312316
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
313317
/// <seealso cref="CDF"/>
314-
public double CumulativeDistribution(double x)
318+
public double CumulativeDistribution(double x)
315319
{
316320
if (x < _lowerBound)
317321
return 0d;
@@ -329,13 +333,12 @@ public double CumulativeDistribution(double x)
329333
/// <param name="p">The location at which to compute the inverse cumulative density.</param>
330334
/// <returns>the inverse cumulative density at <paramref name="p"/>.</returns>
331335
/// <seealso cref="InvCDF"/>
332-
public double InverseCumulativeDistribution(double p)
336+
public double InverseCumulativeDistribution(double p)
333337
{
334338
//TODO check that this is correct with someone.
335339
var pUntruncated = p * _cumulativeDensityWithinBounds + _standardNormal.CumulativeDistribution(_alpha);
336340

337341
return _standardNormal.InverseCumulativeDistribution(pUntruncated) * _sigma + _mu;
338342
}
339-
340343
}
341344
}

src/UnitTests/DistributionTests/CommonDistributionTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ public class CommonDistributionTests
8989
new StudentT(0.0, 1.0, 5.0),
9090
new Triangular(0, 1, 0.7),
9191
new Weibull(1.0, 1.0),
92-
new TruncatedNormal(0, 1.0, -1.0, 1.5), //Finite
93-
new TruncatedNormal(0, 1.0, -0.5), //Semi-finite
92+
new TruncatedNormal(0, 1.0, -1.0, 1.5), //Finite
93+
new TruncatedNormal(0, 1.0, -0.5), //Semi-finite
9494
};
9595

9696
[Test]

0 commit comments

Comments
 (0)