This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
SkewNormalDistribution.cs
385 lines (349 loc) · 13.3 KB
/
SkewNormalDistribution.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Accord Statistics Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2017
// cesarsouza at gmail.com
//
// This library 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.1 of the License, or (at your option) any later version.
//
// This library 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 library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
namespace Accord.Statistics.Distributions.Univariate
{
using System;
using Accord.Math;
using Accord.Compat;
/// <summary>
/// Skew Normal distribution.
/// </summary>
///
/// <remarks>
/// <para>
/// In probability theory and statistics, the skew normal distribution is a
/// <see cref="UnivariateContinuousDistribution">continuous probability distribution</see>
/// that generalises the <see cref="NormalDistribution">normal distribution</see> to allow
/// for non-zero <see cref="NormalDistribution.Skewness">skewness</see>.</para>
///
/// <para>
/// References:
/// <list type="bullet">
/// <item><description><a href="https://en.wikipedia.org/wiki/Skew_normal_distribution">
/// Wikipedia, The Free Encyclopedia. Skew normal distribution. Available on:
/// https://en.wikipedia.org/wiki/Skew_normal_distribution </a></description></item>
/// </list></para>
/// </remarks>
///
/// <example>
/// <para>
/// This examples shows how to create a Skew normal distribution
/// and compute some of its properties and derived measures.</para>
///
/// <code>
/// // Create a Skew normal distribution with location 2, scale 3 and shape 4.2
/// var skewNormal = new SkewNormalDistribution(location: 2, scale: 3, shape: 4.2);
///
/// double mean = skewNormal.Mean; // 4.3285611780515953
/// double median = skewNormal.Median; // 4.0230040653062265
/// double var = skewNormal.Variance; // 3.5778028400709641
/// double mode = skewNormal.Mode; // 3.220622226764422
///
/// double cdf = skewNormal.DistributionFunction(x: 1.4); // 0.020166854942526125
/// double pdf = skewNormal.ProbabilityDensityFunction(x: 1.4); // 0.052257431834162059
/// double lpdf = skewNormal.LogProbabilityDensityFunction(x: 1.4); // -2.9515731621912877
///
/// double ccdf = skewNormal.ComplementaryDistributionFunction(x: 1.4); // 0.97983314505747388
/// double icdf = skewNormal.InverseDistributionFunction(p: cdf); // 1.3999998597203041
///
/// double hf = skewNormal.HazardFunction(x: 1.4); // 0.053332990517581239
/// double chf = skewNormal.CumulativeHazardFunction(x: 1.4); // 0.020372981958858238
///
/// string str = skewNormal.ToString(CultureInfo.InvariantCulture); // Sn(x; ξ = 2, ω = 3, α = 4.2)
/// </code>
/// </example>
///
/// <seealso cref="Accord.Statistics.Distributions.Univariate.NormalDistribution"/>
/// <seealso cref="Accord.Statistics.Distributions.Multivariate.MultivariateNormalDistribution"/>
///
[Serializable]
public class SkewNormalDistribution : UnivariateContinuousDistribution
{
// Distribution parameters
private double ksi = 0; // ξ location (real)
private double omega = 1; // ω scale (positive, real)
private double alpha = 0; // α shape (real)
// Derived parameters
double delta;
/// <summary>
/// Constructs a Skew normal distribution with
/// zero location, unit scale and zero shape.
/// </summary>
///
public SkewNormalDistribution()
{
initialize(ksi, omega, alpha);
}
/// <summary>
/// Constructs a Skew normal distribution with
/// given location, unit scale and zero skewness.
/// </summary>
///
/// <param name="location">The distribution's location value ξ (ksi).</param>
///
public SkewNormalDistribution([Real] double location)
{
initialize(location, omega, alpha);
}
/// <summary>
/// Constructs a Skew normal distribution with
/// given location and scale and zero skewness.
/// </summary>
///
/// <param name="location">The distribution's location value ξ (ksi).</param>
/// <param name="scale">The distribution's scale value ω (omega).</param>
///
public SkewNormalDistribution([Real] double location, [Positive] double scale)
{
if (scale <= 0)
throw new ArgumentOutOfRangeException("scale", "Scale must be positive.");
initialize(location, scale, alpha);
}
/// <summary>
/// Constructs a Skew normal distribution
/// with given mean and standard deviation.
/// </summary>
///
/// <param name="location">The distribution's location value ξ (ksi).</param>
/// <param name="scale">The distribution's scale value ω (omega).</param>
/// <param name="shape">The distribution's shape value α (alpha).</param>
///
public SkewNormalDistribution([Real] double location, [Positive] double scale, [Real] double shape)
{
if (scale <= 0)
throw new ArgumentOutOfRangeException("scale", "Scale must be positive.");
initialize(location, scale, shape);
}
/// <summary>
/// Gets the skew-normal distribution's location value ξ (ksi).
/// </summary>
///
public double Location
{
get { return ksi; }
}
/// <summary>
/// Gets the skew-normal distribution's scale value ω (omega).
/// </summary>
///
public double Scale
{
get { return omega; }
}
/// <summary>
/// Gets the skew-normal distribution's shape value α (alpha).
/// </summary>
///
public double Shape
{
get { return alpha; }
}
/// <summary>
/// Not supported.
/// </summary>
///
public override double Entropy
{
get { throw new NotSupportedException(); }
}
/// <summary>
/// Gets the mean for this distribution.
/// </summary>
///
/// <value>
/// The distribution's mean value.
/// </value>
///
public override double Mean
{
get { return ksi + omega * delta * Math.Sqrt(2.0 / Math.PI); }
}
/// <summary>
/// Gets the variance for this distribution.
/// </summary>
///
/// <value>
/// The distribution's variance.
/// </value>
///
public override double Variance
{
get { return omega * omega * (1 - (2 * delta * delta) / Math.PI); }
}
/// <summary>
/// Gets the skewness for this distribution.
/// </summary>
///
public double Skewness
{
get
{
double a = (4 - Math.PI) / 2.0;
double b = delta * Math.Sqrt(2.0 / Math.PI);
double c = 1 - 2 * delta * delta / Math.PI;
return a * (b * b * b) / Math.Pow(c, 3 / 2);
}
}
/// <summary>
/// Gets the excess kurtosis for this distribution.
/// </summary>
///
public double Kurtosis
{
get
{
double a = 2 * (Math.PI * 3);
double b = delta * Math.Sqrt(2.0 / Math.PI);
double c = 1 - 2 * delta * delta / Math.PI;
return a * (b * b * b * b) / (c * c);
}
}
/// <summary>
/// Gets the support interval for this distribution.
/// </summary>
///
/// <value>
/// A <see cref="DoubleRange" /> containing
/// the support interval for this distribution.
/// </value>
///
public override DoubleRange Support
{
get { return new DoubleRange(Double.NegativeInfinity, Double.PositiveInfinity); }
}
/// <summary>
/// Gets the cumulative distribution function (cdf) for
/// this distribution evaluated at point <c>x</c>.
/// </summary>
///
/// <param name="x">A single point in the distribution range.</param>
///
/// <remarks>
/// The Cumulative Distribution Function (CDF) describes the cumulative
/// probability that a given value or any value smaller than it will occur.
/// </remarks>
///
protected internal override double InnerDistributionFunction(double x)
{
double z = (x - ksi) / omega;
double cdf = Accord.Math.Normal.Function(z) - 2 * OwensT.Function(z, alpha);
return cdf;
}
/// <summary>
/// Gets the probability density function (pdf) for
/// this distribution evaluated at point <c>x</c>.
/// </summary>
///
/// <param name="x">A single point in the distribution range.</param>
///
/// <returns>
/// The probability of <c>x</c> occurring in the current distribution.
/// </returns>
///
/// <remarks>
/// The Probability Density Function (PDF) describes the
/// probability that a given value <c>x</c> will occur.
/// </remarks>
///
protected internal override double InnerProbabilityDensityFunction(double x)
{
double z = (x - ksi) / omega;
double a = Accord.Math.Normal.Derivative(z);
double b = Accord.Math.Normal.Function(alpha * z);
return (2 / omega) * a * b;
}
/// <summary>
/// Gets the log-probability density function (pdf) for
/// this distribution evaluated at point <c>x</c>.
/// </summary>
///
/// <param name="x">A single point in the distribution range.</param>
///
/// <returns>
/// The logarithm of the probability of <c>x</c>
/// occurring in the current distribution.
/// </returns>
///
/// <remarks>
/// The Probability Density Function (PDF) describes the
/// probability that a given value <c>x</c> will occur.
/// </remarks>
///
protected internal override double InnerLogProbabilityDensityFunction(double x)
{
double z = (x - ksi) / omega;
double a = Accord.Math.Normal.LogDerivative(z);
double b = Math.Log(Accord.Math.Normal.Function(alpha * z));
return Math.Log(2 / omega) + a + b;
}
/// <summary>
/// Creates a new object that is a copy of the current instance.
/// </summary>
/// <returns>
/// A new object that is a copy of this instance.
/// </returns>
///
public override object Clone()
{
return new SkewNormalDistribution(ksi, omega, alpha);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
///
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
///
public override string ToString(string format, IFormatProvider formatProvider)
{
return String.Format(formatProvider, "Sn(x; ξ = {0}, ω = {1}, α = {2})",
ksi.ToString(format, formatProvider),
omega.ToString(format, formatProvider),
alpha.ToString(format, formatProvider));
}
private void initialize(double location, double scale, double shape)
{
this.ksi = location;
this.omega = scale;
this.alpha = shape;
// Compute derived values
this.delta = shape / Math.Sqrt(1 + shape * shape);
}
/// <summary>
/// Create a new <see cref="SkewNormalDistribution"/> that
/// corresponds to a <see cref="NormalDistribution"/> with
/// the given mean and standard deviation.
/// </summary>
///
/// <param name="mean">The distribution's mean value μ (mu).</param>
/// <param name="stdDev">The distribution's standard deviation σ (sigma).</param>
///
/// <returns>A <see cref="SkewNormalDistribution"/> representing
/// a <see cref="NormalDistribution"/> with the given parameters.</returns>
///
public static SkewNormalDistribution Normal(int mean, double stdDev)
{
return new SkewNormalDistribution(mean, stdDev);
}
}
}