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
/
WishartDistribution.cs
667 lines (579 loc) · 24 KB
/
WishartDistribution.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
// 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.Multivariate
{
using System;
using Accord.Math;
using Accord.Math.Decompositions;
using Accord.Statistics.Distributions.Univariate;
using Accord.Compat;
/// <summary>
/// Wishart Distribution.
/// </summary>
///
/// <remarks>
/// <para>
/// The Wishart distribution is a generalization to multiple dimensions of
/// the <see cref="ChiSquareDistribution">Chi-Squared distribution, or, in
/// the case of non-integer <see cref="DegreesOfFreedom"/>degrees of
/// freedom</see>, of the <see cref="GammaDistribution">Gamma distribution
/// </see>.</para>
///
/// <para>
/// References:
/// <list type="bullet">
/// <item><description><a href="http://en.wikipedia.org/wiki/Wishart_distribution">
/// Wikipedia, The Free Encyclopedia. Wishart distribution.
/// Available from: http://en.wikipedia.org/wiki/Wishart_distribution </a></description></item>
/// </list></para>
/// </remarks>
///
/// <example>
/// <code>
/// // Create a Wishart distribution with the parameters:
/// WishartDistribution wishart = new WishartDistribution(
///
/// // Degrees of freedom
/// degreesOfFreedom: 7,
///
/// // Scale parameter
/// scale: new double[,]
/// {
/// { 4, 1, 1 },
/// { 1, 2, 2 }, // (must be symmetric and positive definite)
/// { 1, 2, 6 },
/// }
/// );
///
/// // Common measures
/// double[] var = wishart.Variance; // { 224, 56, 504 }
/// double[,] cov = wishart.Covariance; // see below
/// double[,] meanm = wishart.MeanMatrix; // see below
///
/// // 224 63 175 28 7 7
/// // cov = 63 56 112 mean = 7 14 14
/// // 175 112 504 7 14 42
///
/// // (the above matrix representations have been transcribed to text using)
/// string scov = cov.ToString(DefaultMatrixFormatProvider.InvariantCulture);
/// string smean = meanm.ToString(DefaultMatrixFormatProvider.InvariantCulture);
///
/// // For compatibility reasons, .Mean stores a flattened mean matrix
/// double[] mean = wishart.Mean; // { 28, 7, 7, 7, 14, 14, 7, 14, 42 }
///
///
/// // Probability density functions
/// double pdf = wishart.ProbabilityDensityFunction(new double[,]
/// {
/// { 8, 3, 1 },
/// { 3, 7, 1 }, // 0.000000011082455043473361
/// { 1, 1, 8 },
/// });
///
/// double lpdf = wishart.LogProbabilityDensityFunction(new double[,]
/// {
/// { 8, 3, 1 },
/// { 3, 7, 1 }, // -18.317902605850534
/// { 1, 1, 8 },
/// });
/// </code>
/// </example>
///
/// <seealso cref="InverseWishartDistribution"/>
///
[Serializable]
public class WishartDistribution : MatrixContinuousDistribution
{
int size;
int n;
double[,] scaleMatrix;
double constant;
double lnconstant;
double power;
CholeskyDecomposition chol;
double[,] mean;
double[] variance;
double[,] covariance;
/// <summary>
/// Creates a new Wishart distribution.
/// </summary>
///
/// <param name="dimension">The number of rows in the covariance matrices.</param>
/// <param name="degreesOfFreedom">The degrees of freedom n.</param>
///
public WishartDistribution(int dimension, int degreesOfFreedom)
: this(degreesOfFreedom, Matrix.Identity(dimension))
{
}
/// <summary>
/// Creates a new Wishart distribution.
/// </summary>
///
/// <param name="degreesOfFreedom">The degrees of freedom <c>n</c>.</param>
/// <param name="scale">The positive-definite matrix scale matrix <c>V</c>.</param>
///
public WishartDistribution(int degreesOfFreedom, double[,] scale)
: base(scale.Rows(), scale.Columns())
{
if (scale.GetLength(0) != scale.GetLength(1))
throw new DimensionMismatchException("scale", "Matrix must be square.");
this.scaleMatrix = scale;
this.n = degreesOfFreedom;
this.size = scale.GetLength(0);
if (degreesOfFreedom <= size - 1)
throw new ArgumentOutOfRangeException("degreesOfFreedom", "Degrees of freedom must be greater "
+ "than or equal to the number of rows in the scale matrix.");
this.chol = new CholeskyDecomposition(scale);
if (!chol.IsPositiveDefinite)
throw new NonPositiveDefiniteMatrixException("scale");
//if (!chol.Symmetric)
// throw new NonSymmetricMatrixException("scale");
double a = Math.Pow(chol.Determinant, n / 2.0);
double b = Math.Pow(2, (n * size) / 2.0);
double c = Gamma.Multivariate(n / 2.0, size);
this.constant = 1.0 / (a * b * c);
this.lnconstant = Math.Log(constant);
this.power = (n - size - 1) / 2.0;
}
/// <summary>
/// Gets the degrees of freedom for this Wishart distribution.
/// </summary>
///
public double DegreesOfFreedom
{
get { return n; }
}
/// <summary>
/// Gets the mean for this distribution.
/// </summary>
///
/// <value>A vector containing the mean values for the distribution.</value>
///
public override double[,] Mean
{
get
{
if (mean == null)
mean = n.Multiply(scaleMatrix);
return mean;
}
}
/// <summary>
/// Gets the variance for this distribution.
/// </summary>
///
/// <value>A vector containing the variance values for the distribution.</value>
///
public override double[] Variance
{
get
{
if (variance == null)
{
variance = new double[size];
for (int i = 0; i < size; i++)
{
double vii = scaleMatrix[i, i];
variance[i] = 2 * n * (vii * vii);
}
}
return variance;
}
}
/// <summary>
/// Gets the variance-covariance matrix for this distribution.
/// </summary>
///
/// <value>A matrix containing the covariance values for the distribution.</value>
///
public override double[,] Covariance
{
get
{
if (covariance == null)
{
covariance = new double[size, size];
for (int i = 0; i < size; i++)
{
double vii = scaleMatrix[i, i];
for (int j = 0; j < size; j++)
{
double vij = scaleMatrix[i, j];
double vjj = scaleMatrix[j, j];
covariance[i, j] = n * (vij * vij + vii * vjj);
}
}
}
return covariance;
}
}
/// <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.
/// For a matrix distribution, such as the Wishart's, this
/// should be a positive-definite matrix or a matrix written
/// in flat vector form.
/// </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 det = x.Determinant();
double[,] Vx = chol.Solve(x);
double z = -0.5 * Vx.Trace();
double a = Math.Pow(det, power);
double b = Math.Exp(z);
return constant * 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.
/// For a matrix distribution, such as the Wishart's, this
/// should be a positive-definite matrix or a matrix written
/// in flat vector form.
/// </param>
///
/// <returns>
/// The logarithm of the probability of <c>x</c>
/// occurring in the current distribution.
/// </returns>
///
protected internal override double InnerLogProbabilityDensityFunction(double[,] x)
{
double det = x.Determinant();
double[,] Vx = chol.Solve(x);
double z = -0.5 * Vx.Trace();
double a = power * Math.Log(det);
return lnconstant + a + z;
}
/// <summary>
/// Generates a random vector of observations from the current distribution.
/// </summary>
/// <param name="samples">The number of samples to generate.</param>
/// <param name="result">The location where to store the samples.</param>
/// <param name="source">The random number generator to use as a source of randomness.
/// Default is to use <see cref="Accord.Math.Random.Generator.Random" />.</param>
/// <returns>A random vector of observations drawn from this distribution.</returns>
public override double[][,] Generate(int samples, double[][,] result, Random source)
{
return Random(samples, n, scaleMatrix, result, source);
}
/// <summary>
/// Unsupported.
/// </summary>
///
protected internal override double InnerDistributionFunction(double[,] x)
{
throw new NotSupportedException();
}
/// <summary>
/// Fits the underlying distribution to a given set of observations.
/// </summary>
///
/// <param name="observations">The array of observations to fit the model against. The array
/// elements can be either of type double (for univariate data) or
/// type double[] (for multivariate data).</param>
/// <param name="weights">The weight vector containing the weight for each of the samples.</param>
/// <param name="options">Optional arguments which may be used during fitting, such
/// as regularization constants and additional parameters.</param>
///
public override void Fit(double[][,] observations, double[] weights, Fitting.IFittingOptions options)
{
throw new NotSupportedException();
}
/// <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 WishartDistribution(n, scaleMatrix);
}
/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
///
/// <param name="format">The format.</param>
/// <param name="formatProvider">The format provider.</param>
///
/// <returns>
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
///
public override string ToString(string format, IFormatProvider formatProvider)
{
return String.Format(formatProvider, "Wishart(X)");
}
/// <summary>
/// Generates a random vector of observations from the current distribution.
/// </summary>
/// <param name="degreesOfFreedom">The degrees of freedom <c>n</c>.</param>
/// <param name="scale">The positive-definite matrix scale matrix <c>V</c>.</param>
/// <returns>A random vector of observations drawn from this distribution.</returns>
public static double[,] Random(int degreesOfFreedom, double[,] scale)
{
return Random(1, degreesOfFreedom, scale)[0];
}
/// <summary>
/// Generates a random vector of observations from the current distribution.
/// </summary>
/// <param name="degreesOfFreedom">The degrees of freedom <c>n</c>.</param>
/// <param name="scale">The positive-definite matrix scale matrix <c>V</c>.</param>
/// <param name="source">The random number generator to use as a source of randomness.
/// Default is to use <see cref="Accord.Math.Random.Generator.Random" />.</param>
/// <returns>A random vector of observations drawn from this distribution.</returns>
public static double[,] Random(int degreesOfFreedom, double[,] scale, Random source)
{
return Random(1, degreesOfFreedom, scale, source)[0];
}
/// <summary>
/// Generates a random vector of observations from the current distribution.
/// </summary>
/// <param name="samples">The number of samples to generate.</param>
/// <param name="degreesOfFreedom">The degrees of freedom <c>n</c>.</param>
/// <param name="scale">The positive-definite matrix scale matrix <c>V</c>.</param>
/// <returns>A random vector of observations drawn from this distribution.</returns>
public static double[][,] Random(int samples, int degreesOfFreedom, double[,] scale)
{
return Random(samples, degreesOfFreedom, scale, Accord.Math.Random.Generator.Random);
}
/// <summary>
/// Generates a random vector of observations from the current distribution.
/// </summary>
/// <param name="samples">The number of samples to generate.</param>
/// <param name="degreesOfFreedom">The degrees of freedom <c>n</c>.</param>
/// <param name="scale">The positive-definite matrix scale matrix <c>V</c>.</param>
/// <param name="source">The random number generator to use as a source of randomness.
/// Default is to use <see cref="Accord.Math.Random.Generator.Random" />.</param>
/// <returns>A random vector of observations drawn from this distribution.</returns>
public static double[][,] Random(int samples, int degreesOfFreedom, double[,] scale, Random source)
{
int np = scale.GetLength(0);
double[][,] result = new double[samples].Apply(x => new double[np, np]);
return Random(samples, degreesOfFreedom, scale, result, source);
}
/// <summary>
/// Generates a random vector of observations from the current distribution.
/// </summary>
/// <param name="samples">The number of samples to generate.</param>
/// <param name="result">The location where to store the samples.</param>
/// <param name="degreesOfFreedom">The degrees of freedom <c>n</c>.</param>
/// <param name="scale">The positive-definite matrix scale matrix <c>V</c>.</param>
/// <param name="source">The random number generator to use as a source of randomness.
/// Default is to use <see cref="Accord.Math.Random.Generator.Random" />.</param>
/// <returns>A random vector of observations drawn from this distribution.</returns>
public static double[][,] Random(int samples, int degreesOfFreedom, double[,] scale, double[][,] result, Random source)
{
int np = scale.GetLength(0);
var chol = new CholeskyDecomposition(scale);
var d = new double[np * (np + 1) / 2];
for (int j = 0, l = 0; j < np; j++)
for (int k = 0; k <= j; k++, l++)
d[l] = chol.LeftTriangularFactor[j, k];
double[] r = new double[(np * (np + 1)) / 2];
for (int i = 0; i < samples; i++)
{
wshrt(d, degreesOfFreedom, np, source, r);
double[,] ret = result[i];
for (int j = 0, l = 0; j < np; j++)
for (int k = 0; k <= j; k++, l++)
ret[j, k] = ret[k, j] = r[l];
}
return result;
}
/******************************************************************************/
/*
Purpose:
RNORM returns two independent standard random normal deviates.
Discussion:
This routine sets U1 and U2 to two independent standardized
random normal deviates. This is a version of the
method given in Knuth.
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
16 April 2014
Author:
Original FORTRAN77 version by William Smith, Ronald Hocking.
This C version by John Burkardt.
Reference:
Donald Knuth,
The Art of Computer Programming,
Volume 2, Seminumerical Algorithms,
Third Edition,
Addison Wesley, 1997,
ISBN: 0201896842,
LC: QA76.6.K64.
Parameters:
Input/output, int *SEED, a seed for the random
number generator.
Output, double *U1, *U2, two standard random normal deviates.
*/
static void rnorm(Random random, out double u1, out double u2)
{
for (; ; )
{
double x = random.NextDouble();
double y = random.NextDouble();
x = 2.0 * x - 1.0;
y = 2.0 * y - 1.0;
double s = x * x + y * y;
if (s <= 1.0)
{
s = Math.Sqrt(-2.0 * Math.Log(s) / s);
u1 = x * s;
u2 = y * s;
break;
}
}
return;
}
/******************************************************************************/
/*
Purpose:
WSHRT returns a random Wishart variate.
Discussion:
This routine is a Wishart variate generator.
On output, SA is an upper-triangular matrix of size NP * NP,
written in linear form, column ordered, whose elements have a
Wishart(N, SIGMA) distribution.
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
16 April 2014
Author:
Original FORTRAN77 version by William Smith, Ronald Hocking.
This C version by John Burkardt.
Reference:
William Smith, Ronald Hocking,
Algorithm AS 53, Wishart Variate Generator,
Applied Statistics,
Volume 21, Number 3, pages 341-345, 1972.
Parameters:
Input, double D[NP*(NP+1)/2], the upper triangular array that
represents the Cholesky factor of the correlation matrix SIGMA.
D is stored in column-major form.
Input, int N, the number of degrees of freedom.
1 <= N <= NP.
Input, int NP, the size of variables.
Input/output, int *SEED, a seed for the random
number generator.
Output, double WSHART[NP*(NP+1)/2], a sample from the
Wishart distribution.
*/
static void wshrt(double[] d, int n, int np, Random seed, double[] sa)
{
int k = 0;
int nnp = (np * (np + 1)) / 2;
/*
Load SB with independent normal (0, 1) variates.
*/
var sb = new double[nnp];
while (k < nnp)
{
double u1 = 0;
double u2 = 0;
rnorm(seed, out u1, out u2);
sb[k] = u1;
k = k + 1;
if (k < nnp)
{
sb[k] = u2;
k = k + 1;
}
}
/*
Load diagonal elements with square root of chi-square variates.
*/
int ns = 0;
for (int i = 1; i <= np; i++)
{
double df = (double)(np - i + 1);
ns = ns + i;
double u1 = 2.0 / (9.0 * df);
double u2 = 1.0 - u1;
u1 = Math.Sqrt(u1);
/*
Wilson-Hilferty formula for approximating chi-square variates:
The original code did not take the absolute value!
*/
sb[ns - 1] = Math.Sqrt(df * Math.Abs(Math.Pow(u2 + sb[ns - 1] * u1, 3)));
}
double rn = (double)(n);
int nr = 1;
for (int i = 1; i <= np; i++)
{
nr = nr + i - 1;
for (int j = i; j <= np; j++)
{
int ip = nr;
int nq = (j * (j - 1)) / 2 + i - 1;
double c = 0.0;
for (k = i; k <= j; k++)
{
ip = ip + k - 1;
nq = nq + 1;
c = c + sb[ip - 1] * d[nq - 1];
}
sa[ip - 1] = c;
}
}
for (int i = 1; i <= np; i++)
{
int ii = np - i + 1;
int nq = nnp - np;
for (int j = 1; j <= i; j++)
{
int ip = (ii * (ii - 1)) / 2;
double c = 0.0;
for (k = i; k <= np; k++)
{
ip = ip + 1;
nq = nq + 1;
c = c + sa[ip - 1] * sa[nq - 1];
}
sa[nq - 1] = c / rn;
nq = nq - 2 * np + i + j - 1;
}
}
}
}
}