lixiaojun
2022-07-27 96887ec041ba8ddb170c75c1492fc210735ecb37
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
// <copyright file="Multinomial.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
 
using System;
using System.Collections.Generic;
using System.Linq;
using IStation.Numerics.LinearAlgebra;
using IStation.Numerics.LinearAlgebra.Double;
using IStation.Numerics.Random;
using IStation.Numerics.Statistics;
 
namespace IStation.Numerics.Distributions
{
    /// <summary>
    /// Multivariate Multinomial distribution. For details about this distribution, see
    /// <a href="http://en.wikipedia.org/wiki/Multinomial_distribution">Wikipedia - Multinomial distribution</a>.
    /// </summary>
    /// <remarks>
    /// The distribution is parameterized by a vector of ratios: in other words, the parameter
    /// does not have to be normalized and sum to 1. The reason is that some vectors can't be exactly normalized
    /// to sum to 1 in floating point representation.
    /// </remarks>
    public class Multinomial : IDistribution
    {
        System.Random _random;
 
        /// <summary>
        /// Stores the normalized multinomial probabilities.
        /// </summary>
        readonly double[] _p;
 
        /// <summary>
        /// The number of trials.
        /// </summary>
        readonly int _trials;
 
        /// <summary>
        /// Initializes a new instance of the Multinomial class.
        /// </summary>
        /// <param name="p">An array of nonnegative ratios: this array does not need to be normalized
        /// as this is often impossible using floating point arithmetic.</param>
        /// <param name="n">The number of trials.</param>
        /// <exception cref="ArgumentOutOfRangeException">If any of the probabilities are negative or do not sum to one.</exception>
        /// <exception cref="ArgumentOutOfRangeException">If <paramref name="n"/> is negative.</exception>
        public Multinomial(double[] p, int n)
        {
            if (Control.CheckDistributionParameters && !IsValidParameterSet(p, n))
            {
                throw new ArgumentException("Invalid parametrization for the distribution.");
            }
 
            _random = SystemRandomSource.Default;
            _p = (double[])p.Clone();
            _trials = n;
        }
 
        /// <summary>
        /// Initializes a new instance of the Multinomial class.
        /// </summary>
        /// <param name="p">An array of nonnegative ratios: this array does not need to be normalized
        /// as this is often impossible using floating point arithmetic.</param>
        /// <param name="n">The number of trials.</param>
        /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
        /// <exception cref="ArgumentOutOfRangeException">If any of the probabilities are negative or do not sum to one.</exception>
        /// <exception cref="ArgumentOutOfRangeException">If <paramref name="n"/> is negative.</exception>
        public Multinomial(double[] p, int n, System.Random randomSource)
        {
            if (Control.CheckDistributionParameters && !IsValidParameterSet(p, n))
            {
                throw new ArgumentException("Invalid parametrization for the distribution.");
            }
 
            _random = randomSource ?? SystemRandomSource.Default;
            _p = (double[])p.Clone();
            _trials = n;
        }
 
        /// <summary>
        /// Initializes a new instance of the Multinomial class from histogram <paramref name="h"/>. The distribution will
        /// not be automatically updated when the histogram changes.
        /// </summary>
        /// <param name="h">Histogram instance</param>
        /// <param name="n">The number of trials.</param>
        /// <exception cref="ArgumentOutOfRangeException">If any of the probabilities are negative or do not sum to one.</exception>
        /// <exception cref="ArgumentOutOfRangeException">If <paramref name="n"/> is negative.</exception>
        public Multinomial(Histogram h, int n)
        {
            if (h == null)
            {
                throw new ArgumentNullException(nameof(h));
            }
 
            // The probability distribution vector.
            var p = new double[h.BucketCount];
 
            // Fill in the distribution vector.
            for (var i = 0; i < h.BucketCount; i++)
            {
                p[i] = h[i].Count;
            }
 
            if (Control.CheckDistributionParameters && !IsValidParameterSet(p, n))
            {
                throw new ArgumentException("Invalid parametrization for the distribution.");
            }
 
            _p = (double[])p.Clone();
            _trials = n;
            RandomSource = SystemRandomSource.Default;
        }
 
        /// <summary>
        /// A string representation of the distribution.
        /// </summary>
        /// <returns>a string representation of the distribution.</returns>
        public override string ToString()
        {
            return $"Multinomial(Dimension = {_p.Length}, Number of Trails = {_trials})";
        }
 
        /// <summary>
        /// Tests whether the provided values are valid parameters for this distribution.
        /// </summary>
        /// <param name="p">An array of nonnegative ratios: this array does not need to be normalized
        /// as this is often impossible using floating point arithmetic.</param>
        /// <param name="n">The number of trials.</param>
        /// <returns>If any of the probabilities are negative returns <c>false</c>,
        /// if the sum of parameters is 0.0, or if the number of trials is negative; otherwise <c>true</c>.</returns>
        public static bool IsValidParameterSet(IEnumerable<double> p, int n)
        {
            var sum = 0.0;
            foreach (var t in p)
            {
                if (t < 0.0 || double.IsNaN(t))
                {
                    return false;
                }
 
                sum += t;
            }
 
            if (sum == 0.0)
            {
                return false;
            }
 
            return n >= 0;
        }
 
        /// <summary>
        /// Gets the proportion of ratios.
        /// </summary>
        public double[] P => (double[])_p.Clone();
 
        /// <summary>
        /// Gets the number of trials.
        /// </summary>
        public int N => _trials;
 
        /// <summary>
        /// Gets or sets the random number generator which is used to draw random samples.
        /// </summary>
        public System.Random RandomSource
        {
            get => _random;
            set => _random = value ?? SystemRandomSource.Default;
        }
 
        /// <summary>
        /// Gets the mean of the distribution.
        /// </summary>
        public Vector<double> Mean => _trials*(DenseVector)P;
 
        /// <summary>
        /// Gets the variance of the distribution.
        /// </summary>
        public Vector<double> Variance
        {
            get
            {
                // Do not use _p, because operations below will modify _p array. Use P or _p.Clone().
                var res = (DenseVector)P;
                for (var i = 0; i < res.Count; i++)
                {
                    res[i] *= _trials*(1 - res[i]);
                }
 
                return res;
            }
        }
 
        /// <summary>
        /// Gets the skewness of the distribution.
        /// </summary>
        public Vector<double> Skewness
        {
            get
            {
                // Do not use _p, because operations below will modify _p array. Use P or _p.Clone().
                var res = (DenseVector)P;
                for (var i = 0; i < res.Count; i++)
                {
                    res[i] = (1.0 - (2.0*res[i]))/Math.Sqrt(_trials*(1.0 - res[i])*res[i]);
                }
 
                return res;
            }
        }
 
        /// <summary>
        /// Computes values of the probability mass function.
        /// </summary>
        /// <param name="x">Non-negative integers x1, ..., xk</param>
        /// <returns>The probability mass at location <paramref name="x"/>.</returns>
        /// <exception cref="ArgumentNullException">When <paramref name="x"/> is null.</exception>
        /// <exception cref="ArgumentException">When length of <paramref name="x"/> is not equal to event probabilities count.</exception>
        public double Probability(int[] x)
        {
            if (null == x)
            {
                throw new ArgumentNullException(nameof(x));
            }
 
            if (x.Length != _p.Length)
            {
                throw new ArgumentException("All vectors must have the same dimensionality.", nameof(x));
            }
 
            if (x.Sum() == _trials)
            {
                var coef = SpecialFunctions.Multinomial(_trials, x);
                var num = 1.0;
                for (var i = 0; i < x.Length; i++)
                {
                    num *= Math.Pow(_p[i], x[i]);
                }
 
                return coef*num;
            }
 
            return 0.0;
        }
 
        /// <summary>
        /// Computes values of the log probability mass function.
        /// </summary>
        /// <param name="x">Non-negative integers x1, ..., xk</param>
        /// <returns>The log probability mass at location <paramref name="x"/>.</returns>
        /// <exception cref="ArgumentNullException">When <paramref name="x"/> is null.</exception>
        /// <exception cref="ArgumentException">When length of <paramref name="x"/> is not equal to event probabilities count.</exception>
        public double ProbabilityLn(int[] x)
        {
            if (null == x)
            {
                throw new ArgumentNullException(nameof(x));
            }
 
            if (x.Length != _p.Length)
            {
                throw new ArgumentException("All vectors must have the same dimensionality.", nameof(x));
            }
 
            if (x.Sum() == _trials)
            {
                var coef = Math.Log(SpecialFunctions.Multinomial(_trials, x));
                var num = x.Select((t, i) => t*Math.Log(_p[i])).Sum();
                return coef + num;
            }
 
            return 0.0;
        }
 
        /// <summary>
        /// Samples one multinomial distributed random variable.
        /// </summary>
        /// <returns>the counts for each of the different possible values.</returns>
        public int[] Sample()
        {
            return Sample(_random, _p, _trials);
        }
 
        /// <summary>
        /// Samples a sequence multinomially distributed random variables.
        /// </summary>
        /// <returns>a sequence of counts for each of the different possible values.</returns>
        public IEnumerable<int[]> Samples()
        {
            while (true)
            {
                yield return Sample(_random, _p, _trials);
            }
        }
 
        /// <summary>
        /// Samples one multinomial distributed random variable.
        /// </summary>
        /// <param name="rnd">The random number generator to use.</param>
        /// <param name="p">An array of nonnegative ratios: this array does not need to be normalized
        /// as this is often impossible using floating point arithmetic.</param>
        /// <param name="n">The number of trials.</param>
        /// <returns>the counts for each of the different possible values.</returns>
        public static int[] Sample(System.Random rnd, double[] p, int n)
        {
            if (Control.CheckDistributionParameters && !IsValidParameterSet(p, n))
            {
                throw new ArgumentException("Invalid parametrization for the distribution.");
            }
 
            // The cumulative density of p.
            var cp = Categorical.ProbabilityMassToCumulativeDistribution(p);
 
            // The variable that stores the counts.
            var ret = new int[p.Length];
 
            for (var i = 0; i < n; i++)
            {
                ret[Categorical.SampleUnchecked(rnd, cp)]++;
            }
 
            return ret;
        }
 
        /// <summary>
        /// Samples a multinomially distributed random variable.
        /// </summary>
        /// <param name="rnd">The random number generator to use.</param>
        /// <param name="p">An array of nonnegative ratios: this array does not need to be normalized
        /// as this is often impossible using floating point arithmetic.</param>
        /// <param name="n">The number of variables needed.</param>
        /// <returns>a sequence of counts for each of the different possible values.</returns>
        public static IEnumerable<int[]> Samples(System.Random rnd, double[] p, int n)
        {
            if (Control.CheckDistributionParameters && !IsValidParameterSet(p, n))
            {
                throw new ArgumentException("Invalid parametrization for the distribution.");
            }
 
            // The cumulative density of p.
            var cp = Categorical.ProbabilityMassToCumulativeDistribution(p);
 
            while (true)
            {
                // The variable that stores the counts.
                var ret = new int[p.Length];
 
                for (var i = 0; i < n; i++)
                {
                    ret[Categorical.SampleUnchecked(rnd, cp)]++;
                }
 
                yield return ret;
            }
        }
    }
}