forked from andrewkirillov/AForge.NET
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathStatistics.cs
358 lines (336 loc) · 12 KB
/
Statistics.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
// AForge Math Library
// AForge.NET framework
// http://www.aforgenet.com/framework/
//
// Copyright © AForge.NET, 2005-2011
// contacts@aforgenet.com
//
namespace AForge.Math
{
using System;
using AForge;
/// <summary>
/// Set of statistics functions.
/// </summary>
///
/// <remarks>The class represents collection of simple functions used
/// in statistics.</remarks>
///
public static class Statistics
{
/// <summary>
/// Calculate mean value.
/// </summary>
///
/// <param name="values">Histogram array.</param>
///
/// <returns>Returns mean value.</returns>
///
/// <remarks><para>The input array is treated as histogram, i.e. its
/// indexes are treated as values of stochastic function, but
/// array values are treated as "probabilities" (total amount of
/// hits).</para>
///
/// <para>Sample usage:</para>
/// <code>
/// // create histogram array
/// int[] histogram = new int[] { 1, 1, 2, 3, 6, 8, 11, 12, 7, 3 };
/// // calculate mean value
/// double mean = Statistics.Mean( histogram );
/// // output it (5.759)
/// Console.WriteLine( "mean = " + mean.ToString( "F3" ) );
/// </code>
/// </remarks>
///
public static double Mean( int[] values )
{
int hits;
double total = 0;
double mean = 0;
// for all values
for ( int i = 0, n = values.Length; i < n; i++ )
{
hits = values[i];
// accumulate mean
mean += (double) i * hits;
// accumalate total
total += hits;
}
return ( total == 0 ) ? 0 : mean / total;
}
/// <summary>
/// Calculate standard deviation.
/// </summary>
///
/// <param name="values">Histogram array.</param>
///
/// <returns>Returns value of standard deviation.</returns>
///
/// <remarks><para>The input array is treated as histogram, i.e. its
/// indexes are treated as values of stochastic function, but
/// array values are treated as "probabilities" (total amount of
/// hits).</para>
///
/// <para>Sample usage:</para>
/// <code>
/// // create histogram array
/// int[] histogram = new int[] { 1, 1, 2, 3, 6, 8, 11, 12, 7, 3 };
/// // calculate standard deviation value
/// double stdDev = Statistics.StdDev( histogram );
/// // output it (1.999)
/// Console.WriteLine( "std.dev. = " + stdDev.ToString( "F3" ) );
/// </code>
/// </remarks>
///
public static double StdDev( int[] values )
{
return StdDev( values, Mean( values ) );
}
/// <summary>
/// Calculate standard deviation.
/// </summary>
///
/// <param name="values">Histogram array.</param>
/// <param name="mean">Mean value of the histogram.</param>
///
/// <returns>Returns value of standard deviation.</returns>
///
/// <remarks><para>The input array is treated as histogram, i.e. its
/// indexes are treated as values of stochastic function, but
/// array values are treated as "probabilities" (total amount of
/// hits).</para>
///
/// <para>The method is an equevalent to the <see cref="StdDev(int[])"/> method,
/// but it relieas on the passed mean value, which is previously calculated
/// using <see cref="Mean"/> method.</para>
/// </remarks>
///
public static double StdDev( int[] values, double mean )
{
double stddev = 0;
double diff;
int hits;
int total = 0;
// for all values
for ( int i = 0, n = values.Length; i < n; i++ )
{
hits = values[i];
diff = (double) i - mean;
// accumulate std.dev.
stddev += diff * diff * hits;
// accumalate total
total += hits;
}
return ( total == 0 ) ? 0 : Math.Sqrt( stddev / total );
}
/// <summary>
/// Calculate median value.
/// </summary>
///
/// <param name="values">Histogram array.</param>
///
/// <returns>Returns value of median.</returns>
///
/// <remarks>
/// <para>The input array is treated as histogram, i.e. its
/// indexes are treated as values of stochastic function, but
/// array values are treated as "probabilities" (total amount of
/// hits).</para>
///
/// <para><note>The median value is calculated accumulating histogram's
/// values starting from the <b>left</b> point until the sum reaches 50% of
/// histogram's sum.</note></para>
///
/// <para>Sample usage:</para>
/// <code>
/// // create histogram array
/// int[] histogram = new int[] { 1, 1, 2, 3, 6, 8, 11, 12, 7, 3 };
/// // calculate median value
/// int median = Statistics.Median( histogram );
/// // output it (6)
/// Console.WriteLine( "median = " + median );
/// </code>
/// </remarks>
///
public static int Median( int[] values )
{
int total = 0, n = values.Length;
// for all values
for ( int i = 0; i < n; i++ )
{
// accumalate total
total += values[i];
}
int halfTotal = total / 2;
int median = 0, v = 0;
// find median value
for ( ; median < n; median++ )
{
v += values[median];
if ( v >= halfTotal )
break;
}
return median;
}
/// <summary>
/// Get range around median containing specified percentage of values.
/// </summary>
///
/// <param name="values">Histogram array.</param>
/// <param name="percent">Values percentage around median.</param>
///
/// <returns>Returns the range which containes specifies percentage
/// of values.</returns>
///
/// <remarks>
/// <para>The input array is treated as histogram, i.e. its
/// indexes are treated as values of stochastic function, but
/// array values are treated as "probabilities" (total amount of
/// hits).</para>
///
/// <para>The method calculates range of stochastic variable, which summary probability
/// comprises the specified percentage of histogram's hits.</para>
///
/// <para>Sample usage:</para>
/// <code>
/// // create histogram array
/// int[] histogram = new int[] { 1, 1, 2, 3, 6, 8, 11, 12, 7, 3 };
/// // get 75% range around median
/// IntRange range = Statistics.GetRange( histogram, 0.75 );
/// // output it ([4, 8])
/// Console.WriteLine( "range = [" + range.Min + ", " + range.Max + "]" );
/// </code>
/// </remarks>
///
public static IntRange GetRange( int[] values, double percent )
{
int total = 0, n = values.Length;
// for all values
for ( int i = 0; i < n; i++ )
{
// accumalate total
total += values[i];
}
int min, max, hits;
int h = (int) ( total * ( percent + ( 1 - percent ) / 2 ) );
// get range min value
for ( min = 0, hits = total; min < n; min++ )
{
hits -= values[min];
if ( hits < h )
break;
}
// get range max value
for ( max = n - 1, hits = total; max >= 0; max-- )
{
hits -= values[max];
if ( hits < h )
break;
}
return new IntRange( min, max );
}
/// <summary>
/// Calculate entropy value.
/// </summary>
///
/// <param name="values">Histogram array.</param>
///
/// <returns>Returns entropy value of the specified histagram array.</returns>
///
/// <remarks><para>The input array is treated as histogram, i.e. its
/// indexes are treated as values of stochastic function, but
/// array values are treated as "probabilities" (total amount of
/// hits).</para>
///
/// <para>Sample usage:</para>
/// <code>
/// // create histogram array with 2 values of equal probabilities
/// int[] histogram1 = new int[2] { 3, 3 };
/// // calculate entropy
/// double entropy1 = Statistics.Entropy( histogram1 );
/// // output it (1.000)
/// Console.WriteLine( "entropy1 = " + entropy1.ToString( "F3" ) );
///
/// // create histogram array with 4 values of equal probabilities
/// int[] histogram2 = new int[4] { 1, 1, 1, 1 };
/// // calculate entropy
/// double entropy2 = Statistics.Entropy( histogram2 );
/// // output it (2.000)
/// Console.WriteLine( "entropy2 = " + entropy2.ToString( "F3" ) );
///
/// // create histogram array with 4 values of different probabilities
/// int[] histogram3 = new int[4] { 1, 2, 3, 4 };
/// // calculate entropy
/// double entropy3 = Statistics.Entropy( histogram3 );
/// // output it (1.846)
/// Console.WriteLine( "entropy3 = " + entropy3.ToString( "F3" ) );
/// </code>
/// </remarks>
///
public static double Entropy( int[] values )
{
int n = values.Length;
int total = 0;
double entropy = 0;
double p;
// calculate total amount of hits
for ( int i = 0; i < n; i++ )
{
total += values[i];
}
if ( total != 0 )
{
// for all values
for ( int i = 0; i < n; i++ )
{
// get item's probability
p = (double) values[i] / total;
// calculate entropy
if ( p != 0 )
entropy += ( -p * Math.Log( p, 2 ) );
}
}
return entropy;
}
/// <summary>
/// Calculate mode value.
/// </summary>
///
/// <param name="values">Histogram array.</param>
///
/// <returns>Returns mode value of the histogram array.</returns>
///
/// <remarks>
/// <para>The input array is treated as histogram, i.e. its
/// indexes are treated as values of stochastic function, but
/// array values are treated as "probabilities" (total amount of
/// hits).</para>
///
/// <para><note>Returns the minimum mode value if the specified histogram is multimodal.</note></para>
///
/// <para>Sample usage:</para>
/// <code>
/// // create array
/// int[] values = new int[] { 1, 1, 2, 3, 6, 8, 11, 12, 7, 3 };
/// // calculate mode value
/// int mode = Statistics.Mode( values );
/// // output it (7)
/// Console.WriteLine( "mode = " + mode );
/// </code>
/// </remarks>
///
public static int Mode( int[] values )
{
int mode = 0, curMax = 0;
for ( int i = 0, length = values.Length; i < length; i++ )
{
if ( values[i] > curMax )
{
curMax = values[i];
mode = i;
}
}
return mode;
}
}
}