forked from andrewkirillov/AForge.NET
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathHistogram.cs
260 lines (244 loc) · 8.29 KB
/
Histogram.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
// AForge Math Library
// AForge.NET framework
// http://www.aforgenet.com/framework/
//
// Copyright © AForge.NET, 2005-2011
// contacts@aforgenet.com
//
namespace AForge.Math
{
using System;
/// <summary>
/// Histogram for discrete random values.
/// </summary>
///
/// <remarks><para>The class wraps histogram for discrete stochastic function, which is represented
/// by integer array, where indexes of the array are treated as values of the stochastic function,
/// but array values are treated as "probabilities" (total amount of hits).
/// </para>
///
/// <para>Sample usage:</para>
/// <code>
/// // create histogram
/// Histogram histogram = new Histogram( new int[10] { 0, 0, 1, 3, 6, 8, 11, 0, 0, 0 } );
/// // get mean and standard deviation values
/// Console.WriteLine( "mean = " + histogram.Mean + ", std.dev = " + histogram.StdDev );
/// </code>
/// </remarks>
///
[Serializable]
public class Histogram
{
private int[] values;
private double mean = 0;
private double stdDev = 0;
private int median = 0;
private int min;
private int max;
private long total;
/// <summary>
/// Values of the histogram.
/// </summary>
///
/// <remarks><para>Indexes of this array are treated as values of stochastic function,
/// but array values are treated as "probabilities" (total amount of hits).
/// </para></remarks>
///
public int[] Values
{
get { return values; }
}
/// <summary>
/// Mean value.
/// </summary>
///
/// <remarks><para>The property allows to retrieve mean value of the histogram.</para>
///
/// <para>Sample usage:</para>
/// <code>
/// // create histogram
/// Histogram histogram = new Histogram( new int[10] { 0, 0, 1, 3, 6, 8, 11, 0, 0, 0 } );
/// // get mean value (= 4.862)
/// Console.WriteLine( "mean = " + histogram.Mean.ToString( "F3" ) );
/// </code>
/// </remarks>
///
public double Mean
{
get { return mean; }
}
/// <summary>
/// Standard deviation.
/// </summary>
///
/// <remarks><para>The property allows to retrieve standard deviation value of the histogram.</para>
///
/// <para>Sample usage:</para>
/// <code>
/// // create histogram
/// Histogram histogram = new Histogram( new int[10] { 0, 0, 1, 3, 6, 8, 11, 0, 0, 0 } );
/// // get std.dev. value (= 1.136)
/// Console.WriteLine( "std.dev. = " + histogram.StdDev.ToString( "F3" ) );
/// </code>
/// </remarks>
///
public double StdDev
{
get { return stdDev; }
}
/// <summary>
/// Median value.
/// </summary>
///
/// <remarks><para>The property allows to retrieve median value of the histogram.</para>
///
/// <para>Sample usage:</para>
/// <code>
/// // create histogram
/// Histogram histogram = new Histogram( new int[10] { 0, 0, 1, 3, 6, 8, 11, 0, 0, 0 } );
/// // get median value (= 5)
/// Console.WriteLine( "median = " + histogram.Median );
/// </code>
/// </remarks>
///
public int Median
{
get { return median; }
}
/// <summary>
/// Minimum value.
/// </summary>
///
/// <remarks><para>The property allows to retrieve minimum value of the histogram with non zero
/// hits count.</para>
///
/// <para>Sample usage:</para>
/// <code>
/// // create histogram
/// Histogram histogram = new Histogram( new int[10] { 0, 0, 1, 3, 6, 8, 11, 0, 0, 0 } );
/// // get min value (= 2)
/// Console.WriteLine( "min = " + histogram.Min );
/// </code>
/// </remarks>
///
public int Min
{
get { return min; }
}
/// <summary>
/// Maximum value.
/// </summary>
///
/// <remarks><para>The property allows to retrieve maximum value of the histogram with non zero
/// hits count.</para>
///
/// <para>Sample usage:</para>
/// <code>
/// // create histogram
/// Histogram histogram = new Histogram( new int[10] { 0, 0, 1, 3, 6, 8, 11, 0, 0, 0 } );
/// // get max value (= 6)
/// Console.WriteLine( "max = " + histogram.Max );
/// </code>
/// </remarks>
///
public int Max
{
get { return max; }
}
/// <summary>
/// Total count of values.
/// </summary>
///
/// <remarks><para>The property represents total count of values contributed to the histogram, which is
/// essentially sum of the <see cref="Values"/> array.</para>
///
/// <para>Sample usage:</para>
/// <code>
/// // create histogram
/// Histogram histogram = new Histogram( new int[10] { 0, 0, 1, 3, 6, 8, 11, 0, 0, 0 } );
/// // get total value (= 29)
/// Console.WriteLine( "total = " + histogram.TotalCount );
/// </code>
/// </remarks>
///
public long TotalCount
{
get { return total; }
}
/// <summary>
/// Initializes a new instance of the <see cref="Histogram"/> class.
/// </summary>
///
/// <param name="values">Values of the histogram.</param>
///
/// <remarks><para>Indexes of the input array are treated as values of stochastic function,
/// but array values are treated as "probabilities" (total amount of hits).
/// </para></remarks>
///
public Histogram( int[] values )
{
this.values = values;
Update( );
}
/// <summary>
/// Get range around median containing specified percentage of values.
/// </summary>
///
/// <param name="percent">Values percentage around median.</param>
///
/// <returns>Returns the range which containes specifies percentage of values.</returns>
///
/// <remarks><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
/// Histogram histogram = new Histogram( new int[10] { 0, 0, 1, 3, 6, 8, 11, 0, 0, 0 } );
/// // get 50% range
/// IntRange range = histogram.GetRange( 0.5 );
/// // show the range ([4, 6])
/// Console.WriteLine( "50% range = [" + range.Min + ", " + range.Max + "]" );
/// </code>
/// </remarks>
///
public IntRange GetRange( double percent )
{
return Statistics.GetRange( values, percent );
}
/// <summary>
/// Update statistical value of the histogram.
/// </summary>
///
/// <remarks>The method recalculates statistical values of the histogram, like mean,
/// standard deviation, etc., in the case if histogram's values were changed directly.
/// The method should be called only in the case if histogram's values were retrieved
/// through <see cref="Values"/> property and updated after that.
/// </remarks>
///
public void Update( )
{
int i, n = values.Length;
max = 0;
min = n;
total = 0;
// calculate min and max
for ( i = 0; i < n; i++ )
{
if ( values[i] != 0 )
{
// max
if ( i > max )
max = i;
// min
if ( i < min )
min = i;
total += values[i];
}
}
mean = Statistics.Mean( values );
stdDev = Statistics.StdDev( values, mean );
median = Statistics.Median( values );
}
}
}