forked from andrewkirillov/AForge.NET
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathContinuousHistogram.cs
334 lines (306 loc) · 10.6 KB
/
ContinuousHistogram.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
// AForge Math Library
// AForge.NET framework
// http://www.aforgenet.com/framework/
//
// Copyright © AForge.NET, 2007-2011
// contacts@aforgenet.com
//
namespace AForge.Math
{
using System;
/// <summary>
/// Histogram for continuous random values.
/// </summary>
///
/// <remarks><para>The class wraps histogram for continuous stochastic function, which is represented
/// by integer array and range of the function. Values of the integer array are treated
/// as total amount of hits on the corresponding subranges, which are calculated by splitting the
/// specified range into required amount of consequent ranges.</para>
///
/// <para>For example, if the integer array is equal to { 1, 2, 4, 8, 16 } and the range is set
/// to [0, 1], then the histogram consists of next subranges:
/// <list type="bullet">
/// <item>[0.0, 0.2] - 1 hit;</item>
/// <item>[0.2, 0.4] - 2 hits;</item>
/// <item>[0.4, 0.6] - 4 hits;</item>
/// <item>[0.6, 0.8] - 8 hits;</item>
/// <item>[0.8, 1.0] - 16 hits.</item>
/// </list>
/// </para>
///
/// <para>Sample usage:</para>
/// <code>
/// // create histogram
/// ContinuousHistogram histogram = new ContinuousHistogram(
/// new int[] { 0, 0, 8, 4, 2, 4, 7, 1, 0 }, new Range( 0.0f, 1.0f ) );
/// // get mean and standard deviation values
/// Console.WriteLine( "mean = " + histogram.Mean + ", std.dev = " + histogram.StdDev );
/// </code>
/// </remarks>
///
[Serializable]
public class ContinuousHistogram
{
private int[] values;
private Range range;
private float mean;
private float stdDev;
private float median;
private float min;
private float max;
private int total;
/// <summary>
/// Values of the histogram.
/// </summary>
///
public int[] Values
{
get { return values; }
}
/// <summary>
/// Range of random values.
/// </summary>
///
public Range Range
{
get { return range; }
}
/// <summary>
/// Mean value.
/// </summary>
///
/// <remarks><para>The property allows to retrieve mean value of the histogram.</para>
///
/// <para>Sample usage:</para>
/// <code>
/// // create histogram
/// ContinuousHistogram histogram = new ContinuousHistogram(
/// new int[] { 0, 0, 8, 4, 2, 4, 7, 1, 0 }, new Range( 0.0f, 1.0f ) );
/// // get mean value (= 0.505 )
/// Console.WriteLine( "mean = " + histogram.Mean.ToString( "F3" ) );
/// </code>
/// </remarks>
///
public float 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
/// ContinuousHistogram histogram = new ContinuousHistogram(
/// new int[] { 0, 0, 8, 4, 2, 4, 7, 1, 0 }, new Range( 0.0f, 1.0f ) );
/// // get std.dev. value (= 0.215)
/// Console.WriteLine( "std.dev. = " + histogram.StdDev.ToString( "F3" ) );
/// </code>
/// </remarks>
///
public float 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
/// ContinuousHistogram histogram = new ContinuousHistogram(
/// new int[] { 0, 0, 8, 4, 2, 4, 7, 1, 0 }, new Range( 0.0f, 1.0f ) );
/// // get median value (= 0.500)
/// Console.WriteLine( "median = " + histogram.Median.ToString( "F3" ) );
/// </code>
/// </remarks>
///
public float 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
/// ContinuousHistogram histogram = new ContinuousHistogram(
/// new int[] { 0, 0, 8, 4, 2, 4, 7, 1, 0 }, new Range( 0.0f, 1.0f ) );
/// // get min value (= 0.250)
/// Console.WriteLine( "min = " + histogram.Min.ToString( "F3" ) );
/// </code>
/// </remarks>
public float 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
/// ContinuousHistogram histogram = new ContinuousHistogram(
/// new int[] { 0, 0, 8, 4, 2, 4, 7, 1, 0 }, new Range( 0.0f, 1.0f ) );
/// // get max value (= 0.875)
/// Console.WriteLine( "max = " + histogram.Max.ToString( "F3" ) );
/// </code>
/// </remarks>
///
public float Max
{
get { return max; }
}
/// <summary>
/// Initializes a new instance of the <see cref="ContinuousHistogram"/> class.
/// </summary>
///
/// <param name="values">Values of the histogram.</param>
/// <param name="range">Range of random values.</param>
///
/// <remarks>Values of the integer array are treated as total amount of hits on the
/// corresponding subranges, which are calculated by splitting the specified range into
/// required amount of consequent ranges (see <see cref="ContinuousHistogram"/> class
/// description for more information).
/// </remarks>
///
public ContinuousHistogram( int[] values, Range range )
{
this.values = values;
this.range = range;
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
/// ContinuousHistogram histogram = new ContinuousHistogram(
/// new int[] { 0, 0, 8, 4, 2, 4, 7, 1, 0 }, new Range( 0.0f, 1.0f ) );
/// // get 50% range
/// Range range = histogram.GetRange( 0.5f );
/// // show the range ([0.25, 0.75])
/// Console.WriteLine( "50% range = [" + range.Min + ", " + range.Max + "]" );
/// </code>
/// </remarks>
///
public Range GetRange( float percent )
{
int min, max, hits;
int h = (int) ( total * ( percent + ( 1 - percent ) / 2 ) );
int n = values.Length;
int nM1 = n - 1;
// skip left portion
for ( min = 0, hits = total; min < n; min++ )
{
hits -= values[min];
if ( hits < h )
break;
}
// skip right portion
for ( max = nM1, hits = total; max >= 0; max-- )
{
hits -= values[max];
if ( hits < h )
break;
}
// return range between left and right boundaries
return new Range(
( (float) min / nM1 ) * range.Length + range.Min,
( (float) max / nM1 ) * range.Length + range.Min );
}
/// <summary>
/// Update statistical value of the histogram.
/// </summary>
///
/// <remarks>The method recalculates statistical values of the histogram, like mean,
/// standard deviation, etc. The method should be called only in the case if histogram
/// values were retrieved through <see cref="Values"/> property and updated after that.
/// </remarks>
///
public void Update( )
{
int hits;
int i, n = values.Length;
int nM1 = n - 1;
float rangeLength = range.Length;
float rangeMin = range.Min;
max = 0;
min = n;
mean = 0;
stdDev = 0;
total = 0;
double sum = 0;
// calculate mean, min, max
for ( i = 0; i < n; i++ )
{
hits = values[i];
if ( hits != 0 )
{
// max
if ( i > max )
max = i;
// min
if ( i < min )
min = i;
}
// accumulate total value
total += hits;
// accumulate mean value
sum += ( ( (double) i / nM1 ) * rangeLength + rangeMin ) * hits;
}
if ( total != 0 )
{
mean = (float) ( sum / total );
}
min = ( min / nM1 ) * rangeLength + rangeMin;
max = ( max / nM1 ) * rangeLength + rangeMin;
// calculate stadard deviation
sum = 0;
double diff;
for ( i = 0; i < n; i++ )
{
hits = values[i];
diff = ( ( (double) i / nM1 ) * rangeLength + rangeMin ) - mean;
sum += diff * diff * hits;
}
if ( total != 0 )
{
stdDev = (float) Math.Sqrt( sum / total );
}
// calculate median
int m, halfTotal = total / 2;
for ( m = 0, hits = 0; m < n; m++ )
{
hits += values[m];
if ( hits >= halfTotal )
break;
}
median = ( (float) m / nM1 ) * rangeLength + rangeMin;
}
}
}