-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStats.cs
79 lines (63 loc) · 1.81 KB
/
Stats.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ImageQuantization
{
class Stats
{
// not very clean i know
private static int MAX_WEIGHT_INDEX;
private static double Mean(double[] weights, int count)
{
double sum = 0;
for (int i = 0; i < weights.Length; i++)
{
if (weights[i] == -1)
continue;
sum += weights[i];
}
sum /= count;
return sum;
}
private static double Stddev(double[] weights, int count)
{
double mean = Mean(weights, count);
double sum = 0;
double max = double.MinValue;
double sample;
for (int i = 0; i < weights.Length; i++)
{
if (weights[i] == -1)
continue;
sample = Math.Pow(weights[i] - mean, 2);
if (sample > max)
{
max = sample;
MAX_WEIGHT_INDEX = i;
}
sum += sample;
}
sum /= count;
sum = Math.Sqrt(sum);
return sum;
}
public static int MSDR(double[] weights)
{
int N = weights.Length;
double old_stddev = Stddev(weights, N);
double current_stddev;
double diff;
do
{
weights[MAX_WEIGHT_INDEX] = -1;
N--;
current_stddev = Stddev(weights, N);
diff = old_stddev - current_stddev;
old_stddev = current_stddev;
}
while (diff > 0.0001 && N > 1);
return weights.Length - N;
}
}
}