-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
297 lines (244 loc) · 9.78 KB
/
MainWindow.xaml.cs
File metadata and controls
297 lines (244 loc) · 9.78 KB
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
namespace WpfApp8_RandomDistribution
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
this.KeyDown += (sender, args) =>
{
if (args.Key == Key.F5)
{
this.Start();
}
};
this.Start();
}
private static double CalcMedian(List<double> numbers)
{
int numberCount = numbers.Count;
int halfIndex = numbers.Count / 2;
var sortedNumbers = numbers.OrderBy(n => n);
double median;
if ((numberCount % 2) == 0)
{
median = (sortedNumbers.ElementAt(halfIndex)
+ sortedNumbers.ElementAt((halfIndex - 1)))
/ 2.0;
}
else
{
median = sortedNumbers.ElementAt(halfIndex);
}
return median;
}
private static Vector2[] CalculatePoints(
out string text,
double targetProbabilityReverse,
out double thresholdLineX)
{
List<double> listSamples;
(text, listSamples) = GenerateData(targetProbabilityReverse);
var result = new List<Vector2>();
const double scale = 100;
var bucketsCount = Math.Min(targetProbabilityReverse, 50);
double max = listSamples.Max();
double min = listSamples.Min();
double bucketSize = (max - min) / (double)bucketsCount;
thresholdLineX = scale * targetProbabilityReverse / (max - min);
for (var i = 0; i < bucketsCount; i++)
{
var currentRangeFrom = min + i * bucketSize;
var currentRangeTo = min + (i + 1) * bucketSize;
if (i == bucketsCount - 1)
{
currentRangeTo = double.MaxValue;
}
var countInRange = listSamples.Count(v => v >= currentRangeFrom
&& v < currentRangeTo);
var x = scale * (i / (double)(bucketsCount - 1));
var y = scale * countInRange / (double)listSamples.Count;
// normalize values
// TODO: this is not correct but essential to indicate
y *= bucketsCount / 2.0;
result.Add(new Vector2((float)x, (float)y));
}
return result.ToArray();
}
private static Geometry CreateGeometryForData(Vector2[] dataPoints)
{
var maxValue = 100.0; //double.MinValue;
var minValue = 0.0; //double.MaxValue;
foreach (var dataPoint in dataPoints)
{
if (dataPoint.Y > maxValue)
{
maxValue = dataPoint.Y;
}
if (dataPoint.Y < minValue)
{
minValue = dataPoint.Y;
}
}
var normalizationCoefficient = (100.0 / (maxValue - minValue));
var g = new StreamGeometry();
using var c = g.Open();
var isFirst = true;
foreach (var dataPoint in dataPoints)
{
var x = (double)dataPoint.X;
var y = (double)dataPoint.Y;
y -= minValue;
y *= normalizationCoefficient;
var point = new Point(x, y);
if (isFirst)
{
isFirst = false;
c.BeginFigure(point, isFilled: false, isClosed: false);
continue;
}
c.LineTo(point, isStroked: true, isSmoothJoin: false);
}
return g;
}
private static (string sb, List<double> list) GenerateData(double targetProbabilityReverse)
{
const int SamplesCount = 50000;
var random = new Random();
var listSamples = new List<double>();
for (var sampleNumber = 0; sampleNumber < SamplesCount; sampleNumber++)
{
var attempt = 1;
do
{
//var p = targetProbabilityReverse;
// Probability compensation mechanism idea:
// the more attempts were made, the bigger threshold use.
var p = 2 * targetProbabilityReverse - attempt;
var rolledValue = random.NextDouble(); // roll random value in range from 0 to 1 (excluding 1)
if (rolledValue <= 1 / p)
{
listSamples.Add(attempt);
break;
}
attempt++;
}
while (true);
}
listSamples.Sort();
var min = listSamples.Min();
var max = listSamples.Max();
var listAverage = listSamples.Sum() / listSamples.Count;
var listMedian = CalcMedian(listSamples);
//var loosers = listSamples.Where(s => s < 50).Count();
//var winners = listSamples.Where(s => s > 3000).Count();
var sb = new StringBuilder();
sb.AppendLine($"Target probability: 1/{targetProbabilityReverse:0.##} | Samples count: {SamplesCount}");
sb.AppendLine(
string.Format("Random rolls necessary:"
+ Environment.NewLine
+ "- range: [{0:0.##};{1:0.##}]"
+ Environment.NewLine
+ "- average: {2:0.##}"
+ Environment.NewLine
+ "- median: {3:0.##}",
min,
max,
listAverage,
listMedian));
sb.AppendLine("----------------------------------------------------------");
var bucketsCount = 10;
var step = (max - min) / (double)bucketsCount;
for (var i = 0; i < bucketsCount; i++)
{
var currentRangeFrom = min + i * step;
var currentRangeTo = min + (i + 1) * step;
var currentRangeToForCheck = currentRangeTo;
if (i == bucketsCount - 1)
{
currentRangeToForCheck = double.MaxValue;
}
var countInRange = listSamples.Count(v => v >= currentRangeFrom
&& v < currentRangeToForCheck);
var signsCount = (int)Math.Round(120 * (double)countInRange / SamplesCount);
sb.AppendLine(
"["
+ string.Format("{0:0.##}", (int)currentRangeFrom).PadLeft(6)
+ string.Format(";{0:0.##}", (int)currentRangeTo).PadRight(7)
+ "] |"
+ new string('#', signsCount)
+ "|");
}
return (sb.ToString(), listSamples);
}
private void AddCoordinateGrid()
{
var strokeBrush = new SolidColorBrush(Color.FromArgb(0xFF, 0x55, 0x55, 0x55));
var strokeThickness = 0.125;
var scale = 100.0;
for (var v = 0.1; v <= 0.9; v += 0.1)
{
var lineColumn = new Line()
{
X1 = v * scale,
X2 = v * scale,
Y1 = 0,
Y2 = scale,
Stroke = strokeBrush,
StrokeThickness = strokeThickness
};
var lineRow = new Line()
{
Y1 = v * scale,
Y2 = v * scale,
X1 = 0,
X2 = scale,
Stroke = strokeBrush,
StrokeThickness = strokeThickness
};
this.Grid.Children.Add(lineColumn);
this.Grid.Children.Add(lineRow);
}
}
private void Start()
{
// test for 1/Xth probability
var targetProbabilityReverse = 1000;
var dataPoints = CalculatePoints(out var text,
targetProbabilityReverse: targetProbabilityReverse,
out var thresholdLineX);
this.Grid.Children.Clear();
this.TextBlock.Text = text;
this.AddCoordinateGrid();
// add data points line
this.Grid.Children.Add(
new Path()
{
Stroke = new SolidColorBrush(Colors.White),
StrokeThickness = 0.25,
Data = CreateGeometryForData(dataPoints)
});
// add vertical line for the target probability
this.Grid.Children.Add(
new Path()
{
Stroke = new SolidColorBrush(Colors.Red),
StrokeThickness = 0.25,
Data = new LineGeometry(startPoint: new Point(thresholdLineX, 0),
endPoint: new Point(thresholdLineX, 100))
});
}
}
}