This repository was archived by the owner on Oct 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionStats.cs
More file actions
221 lines (193 loc) · 7.33 KB
/
Copy pathCollectionStats.cs
File metadata and controls
221 lines (193 loc) · 7.33 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
using System;
using System.Collections.Generic;
using System.Linq;
using Cysharp.Threading.Tasks;
namespace Argyle.UnclesToolkit
{
public static class CollectionStats
{
private const int minQty = 5;
private const string outOfRangeMessage = "The collection must have at least objects to be properly analyzed.";
private const string noDataMessage = "Collection must contain data to be analyzed.";
/// <summary>
/// Lowest datum in the dataset according to the sorted float keys.
/// </summary>
/// <param name="data">A collection of objects, sorted by some measurement. To be analyzed according to that measurement. </param>
/// <exception cref="IndexOutOfRangeException"></exception>
public static KeyValuePair<float, T> Min<T>(this SortedList<float, T> data)
{
if (data.Count > 0)
{
float key = data.Keys[0];
return new KeyValuePair<float, T>(key, data[key]);
}
else
throw new IndexOutOfRangeException(noDataMessage);
}
/// <summary>
/// Datapoint dividing the 1st quartile from the 2nd in the dataset according to the sorted float keys.
/// </summary>
/// <param name="data">A collection of objects, sorted by some measurement. To be analyzed according to that measurement. </param>
/// <exception cref="IndexOutOfRangeException"></exception>
public static KeyValuePair<float, T> Q1<T>(this SortedList<float, T> data)
{
if (data.Count >= minQty)
{
float key = data.Keys[(data.Count - 1) / 4];
return new KeyValuePair<float, T>(key, data[key]);
}
else
throw new IndexOutOfRangeException(outOfRangeMessage);
}
/// <summary>
/// Middle datum in the dataset according to the sorted float keys.
/// </summary>
/// <param name="data">A collection of objects, sorted by some measurement. To be analyzed according to that measurement. </param>
/// <returns></returns>
public static KeyValuePair<float, T> Med<T>(this SortedList<float, T> data)
{
float key;
if (data.Count >= (minQty + 1) / 2)
key = data.Keys[(data.Count - 1) / 2];
else if (data.Count > 0)
key = data.Keys[0];
else
throw new IndexOutOfRangeException();
return new KeyValuePair<float, T>(key, data[key]);
}
/// <summary>
/// Datapoint dividing the 3rd quartile from the 4th in the dataset according to the sorted float keys.
/// </summary>
/// <param name="data">A collection of objects, sorted by some measurement. To be analyzed according to that measurement. </param>
/// <returns></returns>
public static KeyValuePair<float, T> Q3<T>(this SortedList<float, T> data)
{
float key;
if (data.Count >= minQty)
key = data.Keys[(data.Count - 1) * 3 / 4];
else
throw new IndexOutOfRangeException(outOfRangeMessage);
return new KeyValuePair<float, T>(key, data[key]);
}
/// <summary>
/// Highest datum in the dataset according to the sorted float keys.
/// </summary>
/// <param name="data">A collection of objects, sorted by some measurement. To be analyzed according to that measurement. </param>
/// <returns></returns>
public static KeyValuePair<float, T> Max<T>(this SortedList<float, T> data)
{
if (data.Count > 0)
{
float key = data.Keys[data.Count - 1];
return new KeyValuePair<float, T>(key, data[key]);
}
else
throw new IndexOutOfRangeException(noDataMessage);
}
/// <summary>
/// Inner Quartile Range. The difference between the float keys of the 1st and 3rd quartiles.
/// Used to calculate outliers etc.
/// </summary>
/// <param name="data">A collection of objects, sorted by some measurement. To be analyzed according to that measurement. </param>
/// <param name="things">The objects associated with the given keys</param>
/// <returns></returns>
public static float Iqr<T>(this SortedList<float, T> data, out List<T> things)
{
if (data.Count >= minQty)
{
float q1 = data.Q1().Key;
float q3 = data.Q3().Key;
float iqr = q3 - q1;
things = new List<T>();
foreach (var datum in data)
{
if(datum.Key >= q1 && datum.Key <= q3)
things.Add(datum.Value);
}
return iqr;
}
else
throw new IndexOutOfRangeException(outOfRangeMessage);
}
/// <summary>
/// Inner Quartile Range. The difference between the float keys of the 1st and 3rd quartiles.
/// Used to calculate outliers etc.
/// </summary>
/// <param name="data">A collection of objects, sorted by some measurement. To be analyzed according to that measurement. </param>
/// <returns></returns>
public static float Iqr<T>(this SortedList<float, T> data)
{
if (data.Count >= minQty)
return data.Q3().Key - data.Q1().Key;
else
throw new IndexOutOfRangeException(outOfRangeMessage);
}
/// <summary>
/// Calculates and returns a list of outliers from a give dataset, as organized by the sorted set keys.
/// </summary>
/// <param name="data">A collection of objects, sorted by some measurement. To be analyzed according to that measurement. </param>
/// <param name="multiplier">A multiplier for the average range, beyond which a datum counts as an outlier. </param>
/// <returns></returns>
public static SortedList<float, T> Outliers<T>(this SortedList<float, T> data, float multiplier = 1.5f)
{
if (data.Count >= minQty)
{
float iqr = data.Q3().Key - data.Q1().Key;
float med = data.Med().Key;
SortedList<float, T> outliers = new SortedList<float, T>();
foreach (var datum in data)
if (datum.Key > med + iqr * multiplier || datum.Key < med - iqr * multiplier)
outliers.Add(datum.Key, datum.Value);
return outliers;
}
else
throw new IndexOutOfRangeException(outOfRangeMessage);
}
/// <summary>
/// For finding outliers in a dataset with collections of values (e.g. Vector3).
/// Convert values to a collection of numbers.
/// </summary>
/// <param name="data"></param>
/// <param name="multiplier"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static Dictionary<float[], T> Outliers<T>(this Dictionary<float[], T> data, float multiplier = 1.5f)
{
Dictionary<float[], T> allOutliers = new Dictionary<float[], T>();
var dataKeys = data.Keys.ToArray();
if (data.Count >= minQty)
{
//reorganize data into multiple collections for stat analysis
SortedList<float, T>[] colPerDim = new SortedList<float, T>[dataKeys[0].Length];
for (int i = 0; i < colPerDim.Length; i++)
colPerDim[i] = new SortedList<float, T>();
foreach (var datum in data)
{
var dimensions = datum.Key;
for (int i = 0; i < dimensions.Length; i++)
if(!colPerDim[i].ContainsKey(dimensions[i]))
colPerDim[i].Add(dimensions[i], datum.Value);
}
for (int i = 0; i < colPerDim.Length; i++)
{
var outliers = colPerDim[i].Outliers(multiplier);
for (int j = 0; j < colPerDim[i].Count; j++)
{
if(outliers.ContainsKey(colPerDim[i].Keys[j]))
{
var outlier = dataKeys[j];
if(!allOutliers.ContainsKey(outlier))
allOutliers.Add(dataKeys[j], outliers[colPerDim[i].Keys[j]]);
}
}
}
}
else
throw new IndexOutOfRangeException(outOfRangeMessage);
return allOutliers;
}
public static async UniTask<Dictionary<float[], T>> OutliersAsync<T>(
this Dictionary<float[], T> data, float multiplier = 1.5f) =>
await UniTask.RunOnThreadPool(() => Outliers(data, multiplier));
}
}