1 #Region "Microsoft.VisualBasic::fd2640e3a6bb5921876ddc6ebc5b35fb, Microsoft.VisualBasic.Core\Extensions\Math\StatisticsMathExtensions\Linq\EnumerableStatsMedian.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Module EnumerableStatsMedian
35     
36     '         Function: (+20 OverloadsMedian
37     
38     
39     ' /********************************************************************************/
40
41 #End Region
42
43 Imports System.Collections.Generic
44 Imports System.Linq
45 Imports System.Runtime.CompilerServices
46
47 Namespace Math.Statistics.Linq
48
49     Public Module EnumerableStatsMedian
50         '         ' Summary:
51         '     Computes the Median of a sequence of nullable System.Decimal values.
52         '         ' Parameters:
53         '   source:
54         '     A sequence of nullable System.Decimal values to calculate the Median of.
55         '         ' Returns:
56         '     The Median of the sequence of values, or null if the source sequence is
57         '     empty or contains only values that are null.
58         '         ' Exceptions:
59         '   System.ArgumentNullException:
60         '     source is null.
61         '         '   System.OverflowException:
62         '     The sum of the elements in the sequence is larger than System.Decimal.MaxValue.
63         <Extension>
64         Public Function Median(source As IEnumerable(Of Decimal?)) As Decimal
65             Dim values As IEnumerable(Of Decimal) = source.Coalesce()
66
67             If values.Any() Then
68                 Return values.Median()
69             End If
70
71             Return Nothing
72         End Function
73         '         ' Summary:
74         '     Computes the Median of a sequence of System.Decimal values.
75         '         ' Parameters:
76         '   source:
77         '     A sequence of System.Decimal values to calculate the Median of.
78         '         ' Returns:
79         '     The Median of the sequence of values.
80         '         ' Exceptions:
81         '   System.ArgumentNullException:
82         '     source is null.
83         '         '   System.InvalidOperationException:
84         '     source contains no elements.
85         '         '   System.OverflowException:
86         '     The sum of the elements in the sequence is larger than System.Decimal.MaxValue.
87         <Extension>
88         Public Function Median(source As IEnumerable(Of Decimal)) As Decimal
89             Dim sortedList = From number In source Order By number Select number
90
91             Dim count As Integer = sortedList.Count()
92             Dim itemIndex As Integer = count \ 2
93
94             If count Mod 2 = 0 Then
95                 ' Even number of items.
96                 Return (sortedList.ElementAt(itemIndex) + sortedList.ElementAt(itemIndex - 1)) / 2
97             End If
98
99             ' Odd number of items.
100             Return sortedList.ElementAt(itemIndex)
101         End Function
102         '         ' Summary:
103         '     Computes the Median of a sequence of nullable System.Double values.
104         '         ' Parameters:
105         '   source:
106         '     A sequence of nullable System.Double values to calculate the Median of.
107         '         ' Returns:
108         '     The Median of the sequence of values, or null if the source sequence is
109         '     empty or contains only values that are null.
110         '         ' Exceptions:
111         '   System.ArgumentNullException:
112         '     source is null.
113         <Extension>
114         Public Function Median(source As IEnumerable(Of Double?)) As Double
115             Dim values As IEnumerable(Of Double) = source.Coalesce()
116             If values.Any() Then
117                 Return values.Median()
118             End If
119
120             Return Nothing
121         End Function
122         '         ' Summary:
123         '     Computes the Median of a sequence of System.Double values.
124         '         ' Parameters:
125         '   source:
126         '     A sequence of System.Double values to calculate the Median of.
127         '         ' Returns:
128         '     The Median of the sequence of values.
129         '         ' Exceptions:
130         '   System.ArgumentNullException:
131         '     source is null.
132         '         '   System.InvalidOperationException:
133         '     source contains no elements.
134         <Extension>
135         Public Function Median(source As IEnumerable(Of Double)) As Double
136             Dim sortedList = From number In source Order By number Select number
137
138             Dim count As Integer = sortedList.Count()
139             Dim itemIndex As Integer = count \ 2
140
141             If count Mod 2 = 0 Then
142                 ' Even number of items.
143                 Return (sortedList.ElementAt(itemIndex) + sortedList.ElementAt(itemIndex - 1)) / 2
144             End If
145
146             ' Odd number of items.
147             Return sortedList.ElementAt(itemIndex)
148         End Function
149         '         ' Summary:
150         '     Computes the Median of a sequence of nullable System.Single values.
151         '         ' Parameters:
152         '   source:
153         '     A sequence of nullable System.Single values to calculate the Median of.
154         '         ' Returns:
155         '     The Median of the sequence of values, or null if the source sequence is
156         '     empty or contains only values that are null.
157         '         ' Exceptions:
158         '   System.ArgumentNullException:
159         '     source is null.
160         <Extension>
161         Public Function Median(source As IEnumerable(Of Single?)) As Single
162             Dim values As IEnumerable(Of Single) = source.Coalesce()
163             If values.Any() Then
164                 Return values.Median()
165             End If
166
167             Return Nothing
168         End Function
169         '         ' Summary:
170         '     Computes the Median of a sequence of System.Single values.
171         '         ' Parameters:
172         '   source:
173         '     A sequence of System.Single values to calculate the Median of.
174         '         ' Returns:
175         '     The Median of the sequence of values.
176         '         ' Exceptions:
177         '   System.ArgumentNullException:
178         '     source is null.
179         '         '   System.InvalidOperationException:
180         '     source contains no elements.
181         <Extension>
182         Public Function Median(source As IEnumerable(Of Single)) As Single
183             Dim sortedList = From number In source Order By number Select number
184
185             Dim count As Integer = sortedList.Count()
186             Dim itemIndex As Integer = count \ 2
187
188             If count Mod 2 = 0 Then
189                 ' Even number of items.
190                 Return (sortedList.ElementAt(itemIndex) + sortedList.ElementAt(itemIndex - 1)) / 2
191             End If
192
193             ' Odd number of items.
194             Return sortedList.ElementAt(itemIndex)
195         End Function
196         '         ' Summary:
197         '     Computes the Median of a sequence of nullable System.Int32 values.
198         '         ' Parameters:
199         '   source:
200         '     A sequence of nullable System.Int32values to calculate the Median of.
201         '         ' Returns:
202         '     The Median of the sequence of values, or null if the source sequence is
203         '     empty or contains only values that are null.
204         '         ' Exceptions:
205         '   System.ArgumentNullException:
206         '     source is null.
207         '         '   System.OverflowException:
208         '     The sum of the elements in the sequence is larger than System.Int64.MaxValue.
209         <Extension>
210         Public Function Median(source As IEnumerable(Of Integer?)) As Double
211             Dim values As IEnumerable(Of Integer) = source.Coalesce()
212             If values.Any() Then
213                 Return values.Median()
214             End If
215
216             Return Nothing
217         End Function
218         '         ' Summary:
219         '     Computes the Median of a sequence of System.Int32 values.
220         '         ' Parameters:
221         '   source:
222         '     A sequence of System.Int32 values to calculate the Median of.
223         '         ' Returns:
224         '     The Median of the sequence of values.
225         '         ' Exceptions:
226         '   System.ArgumentNullException:
227         '     source is null.
228         '         '   System.InvalidOperationException:
229         '     source contains no elements.
230         '         '   System.OverflowException:
231         '     The sum of the elements in the sequence is larger than System.Int64.MaxValue.
232         <Extension>
233         Public Function Median(source As IEnumerable(Of Integer)) As Double
234             Dim sortedList = From number In source Order By number Select CDbl(number)
235
236             Dim count As Integer = sortedList.Count()
237             Dim itemIndex As Integer = count \ 2
238
239             If count Mod 2 = 0 Then
240                 ' Even number of items.
241                 Return (sortedList.ElementAt(itemIndex) + sortedList.ElementAt(itemIndex - 1)) / 2
242             End If
243
244             ' Odd number of items.
245             Return sortedList.ElementAt(itemIndex)
246         End Function
247         '         ' Summary:
248         '     Computes the Median of a sequence of nullable System.Int64 values.
249         '         ' Parameters:
250         '   source:
251         '     A sequence of nullable System.Int64 values to calculate the Median of.
252         '         ' Returns:
253         '     The Median of the sequence of values, or null if the source sequence is
254         '     empty or contains only values that are null.
255         '         ' Exceptions:
256         '   System.ArgumentNullException:
257         '     source is null.
258         '         '   System.OverflowException:
259         '     The sum of the elements in the sequence is larger than System.Int64.MaxValue.
260         <Extension>
261         Public Function Median(source As IEnumerable(Of Long?)) As Double
262             Dim values As IEnumerable(Of Long) = source.Coalesce()
263             If values.Any() Then
264                 Return values.Median()
265             End If
266
267             Return Nothing
268         End Function
269         '         ' Summary:
270         '     Computes the Median of a sequence of System.Int64 values.
271         '         ' Parameters:
272         '   source:
273         '     A sequence of System.Int64 values to calculate the Median of.
274         '         ' Returns:
275         '     The Median of the sequence of values.
276         '         ' Exceptions:
277         '   System.ArgumentNullException:
278         '     source is null.
279         '         '   System.InvalidOperationException:
280         '     source contains no elements.
281         '         '   System.OverflowException:
282         '     The sum of the elements in the sequence is larger than System.Int64.MaxValue.
283         <Extension>
284         Public Function Median(source As IEnumerable(Of Long)) As Double
285             Dim sortedList = From number In source Order By number Select CDbl(number)
286
287             Dim count As Integer = sortedList.Count()
288             Dim itemIndex As Integer = count \ 2
289
290             If count Mod 2 = 0 Then
291                 ' Even number of items.
292                 Return (sortedList.ElementAt(itemIndex) + sortedList.ElementAt(itemIndex - 1)) / 2
293             End If
294
295             ' Odd number of items.
296             Return sortedList.ElementAt(itemIndex)
297         End Function
298         '         ' Summary:
299         '     Computes the Median of a sequence of nullable System.Decimal values that
300         '     are obtained by invoking a transform function on each element of the input
301         '     sequence.
302         '         ' Parameters:
303         '   source:
304         '     A sequence of values to calculate the Median of.
305         '         '   selector:
306         '     A transform function to apply to each element.
307         '         ' Type parameters:
308         '   TSource:
309         '     The type of the elements of source.
310         '         ' Returns:
311         '     The Median of the sequence of values, or null if the source sequence is
312         '     empty or contains only values that are null.
313         '         ' Exceptions:
314         '   System.ArgumentNullException:
315         '     source or selector is null.
316         '         '   System.OverflowException:
317         '     The sum of the elements in the sequence is larger than System.Decimal.MaxValue.
318         <Extension>
319         Public Function Median(Of TSource)(source As IEnumerable(Of TSource), selector As Func(Of TSource, Decimal?)) As Decimal
320             Return source.[Select](selector).Median()
321         End Function
322         '         ' Summary:
323         '     Computes the Median of a sequence of System.Decimal values that are obtained
324         '     by invoking a transform function on each element of the input sequence.
325         '         ' Parameters:
326         '   source:
327         '     A sequence of values that are used to calculate an Median.
328         '         '   selector:
329         '     A transform function to apply to each element.
330         '         ' Type parameters:
331         '   TSource:
332         '     The type of the elements of source.
333         '         ' Returns:
334         '     The Median of the sequence of values.
335         '         ' Exceptions:
336         '   System.ArgumentNullException:
337         '     source or selector is null.
338         '         '   System.InvalidOperationException:
339         '     source contains no elements.
340         '         '   System.OverflowException:
341         '     The sum of the elements in the sequence is larger than System.Decimal.MaxValue.
342         <Extension>
343         Public Function Median(Of TSource)(source As IEnumerable(Of TSource), selector As Func(Of TSource, Decimal)) As Decimal
344             Return source.[Select](selector).Median()
345         End Function
346         '         ' Summary:
347         '     Computes the Median of a sequence of nullable System.Double values that
348         '     are obtained by invoking a transform function on each element of the input
349         '     sequence.
350         '         ' Parameters:
351         '   source:
352         '     A sequence of values to calculate the Median of.
353         '         '   selector:
354         '     A transform function to apply to each element.
355         '         ' Type parameters:
356         '   TSource:
357         '     The type of the elements of source.
358         '         ' Returns:
359         '     The Median of the sequence of values, or null if the source sequence is
360         '     empty or contains only values that are null.
361         '         ' Exceptions:
362         '   System.ArgumentNullException:
363         '     source or selector is null.
364         <Extension>
365         Public Function Median(Of TSource)(source As IEnumerable(Of TSource), selector As Func(Of TSource, Double?)) As Double
366             Return source.[Select](selector).Median()
367         End Function
368         '         ' Summary:
369         '     Computes the Median of a sequence of System.Double values that are obtained
370         '     by invoking a transform function on each element of the input sequence.
371         '         ' Parameters:
372         '   source:
373         '     A sequence of values to calculate the Median of.
374         '         '   selector:
375         '     A transform function to apply to each element.
376         '         ' Type parameters:
377         '   TSource:
378         '     The type of the elements of source.
379         '         ' Returns:
380         '     The Median of the sequence of values.
381         '         ' Exceptions:
382         '   System.ArgumentNullException:
383         '     source or selector is null.
384         '         '   System.InvalidOperationException:
385         '     source contains no elements.
386         <Extension>
387         Public Function Median(Of TSource)(source As IEnumerable(Of TSource), selector As Func(Of TSource, Double)) As Double
388             Return source.[Select](selector).Median()
389         End Function
390         '         ' Summary:
391         '     Computes the Median of a sequence of nullable System.Single values that
392         '     are obtained by invoking a transform function on each element of the input
393         '     sequence.
394         '         ' Parameters:
395         '   source:
396         '     A sequence of values to calculate the Median of.
397         '         '   selector:
398         '     A transform function to apply to each element.
399         '         ' Type parameters:
400         '   TSource:
401         '     The type of the elements of source.
402         '         ' Returns:
403         '     The Median of the sequence of values, or null if the source sequence is
404         '     empty or contains only values that are null.
405         '         ' Exceptions:
406         '   System.ArgumentNullException:
407         '     source or selector is null.
408         <Extension>
409         Public Function Median(Of TSource)(source As IEnumerable(Of TSource), selector As Func(Of TSource, Single?)) As Single
410             Return source.[Select](selector).Median()
411         End Function
412         '         ' Summary:
413         '     Computes the Median of a sequence of System.Single values that are obtained
414         '     by invoking a transform function on each element of the input sequence.
415         '         ' Parameters:
416         '   source:
417         '     A sequence of values to calculate the Median of.
418         '         '   selector:
419         '     A transform function to apply to each element.
420         '         ' Type parameters:
421         '   TSource:
422         '     The type of the elements of source.
423         '         ' Returns:
424         '     The Median of the sequence of values.
425         '         ' Exceptions:
426         '   System.ArgumentNullException:
427         '     source or selector is null.
428         '         '   System.InvalidOperationException:
429         '     source contains no elements.
430         <Extension>
431         Public Function Median(Of TSource)(source As IEnumerable(Of TSource), selector As Func(Of TSource, Single)) As Single
432             Return source.[Select](selector).Median()
433         End Function
434         '         ' Summary:
435         '     Computes the Median of a sequence of nullable System.Int32 values that are
436         '     obtained by invoking a transform function on each element of the input sequence.
437         '         ' Parameters:
438         '   source:
439         '     A sequence of values to calculate the Median of.
440         '         '   selector:
441         '     A transform function to apply to each element.
442         '         ' Type parameters:
443         '   TSource:
444         '     The type of the elements of source.
445         '         ' Returns:
446         '     The Median of the sequence of values, or null if the source sequence is
447         '     empty or contains only values that are null.
448         '         ' Exceptions:
449         '   System.ArgumentNullException:
450         '     source or selector is null.
451         '         '   System.OverflowException:
452         '     The sum of the elements in the sequence is larger than System.Int64.MaxValue.
453         <Extension>
454         Public Function Median(Of TSource)(source As IEnumerable(Of TSource), selector As Func(Of TSource, Integer?)) As Double
455             Return source.[Select](selector).Median()
456         End Function
457         '         ' Summary:
458         '     Computes the Median of a sequence of System.Int32 values that are obtained
459         '     by invoking a transform function on each element of the input sequence.
460         '         ' Parameters:
461         '   source:
462         '     A sequence of values to calculate the Median of.
463         '         '   selector:
464         '     A transform function to apply to each element.
465         '         ' Type parameters:
466         '   TSource:
467         '     The type of the elements of source.
468         '         ' Returns:
469         '     The Median of the sequence of values.
470         '         ' Exceptions:
471         '   System.ArgumentNullException:
472         '     source or selector is null.
473         '         '   System.InvalidOperationException:
474         '     source contains no elements.
475         '         '   System.OverflowException:
476         '     The sum of the elements in the sequence is larger than System.Int64.MaxValue.
477         <Extension>
478         Public Function Median(Of TSource)(source As IEnumerable(Of TSource), selector As Func(Of TSource, Integer)) As Double
479             Return source.[Select](selector).Median()
480         End Function
481         '         ' Summary:
482         '     Computes the Median of a sequence of nullable System.Int64 values that are
483         '     obtained by invoking a transform function on each element of the input sequence.
484         '         ' Parameters:
485         '   source:
486         '     A sequence of values to calculate the Median of.
487         '         '   selector:
488         '     A transform function to apply to each element.
489         '         ' Type parameters:
490         '   TSource:
491         '     The type of the elements of source.
492         '         ' Returns:
493         '     The Median of the sequence of values, or null if the source sequence is
494         '     empty or contains only values that are null.
495         <Extension>
496         Public Function Median(Of TSource)(source As IEnumerable(Of TSource), selector As Func(Of TSource, Long?)) As Double
497             Return source.[Select](selector).Median()
498         End Function
499         '         ' Summary:
500         '     Computes the Median of a sequence of System.Int64 values that are obtained
501         '     by invoking a transform function on each element of the input sequence.
502         '         ' Parameters:
503         '   source:
504         '     A sequence of values to calculate the Median of.
505         '         '   selector:
506         '     A transform function to apply to each element.
507         '         ' Type parameters:
508         '   TSource:
509         '     The type of the elements of source.
510         '         ' Returns:
511         '     The Median of the sequence of values.
512         '         ' Exceptions:
513         '   System.ArgumentNullException:
514         '     source or selector is null.
515         '         '   System.InvalidOperationException:
516         '     source contains no elements.
517         '         '   System.OverflowException:
518         '     The sum of the elements in the sequence is larger than System.Int64.MaxValue.
519         <Extension>
520         Public Function Median(Of TSource)(source As IEnumerable(Of TSource), selector As Func(Of TSource, Long)) As Double
521             Return source.[Select](selector).Median()
522         End Function
523     End Module
524 End Namespace