1 #Region "Microsoft.VisualBasic::2468500fe3daa4045acb2131160063a9, Microsoft.VisualBasic.Core\Extensions\Math\StatisticsMathExtensions\Linq\EnumerableStatsVarianceP.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 EnumerableStatsVarianceP
35     
36     '         Function: (+20 Overloads) VarianceP
37     
38     
39     ' /********************************************************************************/
40
41 #End Region
42
43 Imports System.Runtime.CompilerServices
44 Imports sys = System.Math
45
46 Namespace Math.Statistics.Linq
47
48     Public Module EnumerableStatsVarianceP
49         '         ' Summary:
50         '     Computes the VarianceP of a sequence of nullable System.Decimal values.
51         '         ' Parameters:
52         '   source:
53         '     A sequence of nullable System.Decimal values to calculate the VarianceP of.
54         '         ' Returns:
55         '     The VarianceP of the sequence of values, or null if the source sequence is
56         '     empty or contains only values that are null.
57         '         ' Exceptions:
58         '   System.ArgumentNullException:
59         '     source is null.
60         '         '   System.OverflowException:
61         '     The sum of the elements in the sequence is larger than System.Decimal.MaxValue.
62         <Extension>
63         Public Function VarianceP(source As IEnumerable(Of Decimal?)) As Decimal
64             Dim values As IEnumerable(Of Decimal) = source.Coalesce()
65             If values.Any() Then
66                 Return values.VarianceP()
67             End If
68
69             Return Nothing
70         End Function
71         '         ' Summary:
72         '     Computes the VarianceP of a sequence of System.Decimal values.
73         '         ' Parameters:
74         '   source:
75         '     A sequence of System.Decimal values to calculate the VarianceP of.
76         '         ' Returns:
77         '     The VarianceP of the sequence of values.
78         '         ' Exceptions:
79         '   System.ArgumentNullException:
80         '     source is null.
81         '         '   System.InvalidOperationException:
82         '     source contains no elements.
83         '         '   System.OverflowException:
84         '     The sum of the elements in the sequence is larger than System.Decimal.MaxValue.
85         <Extension>
86         Public Function VarianceP(source As IEnumerable(Of Decimal)) As Decimal
87             Return CDec(source.[Select](Function(x) CDbl(x)).VarianceP())
88         End Function
89         '         ' Summary:
90         '     Computes the VarianceP of a sequence of nullable System.Double values.
91         '         ' Parameters:
92         '   source:
93         '     A sequence of nullable System.Double values to calculate the VarianceP of.
94         '         ' Returns:
95         '     The VarianceP of the sequence of values, or null if the source sequence is
96         '     empty or contains only values that are null.
97         '         ' Exceptions:
98         '   System.ArgumentNullException:
99         '     source is null.
100         <Extension>
101         Public Function VarianceP(source As IEnumerable(Of Double?)) As Double
102             Dim values As IEnumerable(Of Double) = source.Coalesce()
103             If values.Any() Then
104                 Return values.VarianceP()
105             End If
106
107             Return Nothing
108         End Function
109         '         ' Summary:
110         '     Computes the VarianceP of a sequence of System.Double values.
111         '         ' Parameters:
112         '   source:
113         '     A sequence of System.Double values to calculate the VarianceP of.
114         '         ' Returns:
115         '     The VarianceP of the sequence of values.
116         '         ' Exceptions:
117         '   System.ArgumentNullException:
118         '     source is null.
119         '         '   System.InvalidOperationException:
120         '     source contains no elements.
121         <Extension>
122         Public Function VarianceP(source As IEnumerable(Of Double)) As Double
123             Dim avg As Double = source.Average()
124             Dim d As Double = source.Aggregate(0.0, func:=Function(total, [next]) As Double
125                                                               total += sys.Pow([next] - avg, 2)
126                                                               Return total
127                                                           End Function)
128             Return d / source.Count()
129         End Function
130         '         ' Summary:
131         '     Computes the VarianceP of a sequence of nullable System.Single values.
132         '         ' Parameters:
133         '   source:
134         '     A sequence of nullable System.Single values to calculate the VarianceP of.
135         '         ' Returns:
136         '     The VarianceP of the sequence of values, or null if the source sequence is
137         '     empty or contains only values that are null.
138         '         ' Exceptions:
139         '   System.ArgumentNullException:
140         '     source is null.
141         <Extension>
142         Public Function VarianceP(source As IEnumerable(Of Single?)) As Single
143             Dim values As IEnumerable(Of Single) = source.Coalesce()
144             If values.Any() Then
145                 Return values.VarianceP()
146             End If
147
148             Return Nothing
149         End Function
150         '         ' Summary:
151         '     Computes the VarianceP of a sequence of System.Single values.
152         '         ' Parameters:
153         '   source:
154         '     A sequence of System.Single values to calculate the VarianceP of.
155         '         ' Returns:
156         '     The VarianceP of the sequence of values.
157         '         ' Exceptions:
158         '   System.ArgumentNullException:
159         '     source is null.
160         '         '   System.InvalidOperationException:
161         '     source contains no elements.
162         <Extension>
163         Public Function VarianceP(source As IEnumerable(Of Single)) As Single
164             Return CSng(source.[Select](Function(x) CDbl(x)).VarianceP())
165         End Function
166         '         ' Summary:
167         '     Computes the VarianceP of a sequence of nullable System.Int32 values.
168         '         ' Parameters:
169         '   source:
170         '     A sequence of nullable System.Int32values to calculate the VarianceP of.
171         '         ' Returns:
172         '     The VarianceP of the sequence of values, or null if the source sequence is
173         '     empty or contains only values that are null.
174         '         ' Exceptions:
175         '   System.ArgumentNullException:
176         '     source is null.
177         '         '   System.OverflowException:
178         '     The sum of the elements in the sequence is larger than System.Int64.MaxValue.
179         <Extension>
180         Public Function VarianceP(source As IEnumerable(Of Integer?)) As Double
181             Dim values As IEnumerable(Of Integer) = source.Coalesce()
182             If values.Any() Then
183                 Return values.VarianceP()
184             End If
185
186             Return Nothing
187         End Function
188         '         ' Summary:
189         '     Computes the VarianceP of a sequence of System.Int32 values.
190         '         ' Parameters:
191         '   source:
192         '     A sequence of System.Int32 values to calculate the VarianceP of.
193         '         ' Returns:
194         '     The VarianceP of the sequence of values.
195         '         ' Exceptions:
196         '   System.ArgumentNullException:
197         '     source is null.
198         '         '   System.InvalidOperationException:
199         '     source contains no elements.
200         '         '   System.OverflowException:
201         '     The sum of the elements in the sequence is larger than System.Int64.MaxValue.
202         <Extension>
203         Public Function VarianceP(source As IEnumerable(Of Integer)) As Double
204             Return source.[Select](Function(x) CDbl(x)).VarianceP()
205         End Function
206         '         ' Summary:
207         '     Computes the VarianceP of a sequence of nullable System.Int64 values.
208         '         ' Parameters:
209         '   source:
210         '     A sequence of nullable System.Int64 values to calculate the VarianceP of.
211         '         ' Returns:
212         '     The VarianceP of the sequence of values, or null if the source sequence is
213         '     empty or contains only values that are null.
214         '         ' Exceptions:
215         '   System.ArgumentNullException:
216         '     source is null.
217         '         '   System.OverflowException:
218         '     The sum of the elements in the sequence is larger than System.Int64.MaxValue.
219         <Extension>
220         Public Function VarianceP(source As IEnumerable(Of Long?)) As Double
221             Dim values As IEnumerable(Of Long) = source.Coalesce()
222             If values.Any() Then
223                 Return values.VarianceP()
224             End If
225
226             Return Nothing
227         End Function
228         '         ' Summary:
229         '     Computes the VarianceP of a sequence of System.Int64 values.
230         '         ' Parameters:
231         '   source:
232         '     A sequence of System.Int64 values to calculate the VarianceP of.
233         '         ' Returns:
234         '     The VarianceP of the sequence of values.
235         '         ' Exceptions:
236         '   System.ArgumentNullException:
237         '     source is null.
238         '         '   System.InvalidOperationException:
239         '     source contains no elements.
240         '         '   System.OverflowException:
241         '     The sum of the elements in the sequence is larger than System.Int64.MaxValue.
242         <Extension>
243         Public Function VarianceP(source As IEnumerable(Of Long)) As Double
244             Return source.[Select](Function(x) CDbl(x)).VarianceP()
245         End Function
246         '         ' Summary:
247         '     Computes the VarianceP of a sequence of nullable System.Decimal values that
248         '     are obtained by invoking a transform function on each element of the input
249         '     sequence.
250         '         ' Parameters:
251         '   source:
252         '     A sequence of values to calculate the VarianceP of.
253         '         '   selector:
254         '     A transform function to apply to each element.
255         '         ' Type parameters:
256         '   TSource:
257         '     The type of the elements of source.
258         '         ' Returns:
259         '     The VarianceP of the sequence of values, or null if the source sequence is
260         '     empty or contains only values that are null.
261         '         ' Exceptions:
262         '   System.ArgumentNullException:
263         '     source or selector is null.
264         '         '   System.OverflowException:
265         '     The sum of the elements in the sequence is larger than System.Decimal.MaxValue.
266         <Extension>
267         Public Function VarianceP(Of TSource)(source As IEnumerable(Of TSource), selector As Func(Of TSource, Decimal?)) As Decimal
268             Return source.[Select](selector).VarianceP()
269         End Function
270         '         ' Summary:
271         '     Computes the VarianceP of a sequence of System.Decimal values that are obtained
272         '     by invoking a transform function on each element of the input sequence.
273         '         ' Parameters:
274         '   source:
275         '     A sequence of values that are used to calculate an VarianceP.
276         '         '   selector:
277         '     A transform function to apply to each element.
278         '         ' Type parameters:
279         '   TSource:
280         '     The type of the elements of source.
281         '         ' Returns:
282         '     The VarianceP of the sequence of values.
283         '         ' Exceptions:
284         '   System.ArgumentNullException:
285         '     source or selector is null.
286         '         '   System.InvalidOperationException:
287         '     source contains no elements.
288         '         '   System.OverflowException:
289         '     The sum of the elements in the sequence is larger than System.Decimal.MaxValue.
290         <Extension>
291         Public Function VarianceP(Of TSource)(source As IEnumerable(Of TSource), selector As Func(Of TSource, Decimal)) As Decimal
292             Return source.[Select](selector).VarianceP()
293         End Function
294         '         ' Summary:
295         '     Computes the VarianceP of a sequence of nullable System.Double values that
296         '     are obtained by invoking a transform function on each element of the input
297         '     sequence.
298         '         ' Parameters:
299         '   source:
300         '     A sequence of values to calculate the VarianceP of.
301         '         '   selector:
302         '     A transform function to apply to each element.
303         '         ' Type parameters:
304         '   TSource:
305         '     The type of the elements of source.
306         '         ' Returns:
307         '     The VarianceP of the sequence of values, or null if the source sequence is
308         '     empty or contains only values that are null.
309         '         ' Exceptions:
310         '   System.ArgumentNullException:
311         '     source or selector is null.
312         <Extension>
313         Public Function VarianceP(Of TSource)(source As IEnumerable(Of TSource), selector As Func(Of TSource, Double?)) As Double
314             Return source.[Select](selector).VarianceP()
315         End Function
316         '         ' Summary:
317         '     Computes the VarianceP of a sequence of System.Double values that are obtained
318         '     by invoking a transform function on each element of the input sequence.
319         '         ' Parameters:
320         '   source:
321         '     A sequence of values to calculate the VarianceP of.
322         '         '   selector:
323         '     A transform function to apply to each element.
324         '         ' Type parameters:
325         '   TSource:
326         '     The type of the elements of source.
327         '         ' Returns:
328         '     The VarianceP of the sequence of values.
329         '         ' Exceptions:
330         '   System.ArgumentNullException:
331         '     source or selector is null.
332         '         '   System.InvalidOperationException:
333         '     source contains no elements.
334         <Extension>
335         Public Function VarianceP(Of TSource)(source As IEnumerable(Of TSource), selector As Func(Of TSource, Double)) As Double
336             Return source.[Select](selector).VarianceP()
337         End Function
338         '         ' Summary:
339         '     Computes the VarianceP of a sequence of nullable System.Single values that
340         '     are obtained by invoking a transform function on each element of the input
341         '     sequence.
342         '         ' Parameters:
343         '   source:
344         '     A sequence of values to calculate the VarianceP of.
345         '         '   selector:
346         '     A transform function to apply to each element.
347         '         ' Type parameters:
348         '   TSource:
349         '     The type of the elements of source.
350         '         ' Returns:
351         '     The VarianceP of the sequence of values, or null if the source sequence is
352         '     empty or contains only values that are null.
353         '         ' Exceptions:
354         '   System.ArgumentNullException:
355         '     source or selector is null.
356         <Extension>
357         Public Function VarianceP(Of TSource)(source As IEnumerable(Of TSource), selector As Func(Of TSource, Single?)) As Single
358             Return source.[Select](selector).VarianceP()
359         End Function
360         '         ' Summary:
361         '     Computes the VarianceP of a sequence of System.Single values that are obtained
362         '     by invoking a transform function on each element of the input sequence.
363         '         ' Parameters:
364         '   source:
365         '     A sequence of values to calculate the VarianceP of.
366         '         '   selector:
367         '     A transform function to apply to each element.
368         '         ' Type parameters:
369         '   TSource:
370         '     The type of the elements of source.
371         '         ' Returns:
372         '     The VarianceP of the sequence of values.
373         '         ' Exceptions:
374         '   System.ArgumentNullException:
375         '     source or selector is null.
376         '         '   System.InvalidOperationException:
377         '     source contains no elements.
378         <Extension>
379         Public Function VarianceP(Of TSource)(source As IEnumerable(Of TSource), selector As Func(Of TSource, Single)) As Single
380             Return source.[Select](selector).VarianceP()
381         End Function
382         '         ' Summary:
383         '     Computes the VarianceP of a sequence of nullable System.Int32 values that are
384         '     obtained by invoking a transform function on each element of the input sequence.
385         '         ' Parameters:
386         '   source:
387         '     A sequence of values to calculate the VarianceP of.
388         '         '   selector:
389         '     A transform function to apply to each element.
390         '         ' Type parameters:
391         '   TSource:
392         '     The type of the elements of source.
393         '         ' Returns:
394         '     The VarianceP of the sequence of values, or null if the source sequence is
395         '     empty or contains only values that are null.
396         '         ' Exceptions:
397         '   System.ArgumentNullException:
398         '     source or selector is null.
399         '         '   System.OverflowException:
400         '     The sum of the elements in the sequence is larger than System.Int64.MaxValue.
401         <Extension>
402         Public Function VarianceP(Of TSource)(source As IEnumerable(Of TSource), selector As Func(Of TSource, Integer?)) As Double
403             Return source.[Select](selector).VarianceP()
404         End Function
405         '         ' Summary:
406         '     Computes the VarianceP of a sequence of System.Int32 values that are obtained
407         '     by invoking a transform function on each element of the input sequence.
408         '         ' Parameters:
409         '   source:
410         '     A sequence of values to calculate the VarianceP of.
411         '         '   selector:
412         '     A transform function to apply to each element.
413         '         ' Type parameters:
414         '   TSource:
415         '     The type of the elements of source.
416         '         ' Returns:
417         '     The VarianceP of the sequence of values.
418         '         ' Exceptions:
419         '   System.ArgumentNullException:
420         '     source or selector is null.
421         '         '   System.InvalidOperationException:
422         '     source contains no elements.
423         '         '   System.OverflowException:
424         '     The sum of the elements in the sequence is larger than System.Int64.MaxValue.
425         <Extension>
426         Public Function VarianceP(Of TSource)(source As IEnumerable(Of TSource), selector As Func(Of TSource, Integer)) As Double
427             Return source.[Select](selector).VarianceP()
428         End Function
429         '         ' Summary:
430         '     Computes the VarianceP of a sequence of nullable System.Int64 values that are
431         '     obtained by invoking a transform function on each element of the input sequence.
432         '         ' Parameters:
433         '   source:
434         '     A sequence of values to calculate the VarianceP of.
435         '         '   selector:
436         '     A transform function to apply to each element.
437         '         ' Type parameters:
438         '   TSource:
439         '     The type of the elements of source.
440         '         ' Returns:
441         '     The VarianceP of the sequence of values, or null if the source sequence is
442         '     empty or contains only values that are null.
443         <Extension>
444         Public Function VarianceP(Of TSource)(source As IEnumerable(Of TSource), selector As Func(Of TSource, Long?)) As Double
445             Return source.[Select](selector).VarianceP()
446         End Function
447         '         ' Summary:
448         '     Computes the VarianceP of a sequence of System.Int64 values that are obtained
449         '     by invoking a transform function on each element of the input sequence.
450         '         ' Parameters:
451         '   source:
452         '     A sequence of values to calculate the VarianceP of.
453         '         '   selector:
454         '     A transform function to apply to each element.
455         '         ' Type parameters:
456         '   TSource:
457         '     The type of the elements of source.
458         '         ' Returns:
459         '     The VarianceP of the sequence of values.
460         '         ' Exceptions:
461         '   System.ArgumentNullException:
462         '     source or selector is null.
463         '         '   System.InvalidOperationException:
464         '     source contains no elements.
465         '         '   System.OverflowException:
466         '     The sum of the elements in the sequence is larger than System.Int64.MaxValue.
467         <Extension>
468         Public Function VarianceP(Of TSource)(source As IEnumerable(Of TSource), selector As Func(Of TSource, Long)) As Double
469             Return source.[Select](selector).VarianceP()
470         End Function
471     End Module
472 End Namespace