1 #Region "Microsoft.VisualBasic::06e542dcaa9576b64a02ea384bff89b5, Microsoft.VisualBasic.Core\Extensions\Math\StatisticsMathExtensions\Linq\EnumerableStatsPearson.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 EnumerableStatsPearson
35     
36     '         Function: (+20 Overloads) Pearson
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 EnumerableStatsPearson
50         '         ' Summary:
51         '     Computes the Pearson of a sequence of nullable System.Decimal values.
52         '         ' Parameters:
53         '   source:
54         '     A sequence of nullable System.Decimal values to calculate the Pearson of.
55         '         ' Returns:
56         '     The Pearson 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 Pearson(source As IEnumerable(Of Decimal?), other As IEnumerable(Of Decimal?)) As Decimal
65             Dim values As IEnumerable(Of Decimal) = source.Coalesce()
66             If values.Any() Then
67                 Return values.Pearson(other.Coalesce())
68             End If
69
70             Return Nothing
71         End Function
72         '         ' Summary:
73         '     Computes the Pearson of a sequence of System.Decimal values.
74         '         ' Parameters:
75         '   source:
76         '     A sequence of System.Decimal values to calculate the Pearson of.
77         '         ' Returns:
78         '     The Pearson of the sequence of values.
79         '         ' Exceptions:
80         '   System.ArgumentNullException:
81         '     source is null.
82         '         '   System.InvalidOperationException:
83         '     source contains no elements.
84         '         '   System.OverflowException:
85         '     The sum of the elements in the sequence is larger than System.Decimal.MaxValue.
86         <Extension>
87         Public Function Pearson(source As IEnumerable(Of Decimal), other As IEnumerable(Of Decimal)) As Decimal
88             Return CDec(source.[Select](Function(x) CDbl(x)).Pearson(other.[Select](Function(x) CDbl(x))))
89         End Function
90         '         ' Summary:
91         '     Computes the Pearson of a sequence of nullable System.Double values.
92         '         ' Parameters:
93         '   source:
94         '     A sequence of nullable System.Double values to calculate the Pearson of.
95         '         ' Returns:
96         '     The Pearson of the sequence of values, or null if the source sequence is
97         '     empty or contains only values that are null.
98         '         ' Exceptions:
99         '   System.ArgumentNullException:
100         '     source is null.
101         <Extension>
102         Public Function Pearson(source As IEnumerable(Of Double?), other As IEnumerable(Of Double?)) As Double
103             Dim values As IEnumerable(Of Double) = source.Coalesce()
104             If values.Any() Then
105                 Return values.Pearson(other.Coalesce())
106             End If
107
108             Return Nothing
109         End Function
110         '         ' Summary:
111         '     Computes the Pearson of a sequence of System.Double values.
112         '         ' Parameters:
113         '   source:
114         '     A sequence of System.Double values to calculate the Pearson of.
115         '         ' Returns:
116         '     The Pearson of the sequence of values.
117         '         ' Exceptions:
118         '   System.ArgumentNullException:
119         '     source is null.
120         '         '   System.InvalidOperationException:
121         '     source contains no elements.
122         <Extension>
123         Public Function Pearson(source As IEnumerable(Of Double), other As IEnumerable(Of Double)) As Double
124             If source.Count() <> other.Count() Then
125                 Throw New ArgumentException("Collections are not of the same length""other")
126             End If
127
128             Return source.Covariance(other) / (source.StandardDeviationP() * other.StandardDeviationP())
129         End Function
130         '         ' Summary:
131         '     Computes the Pearson of a sequence of nullable System.Single values.
132         '         ' Parameters:
133         '   source:
134         '     A sequence of nullable System.Single values to calculate the Pearson of.
135         '         ' Returns:
136         '     The Pearson 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 Pearson(source As IEnumerable(Of Single?), other As IEnumerable(Of Single?)) As Single
143             Dim values As IEnumerable(Of Single) = source.Coalesce()
144             If values.Any() Then
145                 Return values.Pearson(other.Coalesce())
146             End If
147
148             Return Nothing
149         End Function
150         '         ' Summary:
151         '     Computes the Pearson of a sequence of System.Single values.
152         '         ' Parameters:
153         '   source:
154         '     A sequence of System.Single values to calculate the Pearson of.
155         '         ' Returns:
156         '     The Pearson 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 Pearson(source As IEnumerable(Of Single), other As IEnumerable(Of Single)) As Single
164             Return CSng(source.[Select](Function(x) CDbl(x)).Pearson(other.[Select](Function(x) CDbl(x))))
165         End Function
166         '         ' Summary:
167         '     Computes the Pearson of a sequence of nullable System.Int32 values.
168         '         ' Parameters:
169         '   source:
170         '     A sequence of nullable System.Int32values to calculate the Pearson of.
171         '         ' Returns:
172         '     The Pearson 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 Pearson(source As IEnumerable(Of Integer?), other As IEnumerable(Of Integer?)) As Double
181             Dim values As IEnumerable(Of Integer) = source.Coalesce()
182             If values.Any() Then
183                 Return values.Pearson(other.Coalesce())
184             End If
185
186             Return Nothing
187         End Function
188         '         ' Summary:
189         '     Computes the Pearson of a sequence of System.Int32 values.
190         '         ' Parameters:
191         '   source:
192         '     A sequence of System.Int32 values to calculate the Pearson of.
193         '         ' Returns:
194         '     The Pearson 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 Pearson(source As IEnumerable(Of Integer), other As IEnumerable(Of Integer)) As Double
204             Return source.[Select](Function(x) CDbl(x)).Pearson(other.[Select](Function(x) CDbl(x)))
205         End Function
206         '         ' Summary:
207         '     Computes the Pearson of a sequence of nullable System.Int64 values.
208         '         ' Parameters:
209         '   source:
210         '     A sequence of nullable System.Int64 values to calculate the Pearson of.
211         '         ' Returns:
212         '     The Pearson 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 Pearson(source As IEnumerable(Of Long?), other As IEnumerable(Of Long?)) As Double
221             Dim values As IEnumerable(Of Long) = source.Coalesce()
222             If values.Any() Then
223                 Return values.Pearson(other.Coalesce())
224             End If
225
226             Return Nothing
227         End Function
228         '         ' Summary:
229         '     Computes the Pearson of a sequence of System.Int64 values.
230         '         ' Parameters:
231         '   source:
232         '     A sequence of System.Int64 values to calculate the Pearson of.
233         '         ' Returns:
234         '     The Pearson 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 Pearson(source As IEnumerable(Of Long), other As IEnumerable(Of Long)) As Double
244             Return source.[Select](Function(x) CDbl(x)).Pearson(other.[Select](Function(x) CDbl(x)))
245         End Function
246         '         ' Summary:
247         '     Computes the Pearson 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 Pearson 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 Pearson 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 Pearson(Of TSource)(source As IEnumerable(Of TSource), other As IEnumerable(Of TSource), selector As Func(Of TSource, Decimal?)) As Decimal
268             Return source.[Select](selector).Pearson(other.[Select](selector))
269         End Function
270         '         ' Summary:
271         '     Computes the Pearson 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 Pearson.
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 Pearson 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 Pearson(Of TSource)(source As IEnumerable(Of TSource), other As IEnumerable(Of TSource), selector As Func(Of TSource, Decimal)) As Decimal
292             Return source.[Select](selector).Pearson(other.[Select](selector))
293         End Function
294         '         ' Summary:
295         '     Computes the Pearson 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 Pearson 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 Pearson 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 Pearson(Of TSource)(source As IEnumerable(Of TSource), other As IEnumerable(Of TSource), selector As Func(Of TSource, Double?)) As Double
314             Return source.[Select](selector).Pearson(other.[Select](selector))
315         End Function
316         '         ' Summary:
317         '     Computes the Pearson 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 Pearson 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 Pearson 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 Pearson(Of TSource)(source As IEnumerable(Of TSource), other As IEnumerable(Of TSource), selector As Func(Of TSource, Double)) As Double
336             Return source.[Select](selector).Pearson(other.[Select](selector))
337         End Function
338         '         ' Summary:
339         '     Computes the Pearson 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 Pearson 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 Pearson 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 Pearson(Of TSource)(source As IEnumerable(Of TSource), other As IEnumerable(Of TSource), selector As Func(Of TSource, Single?)) As Single
358             Return source.[Select](selector).Pearson(other.[Select](selector))
359         End Function
360         '         ' Summary:
361         '     Computes the Pearson 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 Pearson 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 Pearson 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 Pearson(Of TSource)(source As IEnumerable(Of TSource), other As IEnumerable(Of TSource), selector As Func(Of TSource, Single)) As Single
380             Return source.[Select](selector).Pearson(other.[Select](selector))
381         End Function
382         '         ' Summary:
383         '     Computes the Pearson 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 Pearson 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 Pearson 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 Pearson(Of TSource)(source As IEnumerable(Of TSource), other As IEnumerable(Of TSource), selector As Func(Of TSource, Integer?)) As Double
403             Return source.[Select](selector).Pearson(other.[Select](selector))
404         End Function
405         '         ' Summary:
406         '     Computes the Pearson 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 Pearson 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 Pearson 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 Pearson(Of TSource)(source As IEnumerable(Of TSource), other As IEnumerable(Of TSource), selector As Func(Of TSource, Integer)) As Double
427             Return source.[Select](selector).Pearson(other.[Select](selector))
428         End Function
429         '         ' Summary:
430         '     Computes the Pearson 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 Pearson 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 Pearson 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 Pearson(Of TSource)(source As IEnumerable(Of TSource), other As IEnumerable(Of TSource), selector As Func(Of TSource, Long?)) As Double
445             Return source.[Select](selector).Pearson(other.[Select](selector))
446         End Function
447         '         ' Summary:
448         '     Computes the Pearson 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 Pearson 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 Pearson 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 Pearson(Of TSource)(source As IEnumerable(Of TSource), other As IEnumerable(Of TSource), selector As Func(Of TSource, Long)) As Double
469             Return source.[Select](selector).Pearson(other.[Select](selector))
470         End Function
471     End Module
472 End Namespace