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