1 | #Region "Microsoft.VisualBasic::56397c07d81bb05df5e4fd5c29b574ae, Microsoft.VisualBasic.Core\Extensions\Math\StatisticsMathExtensions\DataSample.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 | ' Class DataSample |
35 | ' |
36 | ' Properties: Max, Mean, Min, SampleSize |
37 | ' |
38 | ' Constructor: (+2 Overloads) Sub New |
39 | ' Function: GetEnumerator, IEnumerable_GetEnumerator, Split, ToString |
40 | ' |
41 | ' Module DataSampleExtensions |
42 | ' |
43 | ' Function: DoubleSample, IntegerSample |
44 | ' |
45 | ' |
46 | ' /********************************************************************************/ |
47 | |
48 | #End Region |
49 | |
50 | Imports System.Runtime.CompilerServices |
51 | Imports Microsoft.VisualBasic.ComponentModel.Ranges.Model |
52 | Imports Microsoft.VisualBasic.Language |
53 | Imports Microsoft.VisualBasic.Serialization.JSON |
54 | |
55 | Namespace Math.Statistics |
56 | |
57 | ''' <summary> |
58 | ''' Numeric value statics property. |
59 | ''' </summary> |
60 | ''' <typeparam name="T"></typeparam> |
61 | Public Class DataSample(Of T As {IComparable, Structure}) |
62 | Implements IEnumerable(Of T) |
63 | |
64 | Protected Friend ranges As IRanges(Of T) |
65 | Protected Friend buffer As List(Of T) |
66 | Protected Friend means As Double |
67 | |
68 | Public Overridable ReadOnly Property Min As T |
69 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
70 | Get |
71 | Return ranges.Min |
72 | End Get |
73 | End Property |
74 | |
75 | Public Overridable ReadOnly Property Max As T |
76 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
77 | Get |
78 | Return ranges.Max |
79 | End Get |
80 | End Property |
81 | |
82 | ''' <summary> |
83 | ''' The sample average |
84 | ''' </summary> |
85 | ''' <returns></returns> |
86 | Public ReadOnly Property Mean As Double |
87 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
88 | Get |
89 | Return means |
90 | End Get |
91 | End Property |
92 | |
93 | ''' <summary> |
94 | ''' The sample size |
95 | ''' </summary> |
96 | ''' <returns></returns> |
97 | Public ReadOnly Property SampleSize As Integer |
98 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
99 | Get |
100 | Return buffer.Count |
101 | End Get |
102 | End Property |
103 | |
104 | Sub New() |
105 | End Sub |
106 | |
107 | Sub New(sample As IEnumerable(Of T)) |
108 | buffer = New List(Of T)(sample) |
109 | End Sub |
110 | |
111 | Public Iterator Function Split(partSize As Integer) As IEnumerable(Of T()) |
112 | For Each chunk As T() In buffer.SplitIterator(partSize) |
113 | Yield chunk |
114 | Next |
115 | End Function |
116 | |
117 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
118 | Public Overrides Function ToString() As String |
119 | Return buffer.ToArray.GetJson |
120 | End Function |
121 | |
122 | Public Iterator Function GetEnumerator() As IEnumerator(Of T) Implements IEnumerable(Of T).GetEnumerator |
123 | For Each x As T In buffer |
124 | Yield x |
125 | Next |
126 | End Function |
127 | |
128 | Private Iterator Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator |
129 | Yield GetEnumerator() |
130 | End Function |
131 | End Class |
132 | |
133 | Public Module DataSampleExtensions |
134 | |
135 | <Extension> |
136 | Public Function IntegerSample(data As IEnumerable(Of Integer)) As DataSample(Of Integer) |
137 | Dim buf As Integer() = data.ToArray |
138 | Return New DataSample(Of Integer)(buf) With { |
139 | .means = buf.Average, |
140 | .ranges = New IntRange(buf.Min, buf.Max) |
141 | } |
142 | End Function |
143 | |
144 | <Extension> |
145 | Public Function DoubleSample(data As IEnumerable(Of Double)) As DataSample(Of Double) |
146 | Dim buf As Double() = data.ToArray |
147 | Return New DataSample(Of Double)(buf) With { |
148 | .means = buf.Average, |
149 | .ranges = New DoubleRange(buf.Min, buf.Max) |
150 | } |
151 | End Function |
152 | End Module |
153 | End Namespace |