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