Skip to content

Commit 98ac836

Browse files
committed
Improved XML comments, efficiency improvements to several methods, new time series download methods.
1 parent 72e5062 commit 98ac836

19 files changed

Lines changed: 1747 additions & 145 deletions

File tree

Numerics/Data/Statistics/Probability.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,6 @@ public static double IndependentExclusive(IList<double> probabilities, int[] ind
10171017

10181018
/// <summary>
10191019
/// Returns an array of exclusive probabilities of multiple events occurring assuming independence.
1020-
/// This method applies the <see cref="IndependentExclusive"/> method to multiple event combinations from the indicators array.
10211020
/// </summary>
10221021
/// <param name="probabilities">An array of probabilities for each event.</param>
10231022
/// <param name="indicators">A 2D array of indicators, where each row represents a combination of events, and 0 means the event did not occur and 1 means the event did occur.</param>

Numerics/Data/Statistics/Statistics.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,16 @@ public static double Minimum(IList<double> data)
100100
if (data == null) throw new ArgumentNullException(nameof(data));
101101
if (data.Count == 0) return double.NaN;
102102

103-
double min = double.MaxValue;
103+
double min = double.PositiveInfinity;
104104
for (int i = 0; i < data.Count; i++)
105105
{
106-
if (data[i] < min || double.IsNaN(data[i]))
107-
{
106+
if (double.IsNaN(data[i]))
107+
return double.NaN;
108+
if (data[i] < min)
108109
min = data[i];
109-
}
110110
}
111111

112-
return min;
112+
return double.IsPositiveInfinity(min) ? double.NaN : min;
113113
}
114114

115115
/// <summary>
@@ -122,16 +122,16 @@ public static double Maximum(IList<double> data)
122122
if (data == null) throw new ArgumentNullException(nameof(data));
123123
if (data.Count == 0) return double.NaN;
124124

125-
double max = double.MinValue;
125+
double max = double.NegativeInfinity;
126126
for (int i = 0; i < data.Count; i++)
127127
{
128-
if (data[i] > max || double.IsNaN(data[i]))
129-
{
128+
if (double.IsNaN(data[i]))
129+
return double.NaN;
130+
if (data[i] > max)
130131
max = data[i];
131-
}
132132
}
133133

134-
return max;
134+
return double.IsNegativeInfinity(max) ? double.NaN : max;
135135
}
136136

137137
/// <summary>
@@ -190,7 +190,11 @@ public static double GeometricMean(IList<double> data)
190190

191191
double sum = 0;
192192
for (int i = 0; i < data.Count; i++)
193+
{
194+
if (data[i] <= 0) return double.NaN;
193195
sum += Math.Log(data[i]);
196+
}
197+
194198
return Math.Exp(sum / data.Count);
195199
}
196200

Numerics/Data/Time Series/Support/TimeSeriesDownload.cs

Lines changed: 463 additions & 1 deletion
Large diffs are not rendered by default.

Numerics/Distributions/Univariate/Base/UnivariateDistributionBase.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,6 @@ public virtual double[] CentralMoments(int steps = 300)
392392
/// </summary>
393393
/// <param name="a">The lower integration limit, a.</param>
394394
/// <param name="b">The upper integration limit, b.</param>
395-
/// <param name="steps">Number of integration steps. Default = 300.</param>
396395
/// <summary>
397396
/// Returns conditional central moments (up to 4th order) between [a, b].
398397
/// </summary>

Numerics/Distributions/Univariate/EmpiricalDistribution.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ public override UnivariateDistributionBase Clone()
501501
}
502502

503503

504-
// <summary>
504+
/// <summary>
505505
/// Convolves two empirical distributions using FFT.
506506
/// </summary>
507507
/// <param name="dist1">The first empirical distribution.</param>

Numerics/Distributions/Univariate/KernelDensity.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,13 @@ public override double[] MaximumOfParameters
416416
/// </summary>
417417
private interface IKernel
418418
{
419+
/// <summary>
420+
/// Evaluates the kernel function at the specified point.
421+
/// </summary>
422+
/// <param name="x">The point at which to evaluate the kernel.</param>
423+
/// <returns>
424+
/// The kernel value.
425+
/// </returns>
419426
double Function(double x);
420427
}
421428

@@ -424,6 +431,17 @@ private interface IKernel
424431
/// </summary>
425432
private class EpanechnikovKernel : IKernel
426433
{
434+
/// <summary>
435+
/// Evaluates the Epanechnikov kernel function at the specified point.
436+
/// </summary>
437+
/// <param name="x">The point at which to evaluate the kernel.</param>
438+
/// <returns>
439+
/// The kernel value: 0.75(1-x²) for |x| ≤ 1, and 0 otherwise.
440+
/// </returns>
441+
/// <remarks>
442+
/// The Epanechnikov kernel is optimal in minimizing the mean integrated squared error
443+
/// and has compact support on [-1, 1].
444+
/// </remarks>
427445
public double Function(double x)
428446
{
429447
if (Math.Abs(x) <= 1.0d)
@@ -443,6 +461,17 @@ public double Function(double x)
443461
/// </summary>
444462
private class GuassianKernel : IKernel
445463
{
464+
/// <summary>
465+
/// Evaluates the Gaussian (Normal) kernel function at the specified point.
466+
/// </summary>
467+
/// <param name="x">The point at which to evaluate the kernel.</param>
468+
/// <returns>
469+
/// The standard normal PDF value at x: (1/√(2π)) * exp(-x²/2).
470+
/// </returns>
471+
/// <remarks>
472+
/// This is the default kernel for kernel density estimation. It has infinite support
473+
/// and is infinitely differentiable.
474+
/// </remarks>
446475
public double Function(double x)
447476
{
448477
return Normal.StandardPDF(x);
@@ -455,6 +484,18 @@ public double Function(double x)
455484
private class TriangularKernel : IKernel
456485
{
457486
private Triangular _triangularDist = new Triangular(-1.0d, 0.0d, 1.0d);
487+
488+
/// <summary>
489+
/// Evaluates the triangular kernel function at the specified point.
490+
/// </summary>
491+
/// <param name="x">The point at which to evaluate the kernel.</param>
492+
/// <returns>
493+
/// The triangular PDF value at x with minimum -1, mode 0, and maximum 1.
494+
/// Returns (1 - |x|) for |x| ≤ 1, and 0 otherwise.
495+
/// </returns>
496+
/// <remarks>
497+
/// The triangular kernel has compact support on [-1, 1] and is computationally efficient.
498+
/// </remarks>
458499
public double Function(double x)
459500
{
460501
return _triangularDist.PDF(x);
@@ -467,6 +508,19 @@ public double Function(double x)
467508
private class UniformKernel : IKernel
468509
{
469510
private Uniform _uniformDist = new Uniform(-1.0d, 1.0d);
511+
512+
/// <summary>
513+
/// Evaluates the uniform kernel function at the specified point.
514+
/// </summary>
515+
/// <param name="x">The point at which to evaluate the kernel.</param>
516+
/// <returns>
517+
/// The uniform PDF value: 0.5 for |x| ≤ 1, and 0 otherwise.
518+
/// </returns>
519+
/// <remarks>
520+
/// The uniform kernel has compact support on [-1, 1] and gives equal weight
521+
/// to all points within the bandwidth. This is the simplest kernel but has
522+
/// discontinuous derivatives.
523+
/// </remarks>
470524
public double Function(double x)
471525
{
472526
return _uniformDist.PDF(x);

Numerics/Distributions/Univariate/Uncertainty Analysis/BootstrapAnalysis.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -745,16 +745,14 @@ private double[] AccelerationConstants(IList<double> sampleData, IList<double> p
745745
}
746746

747747
/// <summary>
748-
/// Estimates the standard error for each probability.
748+
/// Estimates the standard error for each probability using the parametric bootstrap.
749749
/// </summary>
750-
/// <param name="sampleData">Sample of data.</param>
751-
/// <param name="probabilities">List of non-exceedance probabilities.</param>
752-
/// <param name="thetaHats">The list of best-estimate quantiles.</param>
750+
///<param name="parentDist">The parent distribution.</param>
751+
///<param name="probabilities">The list of probabilities where the standard error is calculated.</param>
752+
///<param name="replications">The number of bootstrap replications. Default = 300.</param>
753+
///<param name="seed">The PRNG seed. Default = 12345.</param>
753754
private double[] BootstrapStandardError(UnivariateDistributionBase parentDist, IList<double> probabilities, int replications = 300, int seed = 12345)
754755
{
755-
//var N = sampleData.Count;
756-
//var I2 = new double[probabilities.Count];
757-
//var se = new double[probabilities.Count];
758756
int B = replications;
759757
var r = new MersenneTwister(seed);
760758
var seeds = r.NextIntegers(replications);

0 commit comments

Comments
 (0)