|
|
@ -3,7 +3,7 @@ |
|
|
// http://numerics.mathdotnet.com
|
|
|
// http://numerics.mathdotnet.com
|
|
|
// http://github.com/mathnet/mathnet-numerics
|
|
|
// http://github.com/mathnet/mathnet-numerics
|
|
|
//
|
|
|
//
|
|
|
// Copyright (c) 2009-2013 Math.NET
|
|
|
// Copyright (c) 2009-2018 Math.NET
|
|
|
//
|
|
|
//
|
|
|
// Permission is hereby granted, free of charge, to any person
|
|
|
// Permission is hereby granted, free of charge, to any person
|
|
|
// obtaining a copy of this software and associated documentation
|
|
|
// obtaining a copy of this software and associated documentation
|
|
|
@ -29,15 +29,13 @@ |
|
|
|
|
|
|
|
|
using System; |
|
|
using System; |
|
|
using System.Collections.Generic; |
|
|
using System.Collections.Generic; |
|
|
using System.Linq; |
|
|
|
|
|
using System.Text; |
|
|
|
|
|
using MathNet.Numerics.Distributions; |
|
|
using MathNet.Numerics.Distributions; |
|
|
using MathNet.Numerics.Threading; |
|
|
using MathNet.Numerics.Threading; |
|
|
|
|
|
|
|
|
namespace MathNet.Numerics.Statistics |
|
|
namespace MathNet.Numerics.Statistics |
|
|
{ |
|
|
{ |
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// An enum of the methods the <see cref="MathNet.Numerics.Statistics.KernelDensityEstimator"/> supports
|
|
|
/// An enum of the methods the <see cref="KernelDensityEstimator"/> supports
|
|
|
/// for automatic bandwidth selection.
|
|
|
/// for automatic bandwidth selection.
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
public enum KDEBandwidthSelectionMethod |
|
|
public enum KDEBandwidthSelectionMethod |
|
|
@ -46,6 +44,7 @@ namespace MathNet.Numerics.Statistics |
|
|
/// TBD
|
|
|
/// TBD
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
SilvermansRuleOfThumb, |
|
|
SilvermansRuleOfThumb, |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// TBD
|
|
|
/// TBD
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
@ -53,8 +52,8 @@ namespace MathNet.Numerics.Statistics |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// The <see cref="MathNet.Numerics.Statistics.KernelDensityEstimator"/> supports several predefined Kernels.
|
|
|
/// The <see cref="KernelDensityEstimator"/> supports several predefined Kernels.
|
|
|
/// Note that you can set your own custom kernel by setting <see cref="MathNet.Numerics.Statistics.KernelDensityEstimator.Kernel"/>
|
|
|
/// Note that you can set your own custom kernel by setting <see cref="KernelDensityEstimator.Kernel"/>
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
public enum KDEKernelType |
|
|
public enum KDEKernelType |
|
|
{ |
|
|
{ |
|
|
@ -83,13 +82,13 @@ namespace MathNet.Numerics.Statistics |
|
|
Triangular, |
|
|
Triangular, |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// A custom kernel can be set by property <see cref="MathNet.Numerics.Statistics.KernelDensityEstimator.Kernel"/>
|
|
|
/// A custom kernel can be set by property <see cref="KernelDensityEstimator.Kernel"/>
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
Custom |
|
|
Custom |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// Kernel density estimation
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
public class KernelDensityEstimator |
|
|
public class KernelDensityEstimator |
|
|
{ |
|
|
{ |
|
|
@ -99,6 +98,9 @@ namespace MathNet.Numerics.Statistics |
|
|
KernelType = KDEKernelType.Gaussian; |
|
|
KernelType = KDEKernelType.Gaussian; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public double EstimateDensity(double x) |
|
|
public double EstimateDensity(double x) |
|
|
{ |
|
|
{ |
|
|
var n = Samples.Count; |
|
|
var n = Samples.Count; |
|
|
@ -115,29 +117,30 @@ namespace MathNet.Numerics.Statistics |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private readonly IList<double> _samples; |
|
|
private readonly IList<double> _samples; |
|
|
|
|
|
|
|
|
public IList<double> Samples |
|
|
public IList<double> Samples |
|
|
{ |
|
|
{ |
|
|
get { return _samples; } |
|
|
get { return _samples; } |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private double _bandwidth = 1; |
|
|
private double _bandwidth = 1; |
|
|
|
|
|
|
|
|
public double Bandwidth |
|
|
public double Bandwidth |
|
|
{ |
|
|
{ |
|
|
get |
|
|
get { return _bandwidth; } |
|
|
{ |
|
|
|
|
|
return _bandwidth; |
|
|
|
|
|
} |
|
|
|
|
|
set |
|
|
set |
|
|
{ |
|
|
{ |
|
|
if (value <= 0) |
|
|
if (value <= 0) |
|
|
{ |
|
|
{ |
|
|
throw new ArgumentException("The bandwidth must be a positive number!"); |
|
|
throw new ArgumentException("The bandwidth must be a positive number!"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
_bandwidth = value; |
|
|
_bandwidth = value; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private KDEKernelType _kernelType; |
|
|
private KDEKernelType _kernelType; |
|
|
|
|
|
|
|
|
public KDEKernelType KernelType |
|
|
public KDEKernelType KernelType |
|
|
{ |
|
|
{ |
|
|
get { return _kernelType; } |
|
|
get { return _kernelType; } |
|
|
@ -176,6 +179,7 @@ namespace MathNet.Numerics.Statistics |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private Func<double, double> _kernel; |
|
|
private Func<double, double> _kernel; |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// Sets or Gets the Kernel used for the density estimate.
|
|
|
/// Sets or Gets the Kernel used for the density estimate.
|
|
|
/// Setting the Kernel changes the <see cref="KernelDensityEstimator.KernelType"/> to <see cref="KDEKernelType.Custom"/>
|
|
|
/// Setting the Kernel changes the <see cref="KernelDensityEstimator.KernelType"/> to <see cref="KDEKernelType.Custom"/>
|
|
|
|