Covar, Kendall, Spearman

This commit is contained in:
Miha
2024-11-08 21:20:06 -08:00
parent fce3a783eb
commit f46a5330bf
12 changed files with 788 additions and 191 deletions
+142
View File
@@ -0,0 +1,142 @@
using System.Runtime.CompilerServices;
namespace QuanTAlib;
/// <summary>
/// COVAR: Covariance
/// A statistical measure that quantifies how two variables change together. Unlike correlation,
/// covariance is not normalized and therefore is scale-dependent. A positive covariance indicates
/// that variables tend to move in the same direction, while a negative covariance indicates
/// opposite movement.
/// </summary>
/// <remarks>
/// The Covariance calculation process:
/// 1. Calculates mean of both variables
/// 2. For each pair of points, multiply their deviations from their respective means
/// 3. Sum these products and divide by the number of observations
///
/// Key characteristics:
/// - Measures linear relationship
/// - Scale-dependent measure
/// - Sign indicates direction of relationship
/// - Magnitude depends on scale of variables
/// - Basis for correlation coefficient
///
/// Formula:
/// Cov(X,Y) = Σ((x - μx)(y - μy)) / n
/// where:
/// X, Y = variables
/// μx, μy = means of X and Y
/// n = number of observations
///
/// Market Applications:
/// - Portfolio risk analysis
/// - Pairs trading strategy development
/// - Asset relationship analysis
/// - Risk factor sensitivity analysis
/// - Multi-asset portfolio optimization
///
/// Sources:
/// https://en.wikipedia.org/wiki/Covariance
/// "Modern Portfolio Theory" - Harry Markowitz
///
/// Note: Scale-dependent nature means values should be interpreted in context of the data scales
/// </remarks>
[SkipLocalsInit]
public sealed class Covar : AbstractBase
{
private readonly int Period;
private readonly CircularBuffer _xValues;
private readonly CircularBuffer _yValues;
private const int MinimumPoints = 2;
/// <param name="period">The number of points to consider for covariance calculation.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 2.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Covar(int period)
{
if (period < MinimumPoints)
{
throw new ArgumentOutOfRangeException(nameof(period),
"Period must be greater than or equal to 2 for covariance calculation.");
}
Period = period;
WarmupPeriod = MinimumPoints;
_xValues = new CircularBuffer(period);
_yValues = new CircularBuffer(period);
Name = $"Covar(period={period})";
Init();
}
/// <param name="source">The data source object that publishes updates.</param>
/// <param name="period">The number of points to consider for covariance calculation.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Covar(object source, int period) : this(period)
{
var pubEvent = source.GetType().GetEvent("Pub");
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override void Init()
{
base.Init();
_xValues.Clear();
_yValues.Clear();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void ManageState(bool isNew)
{
if (isNew)
{
_lastValidValue = Input.Value;
_index++;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private static double CalculateMean(ReadOnlySpan<double> values)
{
double sum = 0;
for (int i = 0; i < values.Length; i++)
{
sum += values[i];
}
return sum / values.Length;
}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private static double CalculateCovariance(ReadOnlySpan<double> xValues, ReadOnlySpan<double> yValues, double xMean, double yMean)
{
double covariance = 0;
for (int i = 0; i < xValues.Length; i++)
{
covariance += (xValues[i] - xMean) * (yValues[i] - yMean);
}
return covariance / xValues.Length;
}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
protected override double Calculation()
{
ManageState(Input.IsNew);
_xValues.Add(Input.Value, Input.IsNew);
_yValues.Add(Input2.Value, Input.IsNew);
double covariance = 0;
if (_xValues.Count >= MinimumPoints && _yValues.Count >= MinimumPoints)
{
ReadOnlySpan<double> xValues = _xValues.GetSpan();
ReadOnlySpan<double> yValues = _yValues.GetSpan();
double xMean = CalculateMean(xValues);
double yMean = CalculateMean(yValues);
covariance = CalculateCovariance(xValues, yValues, xMean, yMean);
}
IsHot = _xValues.Count >= Period && _yValues.Count >= Period;
return covariance;
}
}
+182
View File
@@ -0,0 +1,182 @@
using System.Runtime.CompilerServices;
namespace QuanTAlib;
/// <summary>
/// KENDALL: Kendall's Rank Correlation Coefficient (Tau)
/// A nonparametric measure that evaluates the degree of similarity between two sets
/// of rankings by analyzing concordant and discordant pairs. Unlike Spearman correlation,
/// Kendall's tau measures the ordinal association between two variables.
/// </summary>
/// <remarks>
/// The Kendall calculation process:
/// 1. Compares each pair of observations
/// 2. Counts concordant and discordant pairs
/// 3. Handles ties in both variables
///
/// Key characteristics:
/// - Measures ordinal association
/// - Range: -1 to +1
/// - Robust to outliers
/// - More intuitive probabilistic interpretation
/// - Less sensitive to error than Spearman
///
/// Formula:
/// τ = (nc - nd) / sqrt((n0 - n1)(n0 - n2))
/// where:
/// nc = number of concordant pairs
/// nd = number of discordant pairs
/// n0 = n(n-1)/2
/// n1 = sum(u(u-1)/2) for ties in x
/// n2 = sum(v(v-1)/2) for ties in y
///
/// Market Applications:
/// - Rank correlation analysis
/// - Portfolio diversification
/// - Risk assessment
/// - Market trend analysis
/// - Pattern recognition
///
/// Sources:
/// https://en.wikipedia.org/wiki/Kendall_rank_correlation_coefficient
/// "Rank Correlation Methods" - Maurice G. Kendall
///
/// Note: More robust to outliers and errors than other correlation measures
/// </remarks>
[SkipLocalsInit]
public sealed class Kendall : AbstractBase
{
private readonly int Period;
private readonly CircularBuffer _xValues;
private readonly CircularBuffer _yValues;
private const double Epsilon = 1e-10;
private const int MinimumPoints = 2;
/// <param name="period">The number of points to consider for Kendall correlation calculation.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 2.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Kendall(int period)
{
if (period < MinimumPoints)
{
throw new ArgumentOutOfRangeException(nameof(period),
"Period must be greater than or equal to 2 for Kendall correlation calculation.");
}
Period = period;
WarmupPeriod = MinimumPoints;
_xValues = new CircularBuffer(period);
_yValues = new CircularBuffer(period);
Name = $"Kendall(period={period})";
Init();
}
/// <param name="source">The data source object that publishes updates.</param>
/// <param name="period">The number of points to consider for Kendall correlation calculation.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Kendall(object source, int period) : this(period)
{
var pubEvent = source.GetType().GetEvent("Pub");
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override void Init()
{
base.Init();
_xValues.Clear();
_yValues.Clear();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void ManageState(bool isNew)
{
if (isNew)
{
_lastValidValue = Input.Value;
_index++;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private static (int concordant, int discordant, int tiesX, int tiesY) CountPairs(ReadOnlySpan<double> x, ReadOnlySpan<double> y)
{
int n = x.Length;
int concordant = 0;
int discordant = 0;
int tiesX = 0;
int tiesY = 0;
for (int i = 0; i < n - 1; i++)
{
if (double.IsNaN(x[i]) || double.IsNaN(y[i])) continue;
for (int j = i + 1; j < n; j++)
{
if (double.IsNaN(x[j]) || double.IsNaN(y[j])) continue;
double xDiff = x[i] - x[j];
double yDiff = y[i] - y[j];
if (Math.Abs(xDiff) < Epsilon && Math.Abs(yDiff) < Epsilon)
{
tiesX++;
tiesY++;
}
else if (Math.Abs(xDiff) < Epsilon)
{
tiesX++;
}
else if (Math.Abs(yDiff) < Epsilon)
{
tiesY++;
}
else
{
int xSign = xDiff > 0 ? 1 : -1;
int ySign = yDiff > 0 ? 1 : -1;
if (xSign == ySign)
{
concordant++;
}
else
{
discordant++;
}
}
}
}
return (concordant, discordant, tiesX, tiesY);
}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
protected override double Calculation()
{
ManageState(Input.IsNew);
_xValues.Add(Input.Value, Input.IsNew);
_yValues.Add(Input2.Value, Input.IsNew);
double correlation = 0;
if (_xValues.Count >= MinimumPoints && _yValues.Count >= MinimumPoints)
{
ReadOnlySpan<double> xValues = _xValues.GetSpan();
ReadOnlySpan<double> yValues = _yValues.GetSpan();
var (concordant, discordant, tiesX, tiesY) = CountPairs(xValues, yValues);
int n = xValues.Length;
int n0 = (n * (n - 1)) / 2;
// Calculate denominator considering ties
double denominator = Math.Sqrt((n0 - tiesX) * (n0 - tiesY));
if (denominator > Epsilon)
{
correlation = (concordant - discordant) / denominator;
}
}
IsHot = _xValues.Count >= Period && _yValues.Count >= Period;
return correlation;
}
}
+224
View File
@@ -0,0 +1,224 @@
using System.Runtime.CompilerServices;
namespace QuanTAlib;
/// <summary>
/// SPEARMAN: Spearman's Rank Correlation Coefficient
/// A nonparametric measure of rank correlation that assesses the monotonic relationship
/// between two variables. Unlike Pearson correlation, Spearman correlation evaluates
/// the relationship based on ranked values rather than raw data.
/// </summary>
/// <remarks>
/// The Spearman calculation process:
/// 1. Ranks both sets of values
/// 2. Calculates correlation between ranks
/// 3. Handles ties by averaging ranks
///
/// Key characteristics:
/// - Resistant to outliers
/// - Detects monotonic relationships
/// - Range: -1 to +1
/// - Distribution-free measure
/// - Handles non-linear relationships
///
/// Formula:
/// ρ = Cov(rank(X), rank(Y)) / (σrank(X) * σrank(Y))
/// where:
/// X, Y = variables
/// rank() = ranking function
/// Cov = covariance
/// σ = standard deviation
///
/// Market Applications:
/// - Technical analysis
/// - Risk assessment
/// - Market correlation studies
/// - Trend analysis
/// - Pattern recognition
///
/// Sources:
/// https://en.wikipedia.org/wiki/Spearman%27s_rank_correlation_coefficient
/// "Nonparametric Statistics for Non-Statisticians" - Gregory W. Corder
///
/// Note: More robust to outliers than Pearson correlation
/// </remarks>
[SkipLocalsInit]
public sealed class Spearman : AbstractBase
{
private readonly int Period;
private readonly CircularBuffer _xValues;
private readonly CircularBuffer _yValues;
private readonly CircularBuffer _xRanks;
private readonly CircularBuffer _yRanks;
private const double Epsilon = 1e-10;
private const int MinimumPoints = 2;
/// <param name="period">The number of points to consider for Spearman correlation calculation.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 2.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Spearman(int period)
{
if (period < MinimumPoints)
{
throw new ArgumentOutOfRangeException(nameof(period),
"Period must be greater than or equal to 2 for Spearman correlation calculation.");
}
Period = period;
WarmupPeriod = MinimumPoints;
_xValues = new CircularBuffer(period);
_yValues = new CircularBuffer(period);
_xRanks = new CircularBuffer(period);
_yRanks = new CircularBuffer(period);
Name = $"Spearman(period={period})";
Init();
}
/// <param name="source">The data source object that publishes updates.</param>
/// <param name="period">The number of points to consider for Spearman correlation calculation.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Spearman(object source, int period) : this(period)
{
var pubEvent = source.GetType().GetEvent("Pub");
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override void Init()
{
base.Init();
_xValues.Clear();
_yValues.Clear();
_xRanks.Clear();
_yRanks.Clear();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void ManageState(bool isNew)
{
if (isNew)
{
_lastValidValue = Input.Value;
_index++;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private static double[] CalculateRanks(ReadOnlySpan<double> values)
{
int n = values.Length;
var pairs = new (double value, int index)[n];
for (int i = 0; i < n; i++)
{
pairs[i] = double.IsNaN(values[i]) ? (double.NaN, i) : (values[i], i);
}
// Sort non-NaN values
var validPairs = pairs.Where(p => !double.IsNaN(p.value)).OrderBy(p => p.value).ToArray();
var ranks = new double[n];
Array.Fill(ranks, double.NaN);
for (int i = 0; i < validPairs.Length;)
{
int j = i;
// Find ties
while (j < validPairs.Length - 1 && Math.Abs(validPairs[j].value - validPairs[j + 1].value) < Epsilon)
{
j++;
}
// Average rank for ties
double rank = (i + j) / 2.0 + 1;
for (int k = i; k <= j; k++)
{
ranks[validPairs[k].index] = rank;
}
i = j + 1;
}
return ranks;
}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private static double CalculateCovariance(CircularBuffer xBuffer, CircularBuffer yBuffer, double xMean, double yMean)
{
var xSpan = xBuffer.GetSpan();
var ySpan = yBuffer.GetSpan();
double covariance = 0;
int count = 0;
for (int i = 0; i < xSpan.Length; i++)
{
if (!double.IsNaN(xSpan[i]) && !double.IsNaN(ySpan[i]))
{
covariance += (xSpan[i] - xMean) * (ySpan[i] - yMean);
count++;
}
}
return count > 0 ? covariance / count : double.NaN;
}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private static double CalculateStandardDeviation(CircularBuffer buffer, double mean)
{
var span = buffer.GetSpan();
double sumSquaredDeviations = 0;
int count = 0;
for (int i = 0; i < span.Length; i++)
{
if (!double.IsNaN(span[i]))
{
double deviation = span[i] - mean;
sumSquaredDeviations += deviation * deviation;
count++;
}
}
return count > 0 ? Math.Sqrt(sumSquaredDeviations / count) : double.NaN;
}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
protected override double Calculation()
{
ManageState(Input.IsNew);
_xValues.Add(Input.Value, Input.IsNew);
_yValues.Add(Input2.Value, Input.IsNew);
double correlation = 0;
if (_xValues.Count >= MinimumPoints && _yValues.Count >= MinimumPoints)
{
// Convert values to ranks
var xRanks = CalculateRanks(_xValues.GetSpan());
var yRanks = CalculateRanks(_yValues.GetSpan());
// Store ranks in buffers for statistical calculations
_xRanks.Clear();
_yRanks.Clear();
for (int i = 0; i < xRanks.Length; i++)
{
_xRanks.Add(xRanks[i], true);
_yRanks.Add(yRanks[i], true);
}
// Use CircularBuffer's optimized Average() method
double xMean = _xRanks.Average();
double yMean = _yRanks.Average();
if (!double.IsNaN(xMean) && !double.IsNaN(yMean))
{
double covariance = CalculateCovariance(_xRanks, _yRanks, xMean, yMean);
double xStdDev = CalculateStandardDeviation(_xRanks, xMean);
double yStdDev = CalculateStandardDeviation(_yRanks, yMean);
if (!double.IsNaN(covariance) && xStdDev > Epsilon && yStdDev > Epsilon)
{
correlation = covariance / (xStdDev * yStdDev);
}
}
}
IsHot = _xValues.Count >= Period && _yValues.Count >= Period;
return correlation;
}
}
+25 -31
View File
@@ -1,32 +1,26 @@
# Statistics
# Statistics indicators
Done: 21, Todo: 2
Statistical functions and indicators for financial analysis.
## Implemented
- [Beta](Beta.cs) - Beta coefficient measuring volatility relative to market
- [Corr](Corr.cs) - Correlation coefficient between two series
- [Curvature](Curvature.cs) - Curvature of a time series
- [Entropy](Entropy.cs) - Information entropy of a series
- [Hurst](Hurst.cs) - Hurst exponent for trend strength
- [Kurtosis](Kurtosis.cs) - Kurtosis measuring tail extremity
- [Max](Max.cs) - Maximum value over period
- [Median](Median.cs) - Median value over period
- [Min](Min.cs) - Minimum value over period
- [Mode](Mode.cs) - Mode (most frequent value)
- [Percentile](Percentile.cs) - Percentile rank calculation
- [Skew](Skew.cs) - Skewness measuring distribution asymmetry
- [Slope](Slope.cs) - Linear regression slope
- [Stddev](Stddev.cs) - Standard deviation
- [Theil](Theil.cs) - Theil's U statistics for forecast accuracy
- [Tsf](Tsf.cs) - Time series forecast
- [Variance](Variance.cs) - Statistical variance
- [Zscore](Zscore.cs) - Z-score standardization
## Planned
- Cointegration - Test for cointegrated series
- Granger - Granger causality test
- Jarque-Bera - Normality test
- Kendall - Kendall rank correlation
- Spearman - Spearman rank correlation
✔️ BETA - Beta coefficient measuring volatility relative to market
✔️ CORR - Correlation coefficient between two series
✔️ COVAR - Covariance between two series
✔️ CURVATURE - Curvature of a time series
✔️ ENTROPY - Information entropy of a series
✔️ HURST - Hurst exponent for trend strength
✔️ KENDALL - Kendall rank correlation
✔️ KURTOSIS - Kurtosis measuring tail extremity
✔️ MAX - Maximum value over period
✔️ MEDIAN - Median value over period
✔️ MIN - Minimum value over period
✔️ MODE - Mode (most frequent value)
✔️ PERCENTILE - Percentile rank calculation
✔️ SKEW - Skewness measuring distribution asymmetry
✔️ SLOPE - Linear regression slope
✔️ SPEARMAN - Spearman rank correlation
✔️ STDDEV - Standard deviation
✔️ THEIL - Theil's U statistics for forecast accuracy
✔️ TSF - Time series forecast
✔️ VARIANCE - Statistical variance
✔️ ZSCORE - Z-score standardization
COINTEGRATION - Test for cointegrated series
GRANGER - Granger causality test