Correlation coefficient
Created by Karl Pearson, the Correlation coefficient depicts the linear statistical correlation between two price bar histories; includes R-squared (R²) / coefficient of determination, variance, and covariance. [Discuss] 💬
// C# usage syntax
IReadOnlyList<CorrResult> results =
sourceA.ToCorrelation(sourceB, lookbackPeriods);Parameters
| param | description |
|---|---|
sourceAIReadOnlyList<IReusable> | First source series (A), such as an indicator's results or .Use() output. It must have at least the same matching date elements of sourceB. |
sourceBIReadOnlyList<IReusable> | Second source series (B), such as an indicator's results or .Use() output. It must have at least the same matching date elements of sourceA. |
lookbackPeriodsint | Number of periods (N) in the lookback period. Must be greater than 0 to calculate; however we suggest a larger period for statistically appropriate sample size. |
Historical price bars requirements
You must have at least N periods for both sourceA and sourceB to cover the warmup periods. More than the minimum is typically specified.
sourceA and sourceB must have consistent frequency (day, hour, minute, etc). Mismatch histories will throw InvalidBarsException. See the Guide for more information.
Response
IReadOnlyList<CorrResult>- This method returns a time series of all available indicator values for the
sourceAandsourceBprovided. - It always returns the same number of elements as there are in the historical price bars.
- It does not return a single incremental indicator value.
- The first
N-1periods will havenullvalues since there's not enough data to calculate.
CorrResult
| property | description |
|---|---|
TimestampDateTime | Date from evaluated TBar |
VarianceAdouble? | Variance of A |
VarianceBdouble? | Variance of B |
Covariancedouble? | Covariance of A+B |
Correlationdouble? | Correlation R |
RSquareddouble? | R-squared (R²), aka Coefficient of determination |
Utilities
See Utilities and helpers for more information.
Chaining
This indicator may be generated from any chain-enabled indicator or method.
// example
var results = bars
.Use(CandlePart.HL2)
.ToCorrelation(barsMarket.Use(CandlePart.HL2),20);🚩 Matching inputs required
Both sourceA and sourceB arguments must contain the same number of elements and be the results of a chainable indicator or .Use() method.
Results can be further processed on Correlation with additional chain-enabled indicators.
// example
var results = bars
.ToCorrelation(..)
.ToSlope(..);See Chaining indicators for more.
Streaming
Streaming is not supported for this indicator. This indicator requires a second synchronized bar series, which cannot be expressed in the single-series streaming model. Use the Series (batch) implementation with periodic recalculation instead.