Correlation Coefficient
get_correlation(quotes_a, quotes_b, lookback_periods)
Parameters
name | type | notes |
---|---|---|
quotes_a | Iterable[Quote] | Iterable of the Quote class or its sub-class. • See here for usage with pandas.DataFrame |
quotes_b | Iterable[Quote] | Historical quotes (B) must have at least the same matching date elements of quotes_a . |
lookback_periods | int | 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 quotes requirements
You must have at least N
periods for both versions of quotes
to cover the warmup periods. Mismatch histories will produce a InvalidQuotesException
. Historical price quotes should have a consistent frequency (day, hour, minute, etc).
quotes_a
is an Iterable[Quote]
collection of historical price quotes. It should have a consistent frequency (day, hour, minute, etc). See the Guide for more information.
Return
CorrelationResults[CorrelationResult]
- This method returns a time series of all available indicator values for the
quotes
provided. CorrelationResults
is just a list ofCorrelationResult
.- It always returns the same number of elements as there are in the historical quotes.
- It does not return a single incremental indicator value.
- The first
N-1
periods will haveNone
values since there’s not enough data to calculate.
CorrelationResult
name | type | notes |
---|---|---|
date | datetime | Date |
variance_a | float, Optional | Variance of A based on N lookback periods |
variance_b | float, Optional | Variance of B based on N lookback periods |
covariance | float, Optional | Covariance of A+B based on N lookback periods |
correlation | float, Optional | Correlation R based on N lookback periods |
r_squared | float, Optional | R-Squared (R²), aka Coefficient of Determination. Simple linear regression models is used (square of Correlation). |
Utilities
See Utilities and Helpers for more information.
Example
from stock_indicators import indicators
# This method is NOT a part of the library.
quotes_spx = get_historical_quotes("SPX")
quotes_tsla = get_historical_quotes("TSLA")
# Calculate 20-period Correlation
results = indicators.get_correlation(quotes_spx, quotes_tsla, 20)
About Correlation Coefficient
Correlation Coefficient between two quote histories, based on Close price. R-Squared (R²), Variance, and Covariance are also output. [Discuss] 💬