statistics package¶
Submodules¶
statistics.VaR module¶
Value at risk functions here/ no need for class
-
quant_risk.statistics.VaR.conditional_value_at_risk(price: pandas.core.series.Series, threshold: float = 0.05) → float[source]¶ Calculates Conditional Value at Risk for given price series
- Parameters
price (pd.Series) – historical prices of a given security
- Returns
Conditional Value at Risk (VaR value) for given price
- Return type
float
-
quant_risk.statistics.VaR.value_at_risk(price: pandas.core.series.Series, threshold: float = 0.05) → float[source]¶ Calculates Value at Risk for given price series
- Parameters
price (pd.Series) – historical prices of a given security
- Returns
Value at Risk (VaR value) for given price
- Return type
float
statistics.annualize module¶
This file implements different functions for annualising volatility and returns from a given dataframe of returns
-
quant_risk.statistics.annualize.annualised_returns(returns: pandas.core.frame.DataFrame, periodsPerYear: int = 252)[source]¶ This function returns the annualised returns of a given dataframe of returns. If the freq of the data is not daily, the annualisation factor must be specified. The function returns nan if the value computed is too small
- Parameters
returns (pd.DataFrame) – dataframe of returns
periodsPerYear (int, optional) – freq of returns in a year, by default 252
- Returns
Returns the annualised return for each column in the dataframe
- Return type
Annualised Returns
-
quant_risk.statistics.annualize.annualised_volatility(returns: pandas.core.frame.DataFrame, periodsPerYear: int = 252)[source]¶ This function returns the annualised volatility of a given dataframe of returns. If the freq of the data is not daily, the annualisation factor must be specified
- Parameters
returns (pd.DataFrame) – dataframe of returns
periodsPerYear (int, optional) – freq of returns in a year, by default 252
- Returns
Returns the annualised volatility for each column in the dataframe
- Return type
Annualised Returns
statistics.financial_ratios module¶
Put all financial ratios here, no need for class I think
-
quant_risk.statistics.financial_ratios.calmar_ratio(price: Union[pandas.core.frame.DataFrame, pandas.core.series.Series], periodsPerYear: Union[float, int] = 252, riskFreeRate: float = 0.0) → float[source]¶ Calculates annualised calmar ratio for given set of prices and risk free rate
- Parameters
price (Union[pd.DataFrame, pd.Series]) – historical prices of a given security
periodsPerYear (Union[float, int]) – periodicity of the returns data for purposes of annualising
- Returns
annualised calmar ratio
- Return type
float
-
quant_risk.statistics.financial_ratios.omega_ratio(price: Union[pandas.core.frame.DataFrame, pandas.core.series.Series], riskFreeRate: float = 0.0, periodsPerYear: Union[float, int] = 252) → float[source]¶ Calculates annualised omega ratio for given set of prices and risk free rate
- Parameters
price (Union[pd.DataFrame, pd.Series]) – historical prices of a given security
riskFreeRate (float) – given constant risk free rate throughout the period
periodsPerYear (Union[float, int]) – periodicity of the returns data for purposes of annualising
- Returns
annualised omega ratio
- Return type
float
-
quant_risk.statistics.financial_ratios.sharpe_ratio(price: Union[pandas.core.frame.DataFrame, pandas.core.series.Series], riskFreeRate: float = 0.0, periodsPerYear: Union[float, int] = 252) → float[source]¶ Calculates annualised sharpe ratio for given set of prices and risk free rate
- Parameters
price (Union[pd.DataFrame, pd.Series]) – historical prices of a given security
riskFreeRate (float) – given constant risk free rate throughout the period
periodsPerYear (Union[float, int]) – periodicity of the returns data for purposes of annualising
- Returns
annualised sharpe ratio
- Return type
float
-
quant_risk.statistics.financial_ratios.sortino_ratio(price: Union[pandas.core.frame.DataFrame, pandas.core.series.Series], periodsPerYear: Union[float, int] = 252, reqReturn: float = 0) → float[source]¶ Calculates annualised sortino ratio for given set of prices and risk free rate
- Parameters
price (Union[pd.DataFrame, pd.Series]) – historical prices of a given security
periodsPerYear (Union[float, int]) – periodicity of the returns data for purposes of annualising
reqReturn (float, optional) – the minimum acceptable return by investors, by default 0
- Returns
annualised sortino ratio
- Return type
float
-
quant_risk.statistics.financial_ratios.tail_ratio(price: Union[pandas.core.frame.DataFrame, pandas.core.series.Series]) → float[source]¶ Calculates annualised tail ratio for given set of prices and risk free rate
- Parameters
price (Union[pd.DataFrame, pd.Series]) – historical prices of a given security
- Returns
annualised tail ratio
- Return type
float
statistics.statistics module¶
Put summary function here that prints or returns a dataframe
-
quant_risk.statistics.statistics.calculate_kurtosis(price: Union[pandas.core.series.Series, pandas.core.frame.DataFrame], test: bool = False, **kwargs) → Union[float, pandas.core.series.Series][source]¶ Calculates the kurtosis for a given set of prices
- Parameters
price (Union[pd.DataFrame,pd.Series]) – historical prices of a given security
- Returns
kurtosis for a given set of prices
- Return type
Union[float,pd.Series]
-
quant_risk.statistics.statistics.calculate_skewness(price: Union[pandas.core.frame.DataFrame, pandas.core.series.Series], test: bool = False, **kwargs) → Union[float, pandas.core.series.Series][source]¶ Calculates the skewness for a given set of prices
- Parameters
price (Union[pd.DataFrame,pd.Series]) – historical prices of a given security
- Returns
skewness for a given set of prices
- Return type
Union[float,pd.Series]
-
quant_risk.statistics.statistics.covariance_shrinkage(price: pandas.core.frame.DataFrame, delta: float = 0.5, **kwargs)[source]¶ This function computes the covariance matrix using the Ledoit-Wolf covariance shrinkage method taking a linear combination of the Constant Correlation matrix, acting as our prior and the Sample covariance matrix. The posterior covariance matrix is then computed.
- Parameters
price (pd.DataFrame) – Historical prices of a given security
delta (float, optional) – Constant by which to weigh the priori matrix, by default 0.5
- Returns
Returns a covariance matrix
- Return type
pd.DataFrame
-
quant_risk.statistics.statistics.cumulative_returns(price: Union[pandas.core.frame.DataFrame, pandas.core.series.Series]) → float[source]¶ Calculates cumulative returns for a given set of prices
- Parameters
price (Union[pd.DataFrame, pd.Series]) – historical prices of a given security
- Returns
cumulative returns for a given set of prices
- Return type
float
-
quant_risk.statistics.statistics.elton_gruber_covariance(price: pandas.core.frame.DataFrame, **kwargs)[source]¶ This function estimates the covariance matrix by assuming an implicit structure as defined by the Elton-Gruber Constant Correlation model.
- Parameters
price (pd.DataFrame) – Historical prices of a given security
- Returns
Returns a covariance matrix
- Return type
pd.DataFrame
-
quant_risk.statistics.statistics.is_stable(price: pandas.core.series.Series) → float[source]¶ Calculates stability for a given set of prices
- Parameters
price (pd.Series) – historical prices of a given security
- Returns
stability for a given set of prices
- Return type
float
-
quant_risk.statistics.statistics.maximum_drawdown(price: pandas.core.series.Series) → float[source]¶ Calculates maximum drawdown for a given set of prices
- Parameters
price (pd.Series) – historical prices of a given security
- Returns
maximum drawdown for a given set of prices
- Return type
float
-
quant_risk.statistics.statistics.risk_contribution(portfolioWeights: Union[numpy.array, pandas.core.frame.DataFrame], covarianceMatrix: pandas.core.frame.DataFrame)[source]¶ This function computes the contributions to the risk/variance of the constituents of a portfolio, given a set of portfolio weights and a covariance matrix
- Parameters
portfolioWeights (Union[np.array, pd.DataFrame]) – weights of our assets in our portfolio
covarianceMatrix (pd.DataFrame) – the covariance matrix of our assets computed by any method
- Returns
Returns the risk contribution of each asset
- Return type
pd.DataFrame
statistics.summarize module¶
-
quant_risk.statistics.summarize.print_summary(price: pandas.core.series.Series, **kwargs)[source]¶ This function returns a dataframe with the following characteristics: Financial Ratios: 1. Sharpe ratio 2. Sortino ratio 3. Calmar ratio 4. Omega ratio 5. Tail Ratio
Statistics: 1. Skewness 2. Kurtosis 3. Stability 4. Max Drawdown 5. Cumulative returns
Annualise: 1. Returns 2. Vol
Value at Risk 1. VaR 2. cVaR
statistics.tests module¶
Statistical tests
-
quant_risk.statistics.tests.ACF(series: pandas.core.series.Series, adjusted: bool = False, nLags: int = 20, qStat: bool = False, fft: bool = True, alpha: float = None, missing: str = 'none', plot: bool = True) → Union[numpy.ndarray, tuple][source]¶ Calculates the ACF, and optionally the confidence intervals, Ljung-Box Q-Statistic, and its associated p-values for a given series Check documentation here: https://www.statsmodels.org/stable/generated/statsmodels.tsa.stattools.acf.html#statsmodels.tsa.stattools.acf Note: series is only for one security
- Parameters
series (pd.Series) – time series data
adjusted (bool, optional) – If True, then denominators for autocovariance are n-k, otherwise n, by default False
nLags (int, optional) – Number of lags to return autocorrelation for, by default None
qStat (bool, optional) – If True, returns the Ljung-Box q statistic for each autocorrelation coefficient, by default False
fft (bool, optional) – If True, computes the ACF via FFT, by default None
alpha (float, optional) – If a number is given, the confidence intervals for the given level are returned, by default None
missing (str, optional) – A string in [“none”, “raise”, “conservative”, “drop”] specifying how the NaNs are to be treated, by default ‘none’
- Returns
- Returns the autocorrelation function of type np.ndarray, and
Confidence intervals for the ACF, if alpha is not None, of type np.ndarray The Ljung-Box Q-Statistic, if qStat is True, of type np.ndarray The p-values associated with the Q-statistics, if qStat is True, of type np.ndarray
- Return type
Union[np.ndarray,tuple]
-
quant_risk.statistics.tests.PACF(series: pandas.core.series.Series, nLags: int = 20, method: str = 'ywadjusted', alpha: float = None, plot: bool = True) → Union[numpy.ndarray, tuple][source]¶ Calculates the PACF, and optionally the confidence intervals, for the returns of a given series Documentation: https://www.statsmodels.org/stable/generated/statsmodels.tsa.stattools.pacf.html#statsmodels.tsa.stattools.pacf Note: series is only for one security
- Parameters
series (pd.Series) – time series data
nLags (int, optional) – The largest lag for which the PACF is returned, by default None
method (str, optional) – Specifies which method for the calculations to use, full list in documentation, by default ‘ywadjusted’
alpha (float, optional) – If a number is given, the confidence intervals for the given level are returned, by default None
- Returns
- Partial autocorrelations, nlags elements, including lag zero, of type np.ndarray and
Confidence intervals for the PACF if alpha is not None, of type np.ndarray
- Return type
Union[np.ndarray,tuple]
-
quant_risk.statistics.tests.granger_causality(series: pandas.core.frame.DataFrame, maxLags: Union[int, list], addConst: bool = True, verbose: bool = True, testToUse: str = 'ssr_ftest') → dict[source]¶ Performs the Granger Causality Test for the given series Note: pd.DataFrame should contain two columns Note: series data must be stationary, difference before passing if needed
- Parameters
series (pd.DataFrame) – data for testing whether the time series in the second column Granger causes the time series in the first column (missing values not supported)
maxLags (int) – If an integer, computes the test for all lags up to maxlag. If a list, computes the tests only for the lags in maxlag
addConst (bool) – Add a constant to the model, by default True
verbose (bool, optional) – True if debugging information is to be printed, by default True
- Returns
- All test results, dictionary keys are the number of lags. For each lag the values are a tuple,
First element: a dictionary with test statistic, p-values, degrees of freedom, keys: ‘lrtest’, ‘params_ftest’, ‘ssr_chi2test’, ‘ssr_ftest’ Second element: the OLS estimation results for the restricted model, the unrestricted model and the restriction (contrast) matrix for the parameter f_test For example: to get p-value for ssr_ftest for ith lag: res[i][0][‘ssr_ftest’][1]
- Return type
dict
-
quant_risk.statistics.tests.granger_causality_matrix(data: pandas.core.frame.DataFrame, testToUse: str = 'ssr_ftest', verbose: bool = False, maxlag: int = 10)[source]¶ The function returns a NxN matrix where N is the number of columns in our time series dataframe(should be the same as the number of variables in variables). The matrix is just the minimum p-value of the Johansen Cointegration test that is performed for each lag till maxlag for each series pair. The function also returns a dataframe that contains the lag value where the minimum pvalue was found. The variables in the columns are the predictors and the variables in the rows are reponses. The value in each cell of the matrix can be interpreted as the whether we can assume(<0.05) if our column causes our row variable.
- Parameters
data (pd.DataFrame) –
of Multivariate time series (Dataframe) –
testToUse (str, optional) – Which test statistic to use for our Granger Causality test, by default ‘ssr_ftest’
verbose (boolean, optional) – Should the computation be shown for each lag value for each pair computed, by default False
maxlag (int, optional) – The maximum lag that the test checks causality for, by default 6
- Returns
Returns two dataframes that contain the pvalues and the value of the lag at which the minimum pvalue was found.
- Return type
[pd.DataFrame, pd.DataFrame]
-
quant_risk.statistics.tests.hurst_exponent(series: pandas.core.series.Series, maxlag: int) → float[source]¶ Returns the Hurst Exponent value for a given time series Source: https://towardsdatascience.com/introduction-to-the-hurst-exponent-with-code-in-python-4da0414ca52e
- Parameters
series (pd.Series) – time series
maxLags (int) – maximum number of lags
- Returns
Hurst Exponent
- Return type
float
-
quant_risk.statistics.tests.stationary_test_adf(series: pandas.core.series.Series, verbose: bool = True, stationaritySignifiance: float = 0.05) → tuple[source]¶ Runs the Augmented Dickey-Fuller test on the series, with the Null Hypothesis of non-stationarity i.e data has a unit root
- Parameters
series (pd.Series) – Time series data that we want to test for stationarity
verbose (bool, optional) – True if the ADF statistic, p-value and critical values are to be printed, by default True
stationaritySignificance (float, optional) – The level of signifiance at which stationarity is checked, by default 0.05 (5%)
- Returns
Returns the relevant values in the format (p-value, ADF statistic, stationaryBool)
- Return type
tuple