defined custom covariance function to make it compatible with <3.10
replace statistics.coariance in statistics file
This commit is contained in:
parent
56baf83a77
commit
a395f7d98d
@ -8,7 +8,7 @@ from typing import Literal
|
|||||||
from pyfacts.core import date_parser
|
from pyfacts.core import date_parser
|
||||||
|
|
||||||
from .pyfacts import TimeSeries
|
from .pyfacts import TimeSeries
|
||||||
from .utils import _interval_to_years
|
from .utils import _interval_to_years, covariance
|
||||||
|
|
||||||
|
|
||||||
@date_parser(3, 4)
|
@date_parser(3, 4)
|
||||||
@ -212,7 +212,7 @@ def beta(
|
|||||||
asset_rr = asset_data.calculate_rolling_returns(**common_params)
|
asset_rr = asset_data.calculate_rolling_returns(**common_params)
|
||||||
market_rr = market_data.calculate_rolling_returns(**common_params)
|
market_rr = market_data.calculate_rolling_returns(**common_params)
|
||||||
|
|
||||||
cov = statistics.covariance(asset_rr.values, market_rr.values)
|
cov = covariance(asset_rr.values, market_rr.values)
|
||||||
market_var = statistics.variance(market_rr.values)
|
market_var = statistics.variance(market_rr.values)
|
||||||
|
|
||||||
beta = cov / market_var
|
beta = cov / market_var
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
|
import statistics
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import List, Literal, Mapping, Sequence, Tuple
|
from typing import List, Literal, Mapping, Sequence, Tuple
|
||||||
|
|
||||||
@ -187,3 +190,36 @@ def _is_eomonth(dates: Sequence[datetime.datetime], threshold: float = 0.7):
|
|||||||
eomonth_dates = [date.month != (date + relativedelta(days=1)).month for date in dates]
|
eomonth_dates = [date.month != (date + relativedelta(days=1)).month for date in dates]
|
||||||
eomonth_proportion = sum(eomonth_dates) / len(dates)
|
eomonth_proportion = sum(eomonth_dates) / len(dates)
|
||||||
return eomonth_proportion > threshold
|
return eomonth_proportion > threshold
|
||||||
|
|
||||||
|
|
||||||
|
def covariance(series1: list, series2: list) -> float:
|
||||||
|
"""Returns the covariance of two series
|
||||||
|
|
||||||
|
This is a compatibility function for Python versions prior to 3.10.
|
||||||
|
It will be replaced with statistics.covariance when support is dropped for versions <3.10.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
series1 : List
|
||||||
|
A list of numbers
|
||||||
|
series2 : list
|
||||||
|
A list of numbers
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
float
|
||||||
|
Returns the covariance as a float value
|
||||||
|
"""
|
||||||
|
|
||||||
|
n = len(series1)
|
||||||
|
if len(series2) != n:
|
||||||
|
raise ValueError("Lenght of both series must be same for covariance calcualtion.")
|
||||||
|
if n < 2:
|
||||||
|
raise ValueError("At least two data poitns are required for covariance calculation.")
|
||||||
|
|
||||||
|
mean1 = statistics.mean(series1)
|
||||||
|
mean2 = statistics.mean(series2)
|
||||||
|
|
||||||
|
xy = sum([(x - mean1) * (y - mean2) for x, y in zip(series1, series2)])
|
||||||
|
|
||||||
|
return xy / n
|
||||||
|
Loading…
Reference in New Issue
Block a user