From e9bb795ecf052d6eba2cad2950b9e633f19bb48b Mon Sep 17 00:00:00 2001 From: gouravkr Date: Mon, 6 Jun 2022 08:33:58 +0530 Subject: [PATCH] Tests for beta and bug fixes --- pyfacts/pyfacts.py | 4 ++-- pyfacts/statistics.py | 2 ++ tests/test_stats.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/pyfacts/pyfacts.py b/pyfacts/pyfacts.py index 49d3633..12a8306 100644 --- a/pyfacts/pyfacts.py +++ b/pyfacts/pyfacts.py @@ -140,7 +140,7 @@ class TimeSeries(TimeSeriesCore): self, data: List[Iterable] | Mapping, frequency: Literal["D", "W", "M", "Q", "H", "Y"] = None, - validate_frequency: bool = False, + validate_frequency: bool = True, date_format: str = "%Y-%m-%d", ): """Instantiate a TimeSeriesCore object""" @@ -448,7 +448,7 @@ class TimeSeries(TimeSeriesCore): ) rolling_returns.append(returns) rolling_returns.sort() - return self.__class__(rolling_returns, self.frequency.symbol) + return self.__class__(rolling_returns, frequency.symbol) @date_parser(1, 2) def volatility( diff --git a/pyfacts/statistics.py b/pyfacts/statistics.py index 6ef3f5e..d5e092f 100644 --- a/pyfacts/statistics.py +++ b/pyfacts/statistics.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import datetime import statistics from typing import Literal diff --git a/tests/test_stats.py b/tests/test_stats.py index 7eee9b0..9ad1a61 100644 --- a/tests/test_stats.py +++ b/tests/test_stats.py @@ -82,3 +82,37 @@ class TestSharpe: return_period_value=12, ) assert round(sharpe_ratio, 4) == 0.3199 + + +class TestBeta: + def test_beta_daily_freq(self, create_test_data): + market_data = create_test_data(num=3600, frequency=pft.AllFrequencies.D) + stock_data = create_test_data(num=3600, frequency=pft.AllFrequencies.D, mu=0.12, sigma=0.08) + sts = pft.TimeSeries(stock_data, "D") + mts = pft.TimeSeries(market_data, "D") + beta = pft.beta(sts, mts, frequency="D", return_period_unit="days", return_period_value=1) + assert round(beta, 4) == 1.6001 + + def test_beta_daily_freq_daily_returns(self, create_test_data): + market_data = create_test_data(num=3600, frequency=pft.AllFrequencies.D) + stock_data = create_test_data(num=3600, frequency=pft.AllFrequencies.D, mu=0.12, sigma=0.08) + sts = pft.TimeSeries(stock_data, "D") + mts = pft.TimeSeries(market_data, "D") + beta = pft.beta(sts, mts) + assert round(beta, 4) == 1.6292 + + def test_beta_monthly_freq(self, create_test_data): + market_data = create_test_data(num=3600, frequency=pft.AllFrequencies.D) + stock_data = create_test_data(num=3600, frequency=pft.AllFrequencies.D, mu=0.12, sigma=0.08) + sts = pft.TimeSeries(stock_data, "D") + mts = pft.TimeSeries(market_data, "D") + beta = pft.beta(sts, mts, frequency="M") + assert round(beta, 4) == 1.629 + + def test_beta_monthly_freq_monthly_returns(self, create_test_data): + market_data = create_test_data(num=3600, frequency=pft.AllFrequencies.D) + stock_data = create_test_data(num=3600, frequency=pft.AllFrequencies.D, mu=0.12, sigma=0.08) + sts = pft.TimeSeries(stock_data, "D") + mts = pft.TimeSeries(market_data, "D") + beta = pft.beta(sts, mts, frequency="M", return_period_unit="months", return_period_value=1) + assert round(beta, 4) == 1.6023