Compare commits

...

2 Commits

Author SHA1 Message Date
e9bb795ecf Tests for beta and bug fixes 2022-06-06 08:33:58 +05:30
569f20709b Updated test file 2022-06-06 08:17:12 +05:30
4 changed files with 52 additions and 20 deletions

View File

@ -140,7 +140,7 @@ class TimeSeries(TimeSeriesCore):
self, self,
data: List[Iterable] | Mapping, data: List[Iterable] | Mapping,
frequency: Literal["D", "W", "M", "Q", "H", "Y"] = None, frequency: Literal["D", "W", "M", "Q", "H", "Y"] = None,
validate_frequency: bool = False, validate_frequency: bool = True,
date_format: str = "%Y-%m-%d", date_format: str = "%Y-%m-%d",
): ):
"""Instantiate a TimeSeriesCore object""" """Instantiate a TimeSeriesCore object"""
@ -448,7 +448,7 @@ class TimeSeries(TimeSeriesCore):
) )
rolling_returns.append(returns) rolling_returns.append(returns)
rolling_returns.sort() rolling_returns.sort()
return self.__class__(rolling_returns, self.frequency.symbol) return self.__class__(rolling_returns, frequency.symbol)
@date_parser(1, 2) @date_parser(1, 2)
def volatility( def volatility(

View File

@ -1,3 +1,5 @@
from __future__ import annotations
import datetime import datetime
import statistics import statistics
from typing import Literal from typing import Literal

View File

@ -485,7 +485,7 @@
"source": [ "source": [
"import random\n", "import random\n",
"import math\n", "import math\n",
"import fincal as fc\n", "import pyfacts as pft\n",
"from typing import List\n", "from typing import List\n",
"import datetime\n", "import datetime\n",
"from dateutil.relativedelta import relativedelta" "from dateutil.relativedelta import relativedelta"
@ -536,7 +536,7 @@
"\n", "\n",
"\n", "\n",
"def sample_data_generator(\n", "def sample_data_generator(\n",
" frequency: fc.Frequency,\n", " frequency: pft.Frequency,\n",
" num: int = 1000,\n", " num: int = 1000,\n",
" skip_weekends: bool = False,\n", " skip_weekends: bool = False,\n",
" mu: float = 0.1,\n", " mu: float = 0.1,\n",
@ -571,11 +571,11 @@
" start_date = datetime.datetime(2017, 1, 1)\n", " start_date = datetime.datetime(2017, 1, 1)\n",
" timedelta_dict = {\n", " timedelta_dict = {\n",
" frequency.freq_type: int(\n", " frequency.freq_type: int(\n",
" frequency.value * num * (7 / 5 if frequency == fc.AllFrequencies.D and skip_weekends else 1)\n", " frequency.value * num * (7 / 5 if frequency == pft.AllFrequencies.D and skip_weekends else 1)\n",
" )\n", " )\n",
" }\n", " }\n",
" end_date = start_date + relativedelta(**timedelta_dict)\n", " end_date = start_date + relativedelta(**timedelta_dict)\n",
" dates = fc.create_date_series(start_date, end_date, frequency.symbol, skip_weekends=skip_weekends, eomonth=eomonth)\n", " dates = pft.create_date_series(start_date, end_date, frequency.symbol, skip_weekends=skip_weekends, eomonth=eomonth)\n",
" values = create_prices(1000, mu, sigma, num)\n", " values = create_prices(1000, mu, sigma, num)\n",
" ts = list(zip(dates, values))\n", " ts = list(zip(dates, values))\n",
" return ts\n" " return ts\n"
@ -583,40 +583,36 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 6,
"id": "c85b5dd9-9a88-4608-ac58-1a141295f63f", "id": "c85b5dd9-9a88-4608-ac58-1a141295f63f",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"data = sample_data_generator(num=261, frequency=fc.AllFrequencies.W)\n", "market_data = sample_data_generator(num=3600, frequency=pft.AllFrequencies.D)\n",
"ts = fc.TimeSeries(data, \"W\")" "mts = pft.TimeSeries(market_data, \"D\")\n",
"stock_data = sample_data_generator(num=3600, frequency=pft.AllFrequencies.D, mu=0.12, sigma=0.05)\n",
"sts = pft.TimeSeries(stock_data, 'D')"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 8,
"id": "0488a4d0-bca1-4341-9fae-1fd254adc0dc", "id": "0488a4d0-bca1-4341-9fae-1fd254adc0dc",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"TimeSeries([(datetime.datetime(2017, 1, 1, 0, 0), 1003.03),\n", "1.020217253491451"
"\t (datetime.datetime(2017, 1, 8, 0, 0), 1002.71),\n",
"\t (datetime.datetime(2017, 1, 15, 0, 0), 1008.77)\n",
"\t ...\n",
"\t (datetime.datetime(2021, 12, 12, 0, 0), 1107.21),\n",
"\t (datetime.datetime(2021, 12, 19, 0, 0), 1106.66),\n",
"\t (datetime.datetime(2021, 12, 26, 0, 0), 1104.32)], frequency='W')"
] ]
}, },
"execution_count": 13, "execution_count": 8,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"source": [ "source": [
"ts" "pft.beta(sts, mts)"
] ]
}, },
{ {
@ -708,7 +704,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.10.2" "version": "3.10.4"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -82,3 +82,37 @@ class TestSharpe:
return_period_value=12, return_period_value=12,
) )
assert round(sharpe_ratio, 4) == 0.3199 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