Compare commits
2 Commits
c713e3283b
...
e9bb795ecf
Author | SHA1 | Date | |
---|---|---|---|
e9bb795ecf | |||
569f20709b |
@ -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(
|
||||
|
@ -1,3 +1,5 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
import statistics
|
||||
from typing import Literal
|
||||
|
@ -485,7 +485,7 @@
|
||||
"source": [
|
||||
"import random\n",
|
||||
"import math\n",
|
||||
"import fincal as fc\n",
|
||||
"import pyfacts as pft\n",
|
||||
"from typing import List\n",
|
||||
"import datetime\n",
|
||||
"from dateutil.relativedelta import relativedelta"
|
||||
@ -536,7 +536,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"def sample_data_generator(\n",
|
||||
" frequency: fc.Frequency,\n",
|
||||
" frequency: pft.Frequency,\n",
|
||||
" num: int = 1000,\n",
|
||||
" skip_weekends: bool = False,\n",
|
||||
" mu: float = 0.1,\n",
|
||||
@ -571,11 +571,11 @@
|
||||
" start_date = datetime.datetime(2017, 1, 1)\n",
|
||||
" timedelta_dict = {\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",
|
||||
" 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",
|
||||
" ts = list(zip(dates, values))\n",
|
||||
" return ts\n"
|
||||
@ -583,40 +583,36 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"execution_count": 6,
|
||||
"id": "c85b5dd9-9a88-4608-ac58-1a141295f63f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"data = sample_data_generator(num=261, frequency=fc.AllFrequencies.W)\n",
|
||||
"ts = fc.TimeSeries(data, \"W\")"
|
||||
"market_data = sample_data_generator(num=3600, frequency=pft.AllFrequencies.D)\n",
|
||||
"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",
|
||||
"execution_count": 13,
|
||||
"execution_count": 8,
|
||||
"id": "0488a4d0-bca1-4341-9fae-1fd254adc0dc",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"TimeSeries([(datetime.datetime(2017, 1, 1, 0, 0), 1003.03),\n",
|
||||
"\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')"
|
||||
"1.020217253491451"
|
||||
]
|
||||
},
|
||||
"execution_count": 13,
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"ts"
|
||||
"pft.beta(sts, mts)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -708,7 +704,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.2"
|
||||
"version": "3.10.4"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user