PyFacts/tests/test_stats.py

85 lines
2.5 KiB
Python
Raw Normal View History

2022-06-05 17:36:12 +00:00
import pyfacts as pft
def test_conf(conf_fun):
conf_add = conf_fun
assert conf_add(2, 4) == 6
class TestSharpe:
def test_sharpe_daily_freq(self, create_test_data):
2022-06-05 17:36:12 +00:00
data = create_test_data(num=1305, frequency=pft.AllFrequencies.D, skip_weekends=True)
ts = pft.TimeSeries(data, "D")
sharpe_ratio = pft.sharpe_ratio(
ts,
risk_free_rate=0.06,
from_date="2017-02-04",
to_date="2021-12-31",
return_period_unit="months",
return_period_value=1,
)
assert round(sharpe_ratio, 4) == 1.0502
2022-06-05 17:36:12 +00:00
sharpe_ratio = pft.sharpe_ratio(
ts,
risk_free_rate=0.06,
from_date="2017-01-09",
to_date="2021-12-31",
return_period_unit="days",
return_period_value=7,
)
assert round(sharpe_ratio, 4) == 1.0701
2022-06-05 17:36:12 +00:00
sharpe_ratio = pft.sharpe_ratio(
ts,
risk_free_rate=0.06,
from_date="2018-01-02",
to_date="2021-12-31",
return_period_unit="years",
return_period_value=1,
)
assert round(sharpe_ratio, 4) == 1.4374
2022-06-05 17:36:12 +00:00
sharpe_ratio = pft.sharpe_ratio(
ts,
risk_free_rate=0.06,
from_date="2017-07-03",
to_date="2021-12-31",
return_period_unit="months",
return_period_value=6,
)
assert round(sharpe_ratio, 4) == 0.8401
def test_sharpe_weekly_freq(self, create_test_data):
2022-06-05 17:36:12 +00:00
data = create_test_data(num=261, frequency=pft.AllFrequencies.W, mu=0.6, sigma=0.7)
ts = pft.TimeSeries(data, "W")
sharpe_ratio = pft.sharpe_ratio(
ts,
risk_free_rate=0.052,
from_date="2017-01-08",
to_date="2021-12-31",
return_period_unit="days",
return_period_value=7,
)
assert round(sharpe_ratio, 4) == 0.4533
2022-06-05 17:36:12 +00:00
sharpe_ratio = pft.sharpe_ratio(
ts,
risk_free_rate=0.052,
from_date="2017-02-05",
to_date="2021-12-31",
return_period_unit="months",
return_period_value=1,
)
assert round(sharpe_ratio, 4) == 0.4898
2022-05-16 16:59:18 +00:00
2022-06-05 17:36:12 +00:00
sharpe_ratio = pft.sharpe_ratio(
2022-05-16 16:59:18 +00:00
ts,
risk_free_rate=0.052,
from_date="2018-01-01",
to_date="2021-12-31",
return_period_unit="months",
return_period_value=12,
)
assert round(sharpe_ratio, 4) == 0.3199