2022-06-05 17:36:12 +00:00
|
|
|
import pyfacts as pft
|
2022-05-08 15:34:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_conf(conf_fun):
|
|
|
|
conf_add = conf_fun
|
|
|
|
assert conf_add(2, 4) == 6
|
|
|
|
|
|
|
|
|
|
|
|
class TestSharpe:
|
2022-05-10 04:21:12 +00:00
|
|
|
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(
|
2022-05-08 15:34:48 +00:00
|
|
|
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(
|
2022-05-08 15:34:48 +00:00
|
|
|
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(
|
2022-05-08 15:34:48 +00:00
|
|
|
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(
|
2022-05-08 15:34:48 +00:00
|
|
|
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
|
2022-05-10 04:21:12 +00:00
|
|
|
|
|
|
|
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(
|
2022-05-10 04:21:12 +00:00
|
|
|
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(
|
2022-05-10 04:21:12 +00:00
|
|
|
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
|
2022-06-06 03:03:58 +00:00
|
|
|
|
|
|
|
|
2022-06-12 16:05:13 +00:00
|
|
|
class TestSortino:
|
|
|
|
def test_sortino_daily_freq(self, create_test_data):
|
2022-06-26 15:10:03 +00:00
|
|
|
data = create_test_data(num=3600, frequency=pft.AllFrequencies.D, mu=0.12, sigma=0.12)
|
2022-06-12 16:05:13 +00:00
|
|
|
ts = pft.TimeSeries(data, "D")
|
|
|
|
sortino_ratio = pft.sortino_ratio(
|
|
|
|
ts,
|
2022-06-26 15:10:03 +00:00
|
|
|
risk_free_rate=0.06 / 12,
|
2022-06-12 16:05:13 +00:00
|
|
|
from_date="2017-02-02",
|
|
|
|
return_period_unit="months",
|
|
|
|
return_period_value=1,
|
|
|
|
)
|
2022-06-26 15:10:03 +00:00
|
|
|
assert round(sortino_ratio, 4) == 1.625
|
2022-06-12 16:05:13 +00:00
|
|
|
|
2022-06-26 15:10:03 +00:00
|
|
|
sortino_ratio = pft.sortino_ratio(
|
|
|
|
ts,
|
|
|
|
risk_free_rate=0.06,
|
|
|
|
from_date="2018-01-02",
|
|
|
|
return_period_unit="years",
|
|
|
|
return_period_value=1,
|
|
|
|
)
|
|
|
|
assert round(sortino_ratio, 4) == 1.2564
|
2022-06-12 16:05:13 +00:00
|
|
|
|
|
|
|
# def test_sharpe_weekly_freq(self, create_test_data):
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# sharpe_ratio = pft.sharpe_ratio(
|
|
|
|
# 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
|
|
|
|
|
|
|
|
|
2022-06-06 03:03:58 +00:00
|
|
|
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)
|
2022-06-25 07:49:26 +00:00
|
|
|
assert round(beta, 4) == 1.5997
|
2022-06-06 03:03:58 +00:00
|
|
|
|
|
|
|
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)
|
2022-06-25 07:49:26 +00:00
|
|
|
assert round(beta, 4) == 1.6287
|
2022-06-06 03:03:58 +00:00
|
|
|
|
|
|
|
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")
|
2022-06-26 15:10:03 +00:00
|
|
|
assert round(beta, 4) == 1.6131
|
2022-06-06 03:03:58 +00:00
|
|
|
|
|
|
|
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)
|
2022-06-25 07:49:26 +00:00
|
|
|
assert round(beta, 4) == 1.5887
|