def test_cov_to_corr(): df = get_data() rets = risk_models.returns_from_prices(df).dropna() test_corr = risk_models.cov_to_corr(rets.cov()) pd.testing.assert_frame_equal(test_corr, rets.corr()) with pytest.warns(RuntimeWarning) as w: test_corr_numpy = risk_models.cov_to_corr(rets.cov().values) assert len(w) == 1 assert str(w[0].message) == "cov_matrix is not a dataframe" np.testing.assert_array_almost_equal(test_corr_numpy, rets.corr().values)
def test_corr_to_cov(): df = get_data() rets = risk_models.returns_from_prices(df).dropna() test_corr = risk_models.cov_to_corr(rets.cov()) new_cov = risk_models.corr_to_cov(test_corr, rets.std()) pd.testing.assert_frame_equal(new_cov, rets.cov()) with pytest.warns(RuntimeWarning) as w: cov_numpy = risk_models.corr_to_cov(test_corr.to_numpy(), rets.std()) assert len(w) == 1 assert str(w[0].message) == "corr_matrix is not a dataframe" assert isinstance(cov_numpy, pd.DataFrame) np.testing.assert_equal(cov_numpy.to_numpy(), new_cov.to_numpy())
def test_corr_to_cov(): df = get_data() rets = risk_models.returns_from_prices(df).dropna() test_corr = risk_models.cov_to_corr(rets.cov()) new_cov = risk_models.corr_to_cov(test_corr, rets.std()) pd.testing.assert_frame_equal(new_cov, rets.cov())
'secret': '', 'enableRateLimit': True }) ohlcv = exchange.fetch_ohlcv(i, timeframe, limit=limit) ohlcv = exchange.convert_ohlcv_to_trading_view(ohlcv) df = pd.DataFrame(ohlcv) df.t = df.t.apply(lambda x: datetime.datetime.fromtimestamp(x)) df = df.dropna() df = df.set_index(df['t']) df = df.drop(['t'], axis=1) dataset = df dataset = dataset.dropna() data_[i] = dataset.c data_.dropna(axis=1, inplace=True) returns = risk_models.returns_from_prices(data_, log_returns=True) S = CovarianceShrinkage(data_, frequency=180).ledoit_wolf() hrp = HRPOpt(returns, cov_matrix=S) # weights = hrp.optimize() # hrp.portfolio_performance(verbose=True); weights = {w: 1 / len(pair) for w in pair} hrp.set_weights(weights) w = hrp.portfolio_performance(verbose=True, frequency=180) st.write("Expected annual return: {:.2f}%".format(w[0] * 100)) st.write("Annual volatility: {:.2f}%".format(w[1] * 100)) st.write("Sharpe Ratio: {:.2f}".format(w[2])) returns = risk_models.returns_from_prices(data_, log_returns=True) returns["sum"] = returns.sum(axis=1)