Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

18 synthetic returns #22

Merged
merged 5 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion experiments/backtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Callable
import numpy as np
import pandas as pd
from utils import synthetic_returns

# hack to allow importing from parent directory without having a package
sys.path.append(str(Path(__file__).parent.parent))
Expand Down Expand Up @@ -61,7 +62,7 @@ def run_backtest(
post_trade_cash = []
post_trade_quantities = []

returns = prices.pct_change().dropna()
returns = synthetic_returns(prices).dropna()
means = returns.ewm(halflife=125).mean()
covariance_df = returns.ewm(halflife=125).cov()
days = returns.index
Expand Down
31 changes: 12 additions & 19 deletions experiments/taming.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,28 @@
import matplotlib.pyplot as plt


def unconstrained_markowitz(inputs: OptimizationInput) -> np.ndarray:
def unconstrained_markowitz(
inputs: OptimizationInput, long_only: bool = False
) -> np.ndarray:
"""Compute the unconstrained Markowitz portfolio weights."""
n_assets = inputs.prices.shape[1]

mu, Sigma = inputs.mean.values, inputs.covariance.values

w = cp.Variable(n_assets)
c = cp.Variable()
if long_only:
w = cp.Variable(n_assets, nonneg=True)
c = cp.Variable(nonneg=True)
else:
w = cp.Variable(n_assets)
c = cp.Variable()
objective = mu @ w

chol = np.linalg.cholesky(Sigma)
constraints = [
cp.sum(w) + c == 1,
cp.norm2(chol @ w) <= inputs.risk_target,
cp.norm2(chol.T @ w) <= inputs.risk_target,
]

problem = cp.Problem(cp.Maximize(objective), constraints)
problem.solve(get_solver())
assert problem.status in {cp.OPTIMAL, cp.OPTIMAL_INACCURATE}
Expand All @@ -30,21 +37,7 @@ def unconstrained_markowitz(inputs: OptimizationInput) -> np.ndarray:

def long_only_markowitz(inputs: OptimizationInput) -> np.ndarray:
"""Compute the long-only Markowitz portfolio weights."""
n_assets = inputs.prices.shape[1]

mu, Sigma = inputs.mean.values, inputs.covariance.values

w = cp.Variable(n_assets, nonneg=True)
c = cp.Variable(nonneg=True)
objective = mu @ w
constraints = [
cp.sum(w) + c == 1,
cp.quad_form(w, Sigma, assume_PSD=True) <= inputs.risk_target**2,
]
problem = cp.Problem(cp.Maximize(objective), constraints)
problem.solve(get_solver())
assert problem.status in {cp.OPTIMAL, cp.OPTIMAL_INACCURATE}
return w.value, c.value
return unconstrained_markowitz(inputs, long_only=True)


def equal_weights(inputs: OptimizationInput) -> np.ndarray:
Expand Down
8 changes: 8 additions & 0 deletions experiments/utils.py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit puzzled by those coeffcients for sigma_r and sigma_eps

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aaah, sorry I will fix this...

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import numpy as np


def synthetic_returns(prices, sigma_r=0.02236, sigma_eps=0.14142):
returns = prices.pct_change()

alpha = sigma_r**2 / (sigma_r**2 + sigma_eps**2)
return alpha * (returns + np.random.normal(size=returns.shape) * sigma_eps)