Skip to content
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
12 changes: 8 additions & 4 deletions pypfopt/cla.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,16 @@ def _compute_w(self, covarF_inv, covarFB, meanF, wB):
g1 = np.dot(np.dot(onesF.T, covarF_inv), meanF)
g2 = np.dot(np.dot(onesF.T, covarF_inv), onesF)
if wB is None:
g, w1 = float(-self.ls[-1] * g1 / g2 + 1 / g2), 0
g = -self.ls[-1] * g1 / g2 + 1 / g2
w1 = 0
else:
onesB = np.ones(wB.shape)
g3 = np.dot(onesB.T, wB)
g4 = np.dot(covarF_inv, covarFB)
w1 = np.dot(g4, wB)
g4 = np.dot(onesF.T, w1)
g = float(-self.ls[-1] * g1 / g2 + (1 - g3 + g4) / g2)
g = -self.ls[-1] * g1 / g2 + (1 - g3 + g4) / g2
g = float(g[0, 0])
# 2) compute weights
w2 = np.dot(covarF_inv, onesF)
w3 = np.dot(covarF_inv, meanF)
Expand All @@ -167,14 +169,16 @@ def _compute_lambda(self, covarF_inv, covarFB, meanF, wB, i, bi):
# 3) Lambda
if wB is None:
# All free assets
return float((c4[i] - c1 * bi) / c), bi
res = (c4[i] - c1 * bi) / c
else:
onesB = np.ones(wB.shape)
l1 = np.dot(onesB.T, wB)
l2 = np.dot(covarF_inv, covarFB)
l3 = np.dot(l2, wB)
l2 = np.dot(onesF.T, l3)
return float(((1 - l1 + l2) * c4[i] - c1 * (bi + l3[i])) / c), bi
res = ((1 - l1 + l2) * c4[i] - c1 * (bi + l3[i])) / c
res = float(res[0, 0])
return res, bi

def _get_matrices(self, f):
# Slice covarF,covarFB,covarB,meanF,meanB,wF,wB
Expand Down
7 changes: 6 additions & 1 deletion tests/test_efficient_cdar.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,14 @@ def test_cdar_example_weekly():


def test_cdar_example_monthly():
if _check_soft_dependencies("pandas<3", severity="none"):
MONTHLY_FREQ = "M"
else:
MONTHLY_FREQ = "ME"

beta = 0.90
df = get_data()
df = df.resample("M").first()
df = df.resample(MONTHLY_FREQ).first()
mu = expected_returns.mean_historical_return(df, frequency=12)
historical_rets = expected_returns.returns_from_prices(df).dropna()
cd = EfficientCDaR(mu, historical_rets, beta=beta)
Expand Down
7 changes: 6 additions & 1 deletion tests/test_efficient_cvar.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,14 @@ def test_cvar_example_weekly():


def test_cvar_example_monthly():
if _check_soft_dependencies("pandas<3", severity="none"):
MONTHLY_FREQ = "M"
else:
MONTHLY_FREQ = "ME"

beta = 0.95
df = get_data()
df = df.resample("M").first()
df = df.resample(MONTHLY_FREQ).first()
mu = expected_returns.mean_historical_return(df, frequency=12)
historical_rets = expected_returns.returns_from_prices(df).dropna()
cv = EfficientCVaR(mu, historical_rets, beta=beta)
Expand Down
7 changes: 6 additions & 1 deletion tests/test_efficient_semivariance.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,13 @@ def test_es_example_weekly():


def test_es_example_monthly():
if _check_soft_dependencies("pandas<3", severity="none"):
MONTHLY_FREQ = "M"
else:
MONTHLY_FREQ = "ME"

df = get_data()
df = df.resample("M").first()
df = df.resample(MONTHLY_FREQ).first()
mu = expected_returns.mean_historical_return(df, frequency=12)
historical_rets = expected_returns.returns_from_prices(df).dropna()
es = EfficientSemivariance(mu, historical_rets, frequency=12)
Expand Down