Skip to content

Commit

Permalink
fixed smooth weights tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperjo committed Mar 28, 2024
1 parent 2613de2 commit 1a78e22
Show file tree
Hide file tree
Showing 8 changed files with 1,010 additions and 1,004 deletions.
11 changes: 7 additions & 4 deletions tests/generate_smooth_weight_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from cvx.covariance.combination import from_ewmas

save_data = True
save_data = False

# import test data
resource_dir = Path(__file__).parent / "resources"
Expand All @@ -23,7 +23,7 @@
half_life_pairs = [(10, 21), (21, 63)]

iterator_l2 = from_ewmas(returns, half_life_pairs).solve(
window=5, smoother="l2", gamma=1
window=5, smoother="l2", gamma=1, solver="CLARABEL"
)
covariances_l2 = {}
weights_l2 = {}
Expand All @@ -34,7 +34,7 @@
weights_l2 = pd.DataFrame(weights_l2).T

iterator_l1 = from_ewmas(returns, half_life_pairs).solve(
window=5, smoother="l1", gamma=1
window=5, smoother="l1", gamma=1, solver="CLARABEL"
)
covariances_l1 = {}
weights_l1 = {}
Expand All @@ -45,7 +45,7 @@
weights_l1 = pd.DataFrame(weights_l1).T

iterator_sum_squares = from_ewmas(returns, half_life_pairs).solve(
window=5, smoother="sum_squares", gamma=1
window=5, smoother="sum_squares", gamma=1, solver="CLARABEL"
)
covariances_sum_squares = {}
weights_sum_squares = {}
Expand All @@ -57,9 +57,12 @@
weights_sum_squares = pd.DataFrame(weights_sum_squares).T

if save_data:
print("Saving data")
cov_l2.to_csv(resource_dir / "cov_smooth_weights_l2.csv")
cov_l1.to_csv(resource_dir / "cov_smooth_weights_l1.csv")
cov_sum_squares.to_csv(resource_dir / "cov_smooth_weights_sum_squares.csv")
weights_l2.to_csv(resource_dir / "weights_smooth_weights_l2.csv")
weights_l1.to_csv(resource_dir / "weights_smooth_weights_l1.csv")
weights_sum_squares.to_csv(resource_dir / "weights_smooth_weights_sum_squares.csv")
else:
print("Not saving data")
40 changes: 20 additions & 20 deletions tests/resources/cov_smooth_weights_l1.csv

Large diffs are not rendered by default.

40 changes: 20 additions & 20 deletions tests/resources/cov_smooth_weights_l2.csv

Large diffs are not rendered by default.

40 changes: 20 additions & 20 deletions tests/resources/cov_smooth_weights_sum_squares.csv

Large diffs are not rendered by default.

552 changes: 276 additions & 276 deletions tests/resources/weights_smooth_weights_l1.csv

Large diffs are not rendered by default.

552 changes: 276 additions & 276 deletions tests/resources/weights_smooth_weights_l2.csv

Large diffs are not rendered by default.

552 changes: 276 additions & 276 deletions tests/resources/weights_smooth_weights_sum_squares.csv

Large diffs are not rendered by default.

227 changes: 115 additions & 112 deletions tests/test_smooth_weights.py
Original file line number Diff line number Diff line change
@@ -1,112 +1,115 @@
# from __future__ import annotations

# import pandas as pd
# import pytest

# from cvx.covariance.combination import from_ewmas


# @pytest.fixture()
# def returns(resource_dir):
# """
# prices fixture
# """
# return (
# pd.read_csv(
# resource_dir / "stock_prices.csv", index_col=0, header=0, parse_dates=True
# )
# .ffill()
# .pct_change()
# .iloc[1:]
# )


# @pytest.fixture()
# def time_stamp():
# return pd.Timestamp("2017-11-15")


# @pytest.fixture()
# def half_life_pairs():
# return [(10, 21), (21, 63)]


# def test_l2(returns, resource_dir, time_stamp, half_life_pairs):
# iterator = from_ewmas(returns, half_life_pairs).solve(
# window=5, smoother="l2", gamma=1
# )

# covariances = {}
# weights = {}
# for iterate in iterator:
# covariances[iterate.time] = iterate.covariance
# weights[iterate.time] = iterate.weights
# cov = covariances[time_stamp]
# weights = pd.DataFrame(weights).T

# cov_reference = pd.read_csv(
# resource_dir / "cov_smooth_weights_l2.csv", index_col=0, header=0
# )
# weights_reference = pd.read_csv(
# resource_dir / "weights_smooth_weights_l2.csv",
# index_col=0,
# header=0,
# parse_dates=True,
# )

# pd.testing.assert_frame_equal(cov, cov_reference)
# pd.testing.assert_frame_equal(weights, weights_reference)


# def test_l1(returns, resource_dir, time_stamp, half_life_pairs):
# iterator = from_ewmas(returns, half_life_pairs).solve(
# window=5, smoother="l1", gamma=1
# )

# covariances = {}
# weights = {}
# for iterate in iterator:
# covariances[iterate.time] = iterate.covariance
# weights[iterate.time] = iterate.weights
# cov = covariances[time_stamp]
# weights = pd.DataFrame(weights).T

# cov_reference = pd.read_csv(
# resource_dir / "cov_smooth_weights_l1.csv", index_col=0, header=0
# )
# weights_reference = pd.read_csv(
# resource_dir / "weights_smooth_weights_l1.csv",
# index_col=0,
# header=0,
# parse_dates=True,
# )

# pd.testing.assert_frame_equal(cov, cov_reference)
# pd.testing.assert_frame_equal(weights, weights_reference)


# def test_sum_squares(returns, resource_dir, time_stamp, half_life_pairs):
# iterator = from_ewmas(returns, half_life_pairs).solve(
# window=5, smoother="sum_squares", gamma=1
# )

# covariances = {}
# weights = {}
# for iterate in iterator:
# covariances[iterate.time] = iterate.covariance
# weights[iterate.time] = iterate.weights
# cov = covariances[time_stamp]
# weights = pd.DataFrame(weights).T

# cov_reference = pd.read_csv(
# resource_dir / "cov_smooth_weights_sum_squares.csv", index_col=0, header=0
# )
# weights_reference = pd.read_csv(
# resource_dir / "weights_smooth_weights_sum_squares.csv",
# index_col=0,
# header=0,
# parse_dates=True,
# )

# pd.testing.assert_frame_equal(cov, cov_reference)
# pd.testing.assert_frame_equal(weights, weights_reference)
from __future__ import annotations

import pandas as pd
import pytest

from cvx.covariance.combination import from_ewmas


@pytest.fixture()
def returns(resource_dir):
"""
prices fixture
"""
return (
pd.read_csv(
resource_dir / "stock_prices.csv", index_col=0, header=0, parse_dates=True
)
.ffill()
.pct_change()
.iloc[1:]
)


@pytest.fixture()
def time_stamp():
return pd.Timestamp("2017-11-15")


@pytest.fixture()
def half_life_pairs():
return [(10, 21), (21, 63)]


def test_l2(returns, resource_dir, time_stamp, half_life_pairs):
iterator = from_ewmas(returns, half_life_pairs).solve(
window=5, smoother="l2", gamma=1
)

covariances = {}
weights = {}
for iterate in iterator:
covariances[iterate.time] = iterate.covariance
weights[iterate.time] = iterate.weights
cov = covariances[time_stamp]
weights = pd.DataFrame(weights).T

cov_reference = pd.read_csv(
resource_dir / "cov_smooth_weights_l2.csv", index_col=0, header=0
)
weights_reference = pd.read_csv(
resource_dir / "weights_smooth_weights_l2.csv",
index_col=0,
header=0,
parse_dates=True,
)

print(weights.iloc[:, 0].name)
print(weights_reference.iloc[:, 0].name)

pd.testing.assert_frame_equal(cov, cov_reference, atol=1e-3)
pd.testing.assert_frame_equal(weights, weights_reference, atol=1e-3)


def test_l1(returns, resource_dir, time_stamp, half_life_pairs):
iterator = from_ewmas(returns, half_life_pairs).solve(
window=5, smoother="l1", gamma=1
)

covariances = {}
weights = {}
for iterate in iterator:
covariances[iterate.time] = iterate.covariance
weights[iterate.time] = iterate.weights
cov = covariances[time_stamp]
weights = pd.DataFrame(weights).T

cov_reference = pd.read_csv(
resource_dir / "cov_smooth_weights_l1.csv", index_col=0, header=0
)
weights_reference = pd.read_csv(
resource_dir / "weights_smooth_weights_l1.csv",
index_col=0,
header=0,
parse_dates=True,
)

pd.testing.assert_frame_equal(cov, cov_reference, atol=1e-3)
pd.testing.assert_frame_equal(weights, weights_reference, atol=1e-3)


def test_sum_squares(returns, resource_dir, time_stamp, half_life_pairs):
iterator = from_ewmas(returns, half_life_pairs).solve(
window=5, smoother="sum_squares", gamma=1
)

covariances = {}
weights = {}
for iterate in iterator:
covariances[iterate.time] = iterate.covariance
weights[iterate.time] = iterate.weights
cov = covariances[time_stamp]
weights = pd.DataFrame(weights).T

cov_reference = pd.read_csv(
resource_dir / "cov_smooth_weights_sum_squares.csv", index_col=0, header=0
)
weights_reference = pd.read_csv(
resource_dir / "weights_smooth_weights_sum_squares.csv",
index_col=0,
header=0,
parse_dates=True,
)

pd.testing.assert_frame_equal(cov, cov_reference, atol=1e-3)
pd.testing.assert_frame_equal(weights, weights_reference, atol=1e-3)

0 comments on commit 1a78e22

Please sign in to comment.