-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* resampling * testing the resampling * Resampling in the notebook * Resampling in the notebook * Resampling in the notebook
- Loading branch information
Showing
9 changed files
with
55,438 additions
and
17,545 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import pandas as pd | ||
import numpy as np | ||
|
||
|
||
def resample_index(index, rule): | ||
""" | ||
The resample_index function resamples a pandas DatetimeIndex object | ||
to a lower frequency using a specified rule. | ||
Note that the function does not modify the input index object, | ||
but rather returns a pandas DatetimeIndex | ||
""" | ||
series = pd.Series(index=index, data=index) | ||
a = series.resample(rule=rule).first() | ||
return pd.DatetimeIndex(a.values) | ||
|
||
|
||
def project_frame_to_grid(frame, grid): | ||
"""The project_frame_to_grid function projects a pandas | ||
DataFrame object onto a given index grid. | ||
The function returns a new DataFrame that is only updated for times in the grid, | ||
otherwise the previous values carry over. | ||
Note that the function does not modify the input frame object, but rather returns a new object. | ||
""" | ||
sample = pd.DataFrame(index=frame.index, columns=frame.columns, data=np.NaN) | ||
sample.loc[grid] = frame.loc[grid] | ||
return sample.ffill() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import pytest | ||
import pandas as pd | ||
import numpy as np | ||
|
||
from cvx.simulator.builder import builder | ||
from cvx.simulator.grid import resample_index, project_frame_to_grid | ||
|
||
|
||
def test_resample_index(prices): | ||
x = resample_index(prices.index, rule="M") | ||
for t in x: | ||
assert t in prices.index | ||
|
||
assert len(x) == 28 | ||
|
||
# first day in the middle of a month | ||
x = resample_index(prices.index[4:], rule="M") | ||
assert len(x) == 28 | ||
assert x[0] == prices.index[4] | ||
|
||
|
||
def test_project_frame_to_grid(prices): | ||
grid = resample_index(prices.index[4:], rule="M") | ||
frame = project_frame_to_grid(prices, grid=grid) | ||
a = frame.diff().sum(axis=1) | ||
assert a.tail(5).sum() == 0.0 | ||
|
||
|
||
|
||
def test_portfolio_resampling(prices): | ||
# construct a portfolio with only one asset | ||
b = builder(prices[["A"]]) | ||
assert set(b.assets) == {"A"} | ||
|
||
# compute a grid and rebalance only on those particular days | ||
grid = resample_index(prices.index, rule="M") | ||
|
||
# change the position only at days that are in the grid | ||
for times, _ in b: | ||
if times[-1] in grid: | ||
b[times[-1]] = pd.Series({"A": np.random.rand(1)}) | ||
else: | ||
# new position is the old position | ||
# This may look ineffective but we could use | ||
# trading costs for just holding short positions etc. | ||
b[times[-1]] = b[times[-2]] | ||
|
||
portfolio = b.build() | ||
print(portfolio.stocks) | ||
|
||
assert portfolio.stocks["A"].tail(10).std() == pytest.approx(0.0, abs=1e-12) |
Oops, something went wrong.