Skip to content

Commit

Permalink
finished frac diff
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelson Griffiths committed Nov 27, 2024
1 parent 37c9eb5 commit 63f27a8
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 300 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ pyo3-polars = { version = "*", features = ["derive", "dtype-struct"] }
serde = { version = "1", features = ["derive"] }
polars = { version = "*", features = ["dtype-struct"] }
num = "0.4.3"
polars-arrow = "0.44.2"
polars-arrow = "*"
27 changes: 0 additions & 27 deletions polars_trading/_testing/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,3 @@ def frac_diff_ffd(series: pd.DataFrame, d: float, thresh: float = 1e-5) -> pd.Da
df_[loc1] = np.dot(w.T, series_f.loc[loc0:loc1])[0, 0]
df[name] = df_.copy(deep=True)
return pd.concat(df, axis=1)


if __name__ == "__main__":
import polars as pl

from polars_trading._testing.data import generate_trade_data
from polars_trading.features.frac_diff import frac_diff

pl.Config.set_verbose(True)
data = generate_trade_data(10_000, n_companies=3).sort("ts_event")
print(
data.with_columns(
frac_diff("price", 0.5, 1e-3).over("symbol").alias("frac_diff")
).sort("symbol")
)

print(
pl.from_pandas(
data.to_pandas()
.set_index("ts_event")
.groupby("symbol")[["price"]]
.apply(frac_diff_ffd, 0.5, 1e-3),
include_index=True,
).sort("symbol")
)

# print(frac_diff_ffd(data.to_pandas().set_index("ts_event")[["price"]], 0.5, 1e-3))
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ docstring-code-format = true
lint.select = ["ALL"]
lint.ignore = [
'A003',
'ANN101',
'ANN401',
'ARG002', # todo: enable
'ARG003', # todo: enable
Expand Down Expand Up @@ -85,7 +84,6 @@ dev-dependencies = [
"build>=1.2.1",
"commitizen>=3.29.0",
"marimo>=0.8.8",
"maturin[patchelf]>=1.7.1",
"mkdocs-gen-files>=0.5.0",
"mkdocstrings[python]>=0.26.1",
"pip>=24.2",
Expand All @@ -100,6 +98,7 @@ dev-dependencies = [
"pytest-codspeed>=2.2.1",
"mimesis>=12.1.0",
"pytest-cov>=5.0.0",
"maturin>=1.7.4",
]

[tool.commitizen]
Expand Down
2 changes: 1 addition & 1 deletion tests/features/test_frac_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test__get_weights_ffd__benchmark_pandas(benchmark):
def test__frac_diff__matches_pandas(trade_data):
trade_data = trade_data.sort("ts_event")
out = trade_data.select(
"ts_event", "symbol", frac_diff("price", 0.5, 1e-3).alias("frac_diff")
"ts_event", "symbol", frac_diff("price", 0.5, 1e-3).over("symbol").alias("frac_diff")
)
out2 = pl.DataFrame(apply_pd_frac_diff(trade_data.to_pandas(), 0.5, 1e-3)).rename(
{"level_1": "ts_event", "price": "frac_diff"}
Expand Down
Loading

0 comments on commit 63f27a8

Please sign in to comment.