-
Notifications
You must be signed in to change notification settings - Fork 9
Add initial inference data filtering function #621
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
Merged
forstmeier
merged 5 commits into
master
from
08-22-add_initial_inference_data_filtering_function
Aug 24, 2025
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b13d386
Add initial inference data filtering function
forstmeier 790fce7
Add initial inference data filtering function
forstmeier 5dcdc1d
Merge branch '08-22-add_initial_inference_data_filtering_function' of…
forstmeier 4cd84be
Merge branch 'master' into 08-22-add_initial_inference_data_filtering…
forstmeier b9d2f20
Add pull request feedback
forstmeier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
20 changes: 20 additions & 0 deletions
20
applications/portfoliomanager/src/portfoliomanager/preprocess.py
This file contains hidden or 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,20 @@ | ||
| import polars as pl | ||
|
|
||
|
|
||
| def filter_equity_bars(data: pl.DataFrame) -> pl.DataFrame: | ||
| data = data.clone() | ||
|
|
||
| minimum_average_close_price = 10.0 | ||
| minimum_average_volume = 1_000_000.0 | ||
|
|
||
|
forstmeier marked this conversation as resolved.
Outdated
|
||
| return ( | ||
| data.group_by("ticker") | ||
| .agg( | ||
| avg_close_price=pl.col("close_price").mean(), | ||
| avg_volume=pl.col("volume").mean(), | ||
| ) | ||
| .filter( | ||
| (pl.col("avg_close_price") > minimum_average_close_price) | ||
| & (pl.col("avg_volume") > minimum_average_volume) | ||
| ) | ||
| ) | ||
This file contains hidden or 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,214 @@ | ||
| import polars as pl | ||
| import pytest | ||
| from portfoliomanager.preprocess import filter_equity_bars | ||
|
|
||
|
|
||
| def test_filter_equity_bars_above_thresholds() -> None: | ||
| data = pl.DataFrame( | ||
| { | ||
| "ticker": ["AAPL", "AAPL", "AAPL"], | ||
| "close_price": [15.0, 20.0, 25.0], | ||
| "volume": [ | ||
| 1_500_000.0, | ||
| 2_000_000.0, | ||
| 2_500_000.0, | ||
| ], | ||
| } | ||
| ) | ||
|
|
||
| result = filter_equity_bars(data) | ||
|
|
||
| assert len(result) == 1 | ||
| assert result["ticker"][0] == "AAPL" | ||
| assert result["avg_close_price"][0] == 20.0 # noqa: PLR2004 | ||
| assert result["avg_volume"][0] == 2_000_000.0 # noqa: PLR2004 | ||
|
|
||
|
|
||
| def test_filter_equity_bars_below_price_threshold() -> None: | ||
| data = pl.DataFrame( | ||
| { | ||
| "ticker": ["AAPL", "AAPL", "AAPL"], | ||
| "close_price": [5.0, 8.0, 9.0], | ||
| "volume": [ | ||
| 1_500_000.0, | ||
| 2_000_000.0, | ||
| 2_500_000.0, | ||
| ], | ||
| } | ||
| ) | ||
|
|
||
| result = filter_equity_bars(data) | ||
|
|
||
| assert len(result) == 0 | ||
|
|
||
|
|
||
| def test_filter_equity_bars_below_volume_threshold() -> None: | ||
| data = pl.DataFrame( | ||
| { | ||
| "ticker": ["AAPL", "AAPL", "AAPL"], | ||
| "close_price": [15.0, 20.0, 25.0], | ||
| "volume": [500_000.0, 600_000.0, 700_000.0], | ||
| } | ||
| ) | ||
|
|
||
| result = filter_equity_bars(data) | ||
|
|
||
| assert len(result) == 0 | ||
|
|
||
|
|
||
| def test_filter_equity_bars_below_both_thresholds() -> None: | ||
| data = pl.DataFrame( | ||
| { | ||
| "ticker": ["AAPL", "AAPL", "AAPL"], | ||
| "close_price": [5.0, 6.0, 7.0], | ||
| "volume": [500_000.0, 600_000.0, 700_000.0], | ||
| } | ||
| ) | ||
|
|
||
| result = filter_equity_bars(data) | ||
|
|
||
| assert len(result) == 0 | ||
|
|
||
|
|
||
| def test_filter_equity_bars_at_exact_thresholds() -> None: | ||
| data = pl.DataFrame( | ||
| { | ||
| "ticker": ["AAPL", "AAPL", "AAPL"], | ||
| "close_price": [ | ||
| 10.0, | ||
| 10.0, | ||
| 10.0, | ||
| ], | ||
| "volume": [ | ||
| 1_000_000.0, | ||
| 1_000_000.0, | ||
| 1_000_000.0, | ||
| ], | ||
| } | ||
| ) | ||
|
|
||
| result = filter_equity_bars(data) | ||
|
|
||
| assert len(result) == 0 | ||
|
|
||
|
|
||
| def test_filter_equity_bars_just_above_thresholds() -> None: | ||
| data = pl.DataFrame( | ||
| { | ||
| "ticker": ["AAPL", "AAPL", "AAPL"], | ||
| "close_price": [10.01, 10.01, 10.01], | ||
| "volume": [ | ||
| 1_000_001.0, | ||
| 1_000_001.0, | ||
| 1_000_001.0, | ||
| ], | ||
| } | ||
| ) | ||
|
|
||
| result = filter_equity_bars(data) | ||
|
|
||
| assert len(result) == 1 | ||
| assert result["ticker"][0] == "AAPL" | ||
| assert result["avg_close_price"][0] == pytest.approx(10.01) | ||
| assert result["avg_volume"][0] == pytest.approx(1_000_001.0) | ||
|
|
||
|
|
||
| def test_filter_equity_bars_empty_dataframe() -> None: | ||
| data = pl.DataFrame( | ||
| { | ||
| "ticker": [], | ||
| "close_price": [], | ||
| "volume": [], | ||
| } | ||
| ) | ||
|
|
||
| result = filter_equity_bars(data) | ||
|
|
||
| assert len(result) == 0 | ||
|
|
||
|
|
||
| def test_filter_equity_bars_single_row() -> None: | ||
| data = pl.DataFrame( | ||
| { | ||
| "ticker": ["AAPL"], | ||
| "close_price": [15.0], | ||
| "volume": [1_500_000.0], | ||
| } | ||
| ) | ||
|
|
||
| result = filter_equity_bars(data) | ||
|
|
||
| assert len(result) == 1 | ||
| assert result["ticker"][0] == "AAPL" | ||
| assert result["avg_close_price"][0] == 15.0 # noqa: PLR2004 | ||
| assert result["avg_volume"][0] == 1_500_000.0 # noqa: PLR2004 | ||
|
|
||
|
|
||
| def test_filter_equity_bars_mixed_values() -> None: | ||
| data = pl.DataFrame( | ||
| { | ||
| "ticker": ["AAPL", "AAPL"], | ||
| "close_price": [5.0, 25.0], | ||
| "volume": [ | ||
| 500_000.0, | ||
| 1_500_000.0, | ||
| ], | ||
| } | ||
| ) | ||
|
|
||
| result = filter_equity_bars(data) | ||
|
|
||
| assert len(result) == 0 | ||
|
|
||
|
|
||
| def test_filter_equity_bars_multiple_tickers() -> None: | ||
| data = pl.DataFrame( | ||
| { | ||
| "ticker": ["AAPL", "AAPL", "AAPL", "GOOGL", "GOOGL", "TSLA", "TSLA"], | ||
| "close_price": [ | ||
| 15.0, | ||
| 20.0, | ||
| 25.0, | ||
| 5.0, | ||
| 6.0, | ||
| 12.0, | ||
| 18.0, | ||
| ], | ||
| "volume": [ | ||
| 1_500_000.0, | ||
| 2_000_000.0, | ||
| 2_500_000.0, | ||
| 2_000_000.0, | ||
| 3_000_000.0, | ||
| 800_000.0, | ||
| 900_000.0, | ||
| ], | ||
| } | ||
| ) | ||
|
|
||
| result = filter_equity_bars(data) | ||
|
|
||
| assert len(result) == 1 | ||
| assert result["ticker"][0] == "AAPL" | ||
| assert result["avg_close_price"][0] == 20.0 # noqa: PLR2004 | ||
| assert result["avg_volume"][0] == 2_000_000.0 # noqa: PLR2004 | ||
|
|
||
|
|
||
| def test_filter_equity_bars_data_immutability() -> None: | ||
| original_data = pl.DataFrame( | ||
| { | ||
| "ticker": ["AAPL", "AAPL", "AAPL"], | ||
| "close_price": [15.0, 20.0, 25.0], | ||
| "volume": [1_500_000.0, 2_000_000.0, 2_500_000.0], | ||
| } | ||
| ) | ||
|
|
||
| original_tickers = original_data["ticker"].to_list() | ||
| original_close_prices = original_data["close_price"].to_list() | ||
| original_volumes = original_data["volume"].to_list() | ||
|
|
||
| filter_equity_bars(original_data) | ||
|
|
||
| assert original_data["ticker"].to_list() == original_tickers | ||
| assert original_data["close_price"].to_list() == original_close_prices | ||
| assert original_data["volume"].to_list() == original_volumes |
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.