-
Notifications
You must be signed in to change notification settings - Fork 6
add streaming OLS implementation with duckdb arrow interop #17
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
Open
apoorvalal
wants to merge
3
commits into
master
Choose a base branch
from
streaming
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,10 @@ | ||
| """ | ||
| .. include:: ../README.md | ||
| """ | ||
| from .estimators import ( | ||
| DuckRegression, | ||
| DuckMundlak, | ||
| DuckMundlakEventStudy, | ||
| ) | ||
| from .regularized import DuckRidge | ||
| from .stream import StreamingRegression |
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,150 @@ | ||||||||||
| """ | ||||||||||
| Streaming regression leveraging DuckDB's native Arrow IPC support. | ||||||||||
| """ | ||||||||||
|
|
||||||||||
| import numpy as np | ||||||||||
| import duckdb | ||||||||||
| from dataclasses import dataclass | ||||||||||
| from typing import Optional, Iterator | ||||||||||
|
|
||||||||||
|
|
||||||||||
| @dataclass | ||||||||||
| class RegressionStats: | ||||||||||
| """Sufficient statistics for streaming regression.""" | ||||||||||
|
|
||||||||||
| XtX: Optional[np.ndarray] = None | ||||||||||
| Xty: Optional[np.ndarray] = None | ||||||||||
| yty: float = 0.0 | ||||||||||
| n: int = 0 | ||||||||||
| k: Optional[int] = None | ||||||||||
|
|
||||||||||
| def update(self, X: np.ndarray, y: np.ndarray) -> "RegressionStats": | ||||||||||
| """Update statistics with new batch.""" | ||||||||||
| n_batch, k_batch = X.shape | ||||||||||
|
|
||||||||||
| if self.XtX is None: | ||||||||||
| self.k = k_batch | ||||||||||
| self.XtX = np.zeros((k_batch, k_batch)) | ||||||||||
| self.Xty = np.zeros(k_batch) | ||||||||||
|
|
||||||||||
| self.XtX += X.T @ X | ||||||||||
| self.Xty += X.T @ y | ||||||||||
| self.yty += y @ y | ||||||||||
| self.n += n_batch | ||||||||||
| return self | ||||||||||
|
|
||||||||||
| def solve_ols(self) -> np.ndarray: | ||||||||||
| """Compute OLS coefficients.""" | ||||||||||
| if self.n < self.k: | ||||||||||
| return None | ||||||||||
| self.check_condition() | ||||||||||
| return np.linalg.solve(self.XtX, self.Xty) | ||||||||||
|
|
||||||||||
| def solve_ridge(self, lambda_: float = 1.0) -> np.ndarray: | ||||||||||
| """Compute Ridge coefficients.""" | ||||||||||
| if self.XtX is None: | ||||||||||
| return None | ||||||||||
| XtX_reg = self.XtX + lambda_ * np.eye(self.k) | ||||||||||
| return np.linalg.solve(XtX_reg, self.Xty) | ||||||||||
|
|
||||||||||
| def check_condition(self, threshold: float = 1e10): | ||||||||||
| """Check the condition number of the XtX matrix.""" | ||||||||||
| if self.XtX is None: | ||||||||||
| return None | ||||||||||
| cond = np.linalg.cond(self.XtX) | ||||||||||
| if cond > threshold: | ||||||||||
| import warnings | ||||||||||
apoorvalal marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
|
||||||||||
| warnings.warn( | ||||||||||
| f"High condition number: {cond:.2e}. Consider using Ridge regression." | ||||||||||
| ) | ||||||||||
| return cond | ||||||||||
|
|
||||||||||
|
|
||||||||||
| class DuckDBArrowStream: | ||||||||||
| """ | ||||||||||
| Stream data from DuckDB using native Arrow IPC support. | ||||||||||
| """ | ||||||||||
|
|
||||||||||
| def __init__( | ||||||||||
| self, | ||||||||||
| conn: duckdb.DuckDBPyConnection, | ||||||||||
| query: str, | ||||||||||
| chunk_size: int = 10000, | ||||||||||
| feature_cols: list[str] = None, | ||||||||||
| target_col: str = None, | ||||||||||
|
Comment on lines
+74
to
+75
|
||||||||||
| feature_cols: list[str] = None, | |
| target_col: str = None, | |
| feature_cols: Optional[list[str]] = None, | |
| target_col: Optional[str] = None, |
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 |
|---|---|---|
|
|
@@ -4,3 +4,4 @@ tqdm | |
| duckdb | ||
| numba | ||
| pdoc | ||
| pyarrow | ||
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,47 @@ | ||
| import duckdb | ||
| import numpy as np | ||
| import pytest | ||
| from duckreg.stream import StreamingRegression | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def duckdb_conn(): | ||
| """Create an in-memory DuckDB connection.""" | ||
| conn = duckdb.connect(':memory:') | ||
| yield conn | ||
| conn.close() | ||
|
|
||
|
|
||
| def test_streaming_regression(duckdb_conn): | ||
| """Test streaming regression with a simple example.""" | ||
| # Create sample data | ||
| duckdb_conn.execute(""" | ||
| CREATE TABLE regression_data AS | ||
| WITH features AS ( | ||
| SELECT | ||
| random() as x0, | ||
| random() as x1, | ||
| random() as x2 | ||
| FROM generate_series(1, 100000) t(i) | ||
| ) | ||
| SELECT | ||
| x0, | ||
| x1, | ||
| x2, | ||
| 2.0*x0 - 1.5*x1 + 0.8*x2 + 0.1*random() as y | ||
| FROM features | ||
| """) | ||
|
|
||
| # Perform streaming regression | ||
| stream_reg = StreamingRegression.from_table(duckdb_conn, "regression_data") | ||
| stream_reg.fit(feature_cols=["x0", "x1", "x2"], target_col="y") | ||
| beta = stream_reg.solve_ols() | ||
|
|
||
| # Check the results | ||
| true_beta = np.array([2.0, -1.5, 0.8]) | ||
| assert np.allclose(beta, true_beta, atol=0.1) | ||
|
|
||
| # Check that the condition number warning is raised | ||
| with pytest.warns(UserWarning, match='High condition number'): | ||
| stream_reg.stats.XtX = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) | ||
| stream_reg.stats.check_condition() |
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.