Skip to content

Add include_bias option to Polynomial basis function#230

Draft
kimjune01 wants to merge 3 commits into
wilsonrljr:mainfrom
kimjune01:add-include-bias-polynomial
Draft

Add include_bias option to Polynomial basis function#230
kimjune01 wants to merge 3 commits into
wilsonrljr:mainfrom
kimjune01:add-include-bias-polynomial

Conversation

@kimjune01

Copy link
Copy Markdown

The Legendre basis function supports include_bias for controlling whether a bias column is included, but the Polynomial basis function doesn't. Users who need to exclude the bias term from polynomial expansion have no way to do so without manual post-processing.

This adds the same include_bias parameter to Polynomial, following the existing pattern in the Legendre implementation. Default is False to maintain backward compatibility. Includes Array API support for all backends (NumPy, PyTorch, CuPy, JAX).

Fixes #193

Implements the feature requested in issue wilsonrljr#193 to allow users to
optionally include a bias (intercept) term in the Polynomial basis
function, matching the interface already present in newer basis
functions like Legendre.

Changes:
- Add `include_bias` parameter to Polynomial.__init__ (default=False
  for backward compatibility)
- Modify fit() to prepend a column of ones when include_bias=True
- Handle predefined_regressors correctly when bias is included
- Support both NumPy and Array API backends

Tests:
- Add comprehensive test suite in test_polynomial.py
- Verify backward compatibility with existing behavior
- Test bias inclusion/exclusion
- Test interaction with predefined_regressors
- Test shape changes

Maintains backward compatibility: default behavior unchanged.
@codacy-production

codacy-production Bot commented May 11, 2026

Copy link
Copy Markdown

Not up to standards ⛔

🔴 Issues 6 high

Alerts:
⚠ 6 issues (≤ 0 issues of at least minor severity)

Results:
6 new issues

Category Results
Security 6 high

View in Codacy

🟢 Metrics 10 complexity · 0 duplication

Metric Results
Complexity 10
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

kimjune01 added 2 commits May 11, 2026 12:57
Fixes four critical bugs identified in code review:

1. Double bias bug: build_lagged_matrix already prepends a bias column
   at index 0. The previous implementation added another bias column,
   creating rank deficiency. Now we strip the existing bias column
   before polynomial expansion, then optionally re-add it.

2. include_bias=False now actually excludes bias: Previously the
   (0,0,...,0) combination still generated a constant term even when
   include_bias=False. Now we remove the bias column before expansion,
   so all-zero combinations cannot occur.

3. regressor_space index mapping: By stripping the bias column first
   and optionally re-adding it after expansion, regressor indices now
   map correctly whether include_bias is True or False.

4. Default consistency: Changed default from False to True to match
   Legendre basis function behavior.

Implementation follows Legendre's pattern:
- Remove bias column (index 0) from build_lagged_matrix output
- Generate polynomial terms from remaining features
- Re-add bias column if include_bias=True
- Apply predefined_regressors filtering after bias handling
Prior commit broke backward compatibility: with include_bias=True the
output lost the cross-bias polynomial terms (combinations like (0,k))
because fit() sliced column 0 from the input before polynomial
expansion. test_fit_polynomial in test_basis_functions.py caught this.

Restore the original fit() path on default. When include_bias=False,
drop the (0,0,...,0) pure-bias column emitted first by
combinations_with_replacement, and drop the matching row from
RegressorDictionary.regressor_space so regressor_code stays aligned
with psi. Per maintainer guidance on PR wilsonrljr#197.
@kimjune01 kimjune01 marked this pull request as draft May 20, 2026 23:40
@kimjune01

Copy link
Copy Markdown
Author

Cannot validate on current setup, so drafting. Please close or take it over.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add include_bias option for basis function

1 participant