-
Notifications
You must be signed in to change notification settings - Fork 115
OLS Bug + misc #1335
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
Merged
OLS Bug + misc #1335
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d498a5e
guarding for missing X or y in OLS_fit
henrydingliu 770e688
adding test for irregular full triangle
henrydingliu 1156a13
ruff fixes
henrydingliu 1c54522
ruff fix
henrydingliu 57f3dfb
adding sparse path
henrydingliu 53ce194
update sparse dependency
henrydingliu 27648e9
remove python 3.10
henrydingliu 4c292f6
Update pytest.yml
henrydingliu 134047d
Update pytest_upstream_nightly.yml
henrydingliu dd1f3ae
Update test_wtd_reg.py
henrydingliu 1a51358
Update weighted_regression.py
henrydingliu 4e1c304
Update test_wtd_reg.py
henrydingliu ebc24f3
Update test_wtd_reg.py
henrydingliu 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
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
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,31 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import chainladder as cl | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from chainladder import Triangle | ||
|
|
||
|
|
||
| class TestFullTri: | ||
| """Test weight generation on full triangles""" | ||
|
|
||
| def test_triangleweight_full_triangle(self, raa: Triangle) -> None: | ||
| """ | ||
| Testing new path that allows weights on full triangles | ||
| """ | ||
| ult = cl.Chainladder().fit(raa) | ||
| tw = cl.TriangleWeight(n_periods=4).fit(raa) | ||
| tw_full = cl.TriangleWeight(n_periods=4).fit(ult.full_triangle_) | ||
| assert tw.w_.iloc[:, :, :, 0] == tw_full.w_.iloc[:, :, :, 0] | ||
|
|
||
| def test_triangleweight_full_irregular_triangle(self) -> None: | ||
| """ | ||
| Testing unequal grains | ||
| """ | ||
| prism = cl.load_sample("prism_oydq")["Paid"] | ||
| ult = cl.Chainladder().fit(prism) | ||
| tw = cl.TriangleWeight(n_periods=4).fit(prism) | ||
| tw_full = cl.TriangleWeight(n_periods=4).fit(ult.full_triangle_) | ||
| assert tw.w_.iloc[:, :, :, 0] == tw_full.w_.iloc[:, :, :, 0] |
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
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,43 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import numpy as np | ||
| from chainladder.utils.sparse import sp | ||
| from chainladder.utils.weighted_regression import WeightedRegression | ||
|
|
||
|
|
||
| class TestOLS: | ||
| """Test the OLS calculations""" | ||
|
|
||
| def test_missing_data(self) -> None: | ||
| """Check that having nan in X and/or y still results in the right OLS coefficients.""" | ||
| data = [ | ||
| { | ||
| "module": np, | ||
| "X": [ | ||
| np.array([[[[1.0], [2.0], [3.0], [4.0], [5.0]]]]), | ||
| np.array([[[[1.0], [np.nan], [3.0], [4.0], [5.0]]]]), | ||
| ], | ||
| "y": [ | ||
| np.array([[[[1.0], [2.0], [3.0], [4.0], [5.0]]]]), | ||
| np.array([[[[1.0], [2.0], [np.nan], [4.0], [5.0]]]]), | ||
| ], | ||
| "w": np.array([[[[1.0], [1.0], [1.0], [1.0], [1.0]]]]), | ||
| "slope": np.array([[[[1.0]]]]), | ||
| } | ||
| ] | ||
| data.append({ | ||
| "module": sp, | ||
| "X": [sp.COO.from_numpy(i, fill_value=np.nan) for i in data[0]["X"]], | ||
| "y": [sp.COO.from_numpy(i, fill_value=np.nan) for i in data[0]["y"]], | ||
| "w": sp.COO.from_numpy(data[0]["w"]), | ||
| "slope": sp.COO.from_numpy(data[0]["slope"]), | ||
| }) | ||
| for i in data: | ||
| for x in i["X"]: | ||
| for y in i["y"]: | ||
| assert i["module"].all( | ||
| WeightedRegression(xp=i["module"]) | ||
| .fit(x, y, i["w"], "regression") | ||
| .slope_ | ||
| == i["slope"] | ||
| ) | ||
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noob math questions from me:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes to both. though for second question, you can think of it as weight of 1 being applied to a NaN cell.
you can try running the test on main. it will fail because the weights get applied incorrectly to one side of the observation pair that has a NaN.