Repository navigation
[FEAT] Record the seed each random draw used as random_state_, and handle random_state as documented
#1605
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
scks1
wants to merge
9
commits into
casact:main
Choose a base branch
from
scks1:fix-random-state-1568-on-ee2c24d1
base: main
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
[FEAT] Record the seed each random draw used as random_state_, and handle random_state as documented
#1605
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9099e07
[FIX] Accept random_state=None, 0 and RandomState instances as docume…
scks1 28021ba
[FEAT] Record the seed each random draw used as random_state_
scks1 f6a4507
[TST] Cover _check_random_state for a module with its own RandomState
scks1 33aa02c
Merge main into fix-random-state-1568-on-ee2c24d1
scks1 e22e4a3
[FEAT] Share random_state handling through RandomStateMixin
scks1 97609d1
[FEAT] Redraw Benktander aprioris in predict when random_state is None
scks1 fe22666
[FEAT] Move the random_state helpers into RandomStateMixin with type …
scks1 f1301b1
[TST] Add converse seed tests and run the column test on the raa fixture
scks1 214f68f
[TST] Move the CapeCod predict redraw test to test_capecod.py
scks1 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
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 |
|---|---|---|
|
|
@@ -2,10 +2,11 @@ | |
| # License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| # file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
| from chainladder.methods import MethodBase | ||
| from chainladder.utils.utility_functions import RandomStateMixin | ||
| import numpy as np | ||
|
|
||
|
|
||
| class Benktander(MethodBase): | ||
| class Benktander(MethodBase, RandomStateMixin): | ||
| """ | ||
| The Benktander (or iterated Bornhuetter-Ferguson) IBNR model | ||
|
|
||
|
|
@@ -28,15 +29,24 @@ class Benktander(MethodBase): | |
| using as a deterministic method. | ||
| If int, random_state is the seed used by the random number generator; | ||
| If RandomState instance, random_state is the random number generator; | ||
| If None, the random number generator is the RandomState instance used | ||
| by np.random. | ||
| If None, ``fit`` draws an integer seed from the RandomState instance | ||
| used by np.random and keeps it as ``random_state_``, and each | ||
| ``predict`` draws a new seed the same way, so its aprioris differ from | ||
| the fit's. Pass an integer to get the same aprioris from ``fit`` and | ||
| every ``predict``. A RandomState instance keeps advancing, so each | ||
| ``predict`` continues its sequence. | ||
|
|
||
| Attributes | ||
| ---------- | ||
| ultimate_: Triangle | ||
| The ultimate losses per the method | ||
| ibnr_: Triangle | ||
| The IBNR per the method | ||
| random_state_: int, RandomState instance or None | ||
| The seed the apriori draws used: ``random_state`` when it is an integer | ||
| or a RandomState instance, otherwise the integer ``fit`` drew. Passing | ||
| it back as ``random_state`` repeats the draws. It is ``random_state`` | ||
| itself when ``apriori_sigma`` is 0, since no draw is made. | ||
|
|
||
| See Also | ||
| -------- | ||
|
|
@@ -196,7 +206,9 @@ def fit(self, X, y=None, sample_weight=None): | |
| """ | ||
| if sample_weight is None: | ||
| raise ValueError("sample_weight is required.") | ||
|
|
||
| super().fit(X, y, sample_weight) | ||
| self._record_random_state(draws=self.apriori_sigma != 0) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the relationship between |
||
| self.expectation_ = self._get_benktander_aprioris(X, sample_weight) | ||
| self.ultimate_ = self._get_ultimate(self.X_, self.expectation_) | ||
| self.process_variance_ = self._include_process_variance() | ||
|
|
@@ -253,17 +265,24 @@ def predict(self, X, sample_weight=None): | |
| if sample_weight is None: | ||
| raise ValueError("sample_weight is required.") | ||
| X_new = super().predict(X, sample_weight) | ||
| X_new.expectation_ = self._get_benktander_aprioris(X, sample_weight) | ||
| X_new.expectation_ = self._get_benktander_aprioris( | ||
| X, sample_weight, redraw=True | ||
| ) | ||
| X_new.ultimate_ = self._get_ultimate(X_new, X_new.expectation_) | ||
| X_new.n_iters = self.n_iters | ||
| X_new.apriori = self.apriori | ||
| return X_new | ||
|
|
||
| def _get_benktander_aprioris(self, X, sample_weight): | ||
| """Private method to establish Benktander Apriori""" | ||
| def _get_benktander_aprioris(self, X, sample_weight, redraw=False): | ||
| """ | ||
| Private method to establish Benktander Apriori | ||
|
|
||
| ``predict`` passes ``redraw=True`` so that with ``random_state=None`` it | ||
| draws new aprioris instead of repeating the fit's. | ||
| """ | ||
| xp = X.get_array_module() | ||
| if self.apriori_sigma != 0: | ||
| random_state = xp.random.RandomState(self.random_state) | ||
| random_state = self._get_random_state(xp, redraw=redraw) | ||
| # Draw from lognormal with E[apriori] = self.apriori and SD = self.apriori_sigma. | ||
| cov = self.apriori_sigma / self.apriori | ||
| sigma_log = np.sqrt(np.log1p(cov**2)) | ||
|
|
||
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.
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.
for this and the next two tests, do we have and/or need the converse test, i.e. different local/global seeds lead to different results?