Repository navigation
[FEAT] Add Triangle.as_of and document how valuations are stamped #1620
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4009,3 +4009,52 @@ def test_clrd_samples_one_row_per_cell() -> None: | |
| """ | ||
| assert cl.load_sample("clrd", duplicates="raise").shape[0] == 779 | ||
| assert cl.load_sample("clrd2025", duplicates="raise").shape[0] == 772 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "date", | ||
| [ | ||
| "1988", | ||
| 1988, | ||
| "1988Q4", | ||
| "1988-12", | ||
| "1988-12-31", | ||
| pd.Timestamp("1988-12-31"), | ||
| pd.Period("1988", freq="Y"), | ||
| ], | ||
| ) | ||
| def test_as_of_keeps_the_named_period(raa, date) -> None: | ||
| """ | ||
| ``as_of`` keeps the diagonal valued at the end of the named period, which | ||
| a ``valuation <=`` filter with the same date leaves out (#1608). | ||
| """ | ||
| expected = raa[raa.valuation < "1989"] | ||
|
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. can you confirm that tri[tri.valuation < year_str] is explicitly tested somewhere? |
||
| result = raa.as_of(date) | ||
| assert result.shape == (1, 1, 8, 8) | ||
| assert result.valuation_date == expected.valuation_date | ||
| assert result.equals(expected) | ||
|
|
||
|
|
||
| def test_as_of_inside_a_period_stops_at_the_previous_one(raa) -> None: | ||
| """On an annual triangle a date inside 1988 stops at the end of 1987.""" | ||
| result = raa.as_of("1988-06-30") | ||
| assert result.shape == (1, 1, 7, 7) | ||
| assert result.equals(raa[raa.valuation < "1988"]) | ||
|
|
||
|
|
||
| def test_as_of_quarterly(qtr) -> None: | ||
| """A quarter and its last day give the same quarterly slice.""" | ||
| expected = qtr[qtr.valuation < "1995-07-01"] | ||
| assert qtr.as_of("1995Q2").equals(expected) | ||
| assert qtr.as_of("1995-06-30").equals(expected) | ||
| assert qtr.as_of("1995-06-30").valuation_date == expected.valuation_date | ||
|
|
||
|
|
||
| def test_as_of_latest_valuation_returns_the_whole_triangle(raa) -> None: | ||
| assert raa.as_of(raa.valuation_date).equals(raa) | ||
| assert raa.as_of("2030").equals(raa) | ||
|
|
||
|
|
||
| def test_as_of_before_the_first_valuation_raises(raa) -> None: | ||
| with pytest.raises(ValueError): | ||
| raa.as_of("1980") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1305,6 +1305,62 @@ def latest_diagonal(self) -> Triangle: | |
| ) | ||
| return obj | ||
|
|
||
| def as_of(self, date) -> Triangle: | ||
| """ | ||
| The Triangle as it stood at the end of a day or period. | ||
|
|
||
| Keeps every cell valued on or before the end of the period that | ||
| ``date`` names. Valuations are stamped at the last instant of their | ||
| period, while pandas reads a date bound as midnight at the start of | ||
|
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. this is a tad verbose. also "valuation period" is pure hallucinated term in this context. AI isn't comprehending that |
||
| the day, so ``tri[tri.valuation <= "1988-12-31"]`` leaves out the | ||
| cells valued at the end of 1988. ``tri.as_of("1988-12-31")`` keeps | ||
| them. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| date : str, int, pandas.Period or date-like | ||
| A string or integer names the period it spells: ``"1988"`` or | ||
| ``1988`` is the year 1988, ``"1988Q2"`` a quarter, ``"1988-06"`` | ||
| a month and ``"1988-06-30"`` a day. A ``pandas.Period`` is used | ||
|
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. can you list these out in bullets? |
||
| as given. A ``Timestamp``, ``datetime``, ``date`` or | ||
| ``numpy.datetime64`` names its whole day, whatever its time. | ||
|
|
||
| Returns | ||
| ------- | ||
| Triangle | ||
| The cells valued on or before the end of the named period. | ||
|
|
||
| Raises | ||
| ------ | ||
| ValueError | ||
| If ``date`` cannot be parsed, or if no cell is valued by then. | ||
|
|
||
| Examples | ||
| -------- | ||
|
|
||
| .. testsetup:: | ||
|
|
||
| import chainladder as cl | ||
|
|
||
| .. testcode:: | ||
|
|
||
| raa = cl.load_sample("raa") | ||
| print(raa.as_of("1988").shape) | ||
|
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. printing the actual as_of triangle would be a much stronger example |
||
| print(raa.as_of("1988-12-31").shape) | ||
| print(raa.as_of("1988-06-30").shape) | ||
|
|
||
| .. testoutput:: | ||
|
|
||
| (1, 1, 8, 8) | ||
| (1, 1, 8, 8) | ||
| (1, 1, 7, 7) | ||
| """ | ||
| if isinstance(date, (str, int, np.integer, pd.Period)): | ||
| period = pd.Period(date) | ||
| else: | ||
| period = pd.Period(pd.Timestamp(date), freq="D") | ||
| return self[self.valuation <= period.end_time] | ||
|
|
||
| @property | ||
| def link_ratio(self) -> Triangle: | ||
| """ | ||
|
|
||
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.
thanks for writing this all out. unfortunately we aren't displaying the docstring of every property, so this isn't showing up in the API reference.