Skip to content

Commit

Permalink
resolves #68
Browse files Browse the repository at this point in the history
  • Loading branch information
jbogaardt committed Jul 2, 2020
1 parent ee3ffcf commit 3b59c33
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
3 changes: 2 additions & 1 deletion chainladder/core/dunders.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ def _validate_arithmetic(self, other):
other.odims = obj.odims
elif len(obj.odims) == 1 and len(other.odims) > 1:
obj.odims = other.odims
obj.valuation = other.valuation
if len(other.ddims) == 1 and len(obj.ddims) > 1:
other.ddims = obj.ddims
elif len(obj.ddims) == 1 and len(other.ddims) > 1:
obj.ddims = other.ddims
obj.valuation = other.valuation
other = other.values
if not is_broadcastable:
# If broadcasting doesn't work, union axes similar to pandas
Expand Down Expand Up @@ -85,7 +87,6 @@ def _validate_arithmetic(self, other):

def _arithmetic_cleanup(self, obj, other):
''' Common functionality AFTER arithmetic operations '''

obj.values = obj.values * obj._expand_dims(obj._nan_triangle())
obj.values[obj.values == 0] = np.nan
return obj
Expand Down
8 changes: 8 additions & 0 deletions chainladder/core/tests/test_triangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,11 @@ def test_df_period_input():
def test_trend_on_vector():
raa = cl.load_sample('raa').latest_diagonal
assert raa.trend(.05, axis=2).to_frame().astype(int).iloc[0,0]==29216

def latest_diagonal_val_to_dev():
raa = cl.load_sample('raa')
assert raa.latest_diagonal.val_to_dev()==raa[raa.valuation==raa.valuation_date]

def vector_division():
raa = cl.load_sample('raa')
raa.latest_diagonal/raa
11 changes: 5 additions & 6 deletions chainladder/core/triangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,23 +357,22 @@ def val_to_dev(self, inplace=False):
def _val_dev_chg(self):
xp = cp.get_array_module(self.values)
obj = copy.deepcopy(self)
if self.shape[-1] == 1:
return obj
x = np.nan_to_num(obj.values)
val_mtrx = \
(np.array(obj.valuation.year).reshape(obj.shape[-2:], order='f') -
np.array(pd.DatetimeIndex(obj.odims).year)[..., None])*12 + \
(np.array(obj.valuation.month).reshape(obj.shape[-2:], order='f') -
np.array(pd.DatetimeIndex(obj.odims).month)[..., None]) + 1
rng = np.sort(np.unique(val_mtrx.flatten()[val_mtrx.flatten()>0]))
x = [np.sum((val_mtrx==item)*x,-1, keepdims=True)
for item in np.array(rng)]
x = [np.sum((val_mtrx==item)*x, -1, keepdims=True)
for item in np.array(rng)]
x = np.concatenate(x, -1)
obj.values = x
obj.values[obj.values == 0] = xp.nan
obj.ddims = np.array([item for item in rng])
obj.valuation = obj._valuation_triangle()
obj._set_slicers()
return obj
return obj.dropna()


def grain(self, grain='', trailing=False, inplace=False):
Expand Down Expand Up @@ -460,7 +459,7 @@ def grain(self, grain='', trailing=False, inplace=False):
if not self.is_cumulative:
obj = obj.cum_to_incr()
if self.is_val_tri:
obj = obj.dev_to_val()
obj = obj.dev_to_val().dropna()
if inplace:
self = obj
return self
Expand Down

0 comments on commit 3b59c33

Please sign in to comment.