def fit(self, X, y=None, sample_weight=None): """Fit the model with X. Parameters ---------- X : Triangle-like Set of LDFs to which the tail will be applied. y : Ignored sample_weight : Ignored Returns ------- self : object Returns the instance itself. """ super().fit(X, y, sample_weight) decay_range = self.ldf_.shape[-1]-X.shape[-1]+1 ldfs = 1+self._get_initial_ldf()*(self.decay**np.arange(1000)) ldfs = ldfs[:decay_range] ldfs[-1] = self.tail/np.prod(ldfs[:-1]) ldfs = X._expand_dims(ldfs[np.newaxis]) self.ldf_.values[..., -decay_range:] = \ self.ldf_.values[..., -decay_range:]*ldfs self.cdf_ = DevelopmentBase._get_cdf(self) return self
def fit(self, X, y=None, sample_weight=None): """Fit the model with X. Parameters ---------- X : Triangle-like Set of LDFs to which the tail will be applied. y : Ignored sample_weight : Triangle-like Exposure vector used to invoke the Cape Cod method. Returns ------- self : object Returns the instance itself. """ super().fit(X, y, sample_weight) model = ClarkLDF(growth=self.growth).fit(X, sample_weight=sample_weight) xp = cp.get_array_module(X.values) age_offset = {'Y': 6., 'Q': 1.5, 'M': 0.5}[X.development_grain] tail = 1 / model.G_( xp.array([ item * self._ave_period[1] + X.ddims[-1] - age_offset for item in range(self._ave_period[0] + 1) ])) tail = xp.concatenate((tail.values[..., :-1] / tail.values[..., -1], tail.values[..., -1:]), -1) self.ldf_.values = xp.concatenate( (X.ldf_.values, xp.repeat(tail, X.shape[2], 2)), -1) self.cdf_ = DevelopmentBase._get_cdf(self) return self
def fit(self, X, y=None, sample_weight=None): obj = copy.copy(X) if 'ldf_'not in obj: obj = Development().fit_transform(obj) self._ave_period = {'Y': (1, 12), 'Q': (4, 3), 'M': (12, 1)}[obj.development_grain] ddims = np.concatenate( (obj.ddims, [(item+1)*self._ave_period[1] + obj.ddims[-1] for item in range(self._ave_period[0])], [9999]), 0) self.ldf_ = copy.copy(obj.ldf_) tail = np.ones(self.ldf_.shape)[..., -1:] tail = np.repeat(tail, self._ave_period[0]+1, -1) self.ldf_.values = np.concatenate((self.ldf_.values, tail), -1) self.ldf_.ddims = np.array(['{}-{}'.format(ddims[i], ddims[i+1]) for i in range(len(ddims)-1)]) self.ldf_.valuation = self.ldf_._valuation_triangle() self.sigma_ = copy.copy(getattr(obj, 'sigma_', obj.cdf_*0)) self.std_err_ = copy.copy(getattr(obj, 'std_err_', obj.cdf_*0)) zeros = tail[..., -1:]*0 self.sigma_.values = np.concatenate( (self.sigma_.values, zeros), -1) self.std_err_.values = np.concatenate( (self.std_err_.values, zeros), -1) self.sigma_.ddims = self.std_err_.ddims = \ np.append(obj.ldf_.ddims, ['{}-9999'.format(int(obj.ddims[-1]))]) val_array = self.sigma_._valuation_triangle(self.sigma_.ddims) self.sigma_.valuation = self.std_err_.valuation = val_array self.cdf_ = DevelopmentBase._get_cdf(self) self.cdf_.set_slicers() self.ldf_.set_slicers() self.sigma_.set_slicers() self.std_err_.set_slicers() return self
def fit(self, X, y=None, sample_weight=None): """Fit the model with X. Parameters ---------- X : Triangle-like Set of LDFs to which the tail will be applied. y : Ignored sample_weight : Ignored Returns ------- self : object Returns the instance itself. """ super().fit(X, y, sample_weight) xp = cp.get_array_module(self.ldf_.values) _y = self.ldf_.values[..., :X.shape[-1] - 1].copy() _w = xp.zeros(_y.shape) if type(self.fit_period) is not slice: raise TypeError('fit_period must be slice.') else: _w[..., self.fit_period] = 1.0 if self.errors == 'ignore': _w[_y <= 1.0] = 0 _y[_y <= 1.0] = 1.01 elif self.errors == 'raise' and xp.any(y < 1.0): raise ZeroDivisionError('Tail fit requires all LDFs to be' + ' greater than 1.0') _y = xp.log(_y - 1) n_obs = X.shape[-1] - 1 k, v = X.shape[:2] _x = self._get_x(_w, _y) # Get LDFs coefs = WeightedRegression(axis=3).fit(_x, _y, _w) slope, intercept = coefs.slope_, coefs.intercept_ extrapolate = xp.cumsum( xp.ones(tuple(list(_y.shape)[:-1] + [self.extrap_periods + n_obs])), -1) tail = self._predict_tail(slope, intercept, extrapolate) if self.attachment_age: attach_idx = xp.min(xp.where(X.ddims >= self.attachment_age)) else: attach_idx = len(X.ddims) - 1 self.ldf_.values = xp.concatenate( (self.ldf_.values[..., :attach_idx], tail[..., attach_idx:]), -1) obj = Development().fit_transform(X) if 'ldf_' not in X else X sigma, std_err = self._get_tail_stats(obj) self.sigma_.values[..., -1] = sigma[..., -1] self.std_err_.values[..., -1] = std_err[..., -1] self.slope_ = slope self.intercept_ = intercept self.cdf_ = DevelopmentBase._get_cdf(self) return self
def fit(self, X, y=None, sample_weight=None): """Fit the model with X. Parameters ---------- X : Triangle-like Set of LDFs to which the tail will be applied. y : Ignored sample_weight : Ignored Returns ------- self : object Returns the instance itself. """ super().fit(X, y, sample_weight) _y = self.ldf_.values[..., :-1].copy() _w = np.zeros(_y.shape) if type(self.fit_period) is not slice: raise TypeError('fit_period must be slice.') else: _w[..., self.fit_period] = 1.0 if self.errors == 'ignore': _w[_y <= 1.0] = 0 _y[_y <= 1.0] = 1.01 elif self.errors == 'raise' and np.any(y < 1.0): raise ZeroDivisionError('Tail fit requires all LDFs to be \ greater than 1.0') _y = np.log(_y - 1) n_obs = X.shape[-1] - 1 k, v = X.shape[:2] _x = self._get_x(_w, _y) # Get LDFs coefs = WeightedRegression(axis=3).fit(_x, _y, _w) slope, intercept = coefs.slope_, coefs.intercept_ extrapolate = np.cumsum( np.ones(tuple(list(_y.shape)[:-1] + [self.extrap_periods])), -1) + n_obs tail = self._predict_tail(slope, intercept, extrapolate) self.ldf_.values = self.ldf_.values[..., :-tail.shape[-1]] self.ldf_.values = np.concatenate((self.ldf_.values, tail), -1) if X.__dict__.get('ldf_', None) is None: obj = Development().fit_transform(X) else: obj = X sigma, std_err = self._get_tail_stats(obj) self.sigma_.values[..., -1] = sigma[..., -1] self.std_err_.values[..., -1] = std_err[..., -1] self.slope_ = slope self.intercept_ = intercept self.cdf_ = DevelopmentBase._get_cdf(self) return self
def _apply_decay(self, X, tail): ''' Created Tail vector with decay over time ''' xp = cp.get_array_module(X.values) decay_range = self.ldf_.shape[-1] - X.shape[-1] + 1 if xp.max(tail) == 1.0: ldfs = 1 + 0 * (self.decay**xp.arange(1000)) else: ldfs = 1 + self._get_initial_ldf(xp, tail) * (self.decay** xp.arange(1000)) ldfs = ldfs[..., :decay_range] ldfs[..., -1:] = tail / xp.prod(ldfs[..., :-1], axis=-1, keepdims=True) self.ldf_.values[..., -decay_range:] = \ self.ldf_.values[..., -decay_range:]*ldfs self.cdf_ = DevelopmentBase._get_cdf(self) return self
def fit(self, X, y=None, sample_weight=None): """Fit the model with X. Parameters ---------- X : Triangle-like Set of LDFs to which the tail will be applied. y : Ignored sample_weight : Ignored Returns ------- self : object Returns the instance itself. """ super().fit(X, y, sample_weight) self.ldf_.values[..., -1] = self.ldf_.values[..., -1] * self.tail self.cdf_ = DevelopmentBase._get_cdf(self) return self