def wls(x): ''' Performs weighted least squares on the numpy array x of shape (N,3), where the three components are indep variable, dep variable, dep variable errors. Returns log(likelihood). ''' wls_model = WLS(x[:, 1], sm.add_constant(x[:, 0]), weights=1 / x[:, 2]**2) result = wls_model.fit() return -np.sum( (x[:, 1] - wls_model.predict(result.params))**2 / x[:, 2]** 2), result.params[1], x[:, 1] - wls_model.predict(result.params)
def fit(self, x, y1, y2, cens, w, verbose=False): """ Fit a maximum-likelihood Tobit regression :param x: Pandas DataFrame (n_samples, n_features): Data :param y: Pandas Series (n_samples,): Target :param cens: Pandas Series (n_samples,): -1 indicates left-censored samples, 0 for uncensored, 1 for right-censored :param verbose: boolean, show info from minimization :return: """ x_copy = x.copy() if self.fit_intercept: x_copy = np.insert(x_copy, 0, 1, axis=1) else: x_copy = skl.scale(x_copy, with_mean=True, with_std=False, copy=False) ## qui gen double `z' = cond(`y1'<.&`y2'<.,(`y1'+`y2')/2, /* ## */ cond(`y1'<.,`y1',`y2')) `moff' if `doit' y = [] counts = cens.value_counts() for value in [-1, 0, 1]: if value in counts: if value == -1: split = cens == value y_l = np.squeeze(y2[split].values) y.append(y_l) elif value == 1: split = cens == value y_r = np.squeeze(y1[split].values) y.append(y_r) elif value == 0: split = cens == value y_int = np.squeeze( (y1[split].values + y2[split].values) / 2) y.append(y_int) y = np.concatenate(y, axis=0) init_reg = WLS(y, x_copy, weights=w).fit() b0 = init_reg.params print(b0) y_pred = init_reg.predict(x_copy) resid = y - y_pred resid_var = np.var(resid) s0 = np.sqrt(resid_var) params0 = np.append(b0, s0) xs, ys, ys1, ys2, ws = split_left_right_censored( x_copy, y1, y2, cens, w) result = minimize(lambda params: tobit_neg_log_likelihood( xs, ys, ys1, ys2, ws, params), params0, jac=None, method='Powell', tol=0.000001, options={ 'disp': verbose, 'maxiter': 10000000, 'fatol': 0.00000001 }) if verbose: print(result) # self.ols_coef_ = b0[1:] # self.ols_intercept = b0[0] if self.fit_intercept: self.intercept_ = result.x[0] self.coef_ = result.x[1:-1] else: self.coef_ = result.x[:-1] self.intercept_ = 0 self.sigma_ = result.x[-1] return self