def __predict__(df, model, variable, formula): """UNUSED FUNCTION New hidden predict method to shorten Monte Carlo estimation code. This is slower than statsmodels predict, but likely can be optimized. I would need to find a faster alternative to np.dot()... For this to work, I need to do the following: -need to have each ..._model() store a self.formula -switch all functions to sm.GLM or sm.GEE """ import patsy from zepid.calc import odds_to_probability xdata = patsy.dmatrix(formula, df) # , return_type='dataframe') # pred = xdata.mul(np.array(model.params), axis='columns').sum(axis=1) pred = xdata.dot(model.params) # TODO optimize this... if variable == 'binary': pred = np.random.binomial(1, odds_to_probability(np.exp(pred)), size=xdata.shape[0]) elif variable == 'continuous': pred = np.random.normal(loc=pred, scale=np.std(model.resid), size=len(pp)) # TODO add optimization for multinomial (if applicable) else: raise ValueError('That option is not supported') return pred
def test_back_and_forth_conversions(self): original = 0.12 odd = probability_to_odds(original) pr = odds_to_probability(odd) npt.assert_allclose(original, pr)
def test_forth_and_back_conversions(self): original = 1.1 pr = odds_to_probability(original) odd = probability_to_odds(pr) npt.assert_allclose(original, odd)
def test_odds_to_probability(self): pr = odds_to_probability(1.1) npt.assert_allclose(pr, 1.1 / 2.1)