def uniform_doseless(start, end, height=None): scaled = False if height is None: height = 1 / (end - start) scaled = True conditional = SplineModelConditional() conditional.add_segment(SplineModel.neginf, start, [SplineModel.neginf]) conditional.add_segment(start, end, [math.log(height)]) conditional.add_segment(end, SplineModel.posinf, [SplineModel.neginf]) return SplineModel(True, [''], [conditional], scaled)
def apply_as_distribution(self, model): if model.kind() == 'ddp_model': if len(model.xx) != len(self.yy): raise ValueError("Wrong number of values.") for ii in range(len(model.xx)): if model.get_xx()[ii] != self.get_yy()[ii]: raise ValueError("Wrong values in distribution.") vv = self.lin_p().dot(model.lin_p()) return DDPModel('ddp1', 'distribution', self.xx_is_categorical, self.get_xx(), model.yy_is_categorical, model.get_yy(), vv, scaled=self.scaled) elif model.kind() == 'spline_model': if len(model.xx) != len(self.yy): raise ValueError("Wrong number of values.") for ii in range(len(model.xx)): if model.get_xx()[ii] not in self.get_yy(): raise ValueError("Wrong values in distribution.") pp = self.lin_p() conditionals = [] for ii in range(len(self.xx)): conds = [] for jj in range(len(model.xx)): original = model.get_conditional(model.get_xx()[jj]) conditional = SplineModelConditional( original.y0s, original.y1s, original.coeffs) conditional.scale( pp[ii, self.get_yy().index(model.get_xx()[jj])]) conds.append(conditional) conditionals.append( SplineModelConditional.approximate_sum(conds)) return SplineModel(self.xx_is_categorical, self.get_xx(), conditionals, scaled=self.scaled) else: raise ValueError("Unknown model type in apply_as_distribution")
def test_exponential(): fp = StringIO.StringIO() fp.write("dpc1,mean\n") fp.write("0,1\n") fp.seek(0) output = StringIO.StringIO() FeaturesInterpreter.init_from_feature_file(SplineModel(), fp, ',', (0, 100)).write(output, ',') print output.getvalue() fp.close() output.close()
def test_gtc(modelfile, distfile): with open(modelfile, "r") as modelfp: model = SplineModel() FeaturesInterpreter.init_from_feature_file(model, modelfp, ',', (SplineModel.neginf, SplineModel.posinf)) with open(distfile, "r") as distfp: dist = DistributionModel() dist.init_from(distfp, ',') output = StringIO.StringIO() dist.apply_as_distribution(model).write(output, ',') print output.getvalue() output.close()
def test_non_gaussian(): fp = StringIO.StringIO() fp.write("dpc1,mean,.025,.975\n") fp.write("0,0,-1,2\n") fp.seek(0) output = StringIO.StringIO() FeaturesInterpreter.init_from_feature_file(SplineModel(), fp, ',', ('-inf', 'inf')).write( output, ',') print output.getvalue() fp.close() output.close()
def polynomial(lowbound, highbound, betas, covas, num=40): betas = np.array(betas) covas = np.mat(covas) if covas.shape[0] != covas.shape[1] and len(betas) != covas.shape[0]: return "Error: Please provide a complete covariance matrix." if np.any(linalg.eig(covas)[0] < 0): return "Error: Covariance matrix is not positive definite." xx = np.linspace(lowbound, highbound, num=num) xxs = {} for x in xx: xvec = np.mat([[1, x, x**2, x**3][0:len(betas)]]) serr = np.sqrt(xvec * covas * np.transpose(xvec)) xxs[x] = (betas.dot(np.squeeze(np.asarray(xvec))), serr[0,0]) return SplineModel.create_gaussian(xxs, xx, False)
import sys, StringIO sys.path.append("../lib/models") from model import Model from ddp_model import DDPModel from spline_model import SplineModel mod1 = DDPModel.create_lin([0, 1], dict(control=[.5, .5], treatment=[0, 1])) mod2 = DDPModel.create_lin([0, 1], dict(control=[.5, .5], treatment=[1, 0])) def printmod(mod): output = StringIO.StringIO() mod.write(output, ',') print output.getvalue() printmod(Model.combine([mod1, mod2], [.5, .5])) printmod(Model.combine([mod1, mod2], [.25, 2])) mod1 = SplineModel.create_gaussian(dict(control=(0, 1), treatment=(1, 1e-3))) mod2 = SplineModel.create_gaussian(dict(control=(0, 1), treatment=(0, 1e-3))) def printmod2(mod): output = StringIO.StringIO() mod.write_gaussian_plus(output, ',') print output.getvalue() printmod2(Model.combine([mod1, mod2], [.5, .5])) printmod2(Model.combine([mod1, mod2], [.25, 2]))
from model import Model from ddp_model import DDPModel from spline_model import SplineModel mod1 = DDPModel.create_lin([0, 1], dict(control=[.5, .5], treatment=[0, 1])) mod2 = DDPModel.create_lin([0, 1], dict(control=[.5, .5], treatment=[1, 0])) def printmod(mod): output = StringIO.StringIO() mod.write(output, ',') print output.getvalue() printmod(Model.combine([mod1, mod2], [.5, .5])) printmod(Model.combine([mod1, mod2], [.25, 2])) mod1 = SplineModel.create_gaussian(dict(control=(0, 1), treatment=(1, 1e-3))) mod2 = SplineModel.create_gaussian(dict(control=(0, 1), treatment=(0, 1e-3))) def printmod2(mod): output = StringIO.StringIO() mod.write_gaussian_plus(output, ',') print output.getvalue() printmod2(Model.combine([mod1, mod2], [.5, .5])) printmod2(Model.combine([mod1, mod2], [.25, 2]))