def pl_for_model(model_f, obsSet): modelWh = stats.norm(0, 1) modelPu = stats.norm(10, 1) dg = model_f(modelWh, modelPu) m = model.Model(dg, obsSet) logP = m.segmentGraph.p_forward(m.logPobsDict) llDict = basic_pl(m.segmentGraph) return llDict, logP
def subgraph_pl_test(modelDict=dict(mix=mixture_model2, family=family_model2, unrelated=unrelated_model2, environmental=environmental_model2), obsSet=None): 'build multi-model hypergraph, apply to the obs, and print posterior liklihoods' p = 1. / len(modelDict) # uninformative prior modelWh = stats.norm(0, 1) modelPu = stats.norm(10, 1) if obsSet is None: obsSet = get_family_obs(matingID=0) stop = model.StopState(useObsLabel=False) d = {} d2 = {} for model_name, model_f in modelDict.items(): # build distinct models state = model.SilentState(model_name) state.subgraph = model_f(modelWh, modelPu) d[state] = p d2[state] = {stop: 1.} prior = model.StateGraph({'START': d}) term = model.StateGraph(d2) dg = model.DependencyGraph({ 'START': { 'model': prior }, 'model': { 'STOP': term } }) m = model.Model(dg, obsSet) m.segmentGraph.p_forward(m.logPobsDict) f = m.segmentGraph.fprob[m.start].f fmerge = {} for node, logP in f.items(): # merge forward calcs from subgraphs try: subgraph = node.segmentGraph except AttributeError: pass else: merge_forward_dict(subgraph.fprob[subgraph.start].f, logP, fmerge) llDict = model.posterior_ll(fmerge) print_pl(llDict) return llDict
def mating_test(species, priors=None, **kwargs): 'generate 2 x 2 test of all possible mating combinations' if not priors: priors = (1. / len(species), ) * len(species) scm = robomendel.SpeciesCrossModel(species, priors, **kwargs) mstate = model.LinearState('mating', scm) prior = model.StateGraph({'START': {mstate: 1}}) branches = model.BranchGenerator('chi', prior, iterTag='matingID') stop = model.StopState(useObsLabel=False) term = model.StateGraph({mstate: {stop: 1.}}) dg = model.DependencyGraph({ 'START': { branches: {} }, 'chi': { 'STOP': term } }) obsSet = model.ObsSet('mating obs') obsSet.add_obs(species[0].rvs(3), matingID=0) obsSet.add_obs((species[0].rvs(1)[0], species[1].rvs(1)[0], None), matingID=1) obsSet.add_obs((species[0].rvs(1)[0], species[0].rvs(1)[0], None), matingID=2) obsSet.add_obs( (species[0].rvs(1)[0], species[1].rvs(1)[0], species[0].rvs(1)[0]), matingID=3) m = model.Model(dg, obsSet) logPobs = m.calc_fb() llDict = m.posterior_ll() for matingID, t in enumerate( ((0, 0, 0), (0, 1, None), (0, 0, None), (0, 1, 0))): obsLabel = obsSet.get_subset(matingID=matingID) print 'mating %s:\tlogP = %1.3f, %1.3f, %1.3f' % \ tuple([str(t)] + llDict[obsLabel])
def multicond2_calc(modelWh, modelPu, obsSet): '''This test creates nodes representing mom, dad and the child, with a multi-cond edge from (mom,dad) --> child and tests two different matings simultaneously.''' pstate, wstate, prior, stop, term, sct = multicond_setup(modelWh, modelPu) moms = model.BranchGenerator('mom', prior, iterTag='matingID') dads = model.BranchGenerator('dad', prior, iterTag='matingID') dg = model.DependencyGraph( { 'START': { moms: {}, dads: {} }, ('mom', 'dad'): { 'child': sct }, 'child': { 'STOP': term } }, joinTags=('matingID', )) m = model.Model(dg, obsSet) return m, m.segmentGraph.p_forward(m.logPobsDict)
def pheno1_setup(modelWh, modelPu): pstate = model.LinearState('Pu', modelPu) wstate = model.LinearState('Wh', modelWh) prior = model.StateGraph({'START': {pstate: 0.9, wstate: 0.1}}) stop = model.StopState(useObsLabel=False) term = model.StateGraph({pstate: {stop: 1.}, wstate: {stop: 1.}}) branches = model.BranchGenerator('chi', prior, iterTag='plantID') dg = model.DependencyGraph({ 'START': { branches: {} }, 'chi': { 'STOP': term } }) obsSet = model.ObsSet('plants') for plant in range(2): # two white plants obsSet.add_obs(modelWh.rvs(100), plantID=plant) for plant in range(2, 20): # 18 purple plants obsSet.add_obs(modelPu.rvs(100), plantID=plant) m = model.Model(dg, obsSet) return m, obsSet
def multicond_calc(modelWh, modelPu, obsSet): '''This test creates nodes representing mom, dad and the child, with a multi-cond edge from (mom,dad) --> child''' m = model.Model(family_model(modelWh, modelPu), obsSet) return m, m.segmentGraph.p_forward(m.logPobsDict)