def mixture_model2(modelWh, modelPu): 'model process as single species, with mixture emission, supports multiple matingID' mixModel = get_mix_model(modelWh, modelPu) peaSpecies = model.VarFilterState('pea', mixModel) prior = model.StateGraph({'START': {peaSpecies: 1.0}}) stop = model.StopState(useObsLabel=False) term = model.StateGraph({ peaSpecies: { stop: 1. }, robomendel.noneState: { stop: 1. } }) sct = robomendel.SpeciesCrossTransition() 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', )) return dg
def unrelated_model(modelWh, modelPu): 'model mom, dad, child as independent' pstate = model.VarFilterState('Pu', modelPu) wstate = model.VarFilterState('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. }, robomendel.noneState: { stop: 1. } }) dg = model.DependencyGraph({ 'START': { 'mom': prior, 'dad': prior, 'child': prior }, 'mom': { 'STOP': term }, 'dad': { 'STOP': term }, 'child': { 'STOP': term } }) return dg
def mixture_model(modelWh, modelPu): 'model process as single species, with mixture emission' mixModel = get_mix_model(modelWh, modelPu) peaSpecies = model.VarFilterState('pea', mixModel) prior = model.StateGraph({'START': {peaSpecies: 1.0}}) stop = model.StopState(useObsLabel=False) term = model.StateGraph({ peaSpecies: { stop: 1. }, robomendel.noneState: { stop: 1. } }) sct = robomendel.SpeciesCrossTransition() dg = model.DependencyGraph({ 'START': { 'mom': prior, 'dad': prior }, ('mom', 'dad'): { 'child': sct }, 'child': { 'STOP': term } }) return dg
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 multicond_setup(modelWh, modelPu, pHybrid=0.): pstate = model.VarFilterState('Pu', modelPu) wstate = model.VarFilterState('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. }, robomendel.noneState: { stop: 1. } }) sct = robomendel.SpeciesCrossTransition(pHybrid=pHybrid) return pstate, wstate, prior, stop, term, sct
def unrelated_model2(modelWh, modelPu): 'model mom, dad, child as independent, supports multiple matingID' pstate = model.VarFilterState('Pu', modelPu) wstate = model.VarFilterState('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. }, robomendel.noneState: { stop: 1. } }) moms = model.BranchGenerator('mom', prior, iterTag='matingID') dads = model.BranchGenerator('dad', prior, iterTag='matingID') kids = model.BranchGenerator('child', prior, iterTag='matingID') dg = model.DependencyGraph( { 'START': { moms: {}, dads: {}, kids: {} }, 'mom': { 'STOP': term }, 'dad': { 'STOP': term }, 'child': { 'STOP': term } }, joinTags=('matingID', )) return dg
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 environmental_model(modelWh, modelPu): 'model wh / pu as random extrinsic variable' def filter_from_node(fromNode, *args): return dict(var=fromNode.var.label) pstate = model.VarFilterState('Pu', modelPu, filter_f=filter_from_node) wstate = model.VarFilterState('Wh', modelWh, filter_f=filter_from_node) peaSpecies = model.SilentState('pea') prior = model.StateGraph({'START': {peaSpecies: 1.}}) extSG = model.StateGraph({peaSpecies: {pstate: 0.9, wstate: 0.1}}) stop = model.StopState(useObsLabel=False) term = model.StateGraph({pstate: {stop: 1.}, wstate: {stop: 1.}}) sct = robomendel.SpeciesCrossTransition() dg = model.DependencyGraph({ 'START': { 'mom': prior, 'dad': prior }, 'mom': { 'ext': extSG }, 'dad': { 'ext': extSG }, ('mom', 'dad'): { 'child': sct }, 'child': { 'ext': extSG }, 'ext': { 'STOP': term } }) return dg
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 environmental_model2(modelWh, modelPu): 'model wh / pu as random extrinsic variable, supports multiple matingID' def filter_from_node(fromNode, *args): return dict(var=fromNode.var.label) pstate = model.VarFilterState('Pu', modelPu, filter_f=filter_from_node) wstate = model.VarFilterState('Wh', modelWh, filter_f=filter_from_node) peaSpecies = model.SilentState('pea') prior = model.StateGraph({'START': {peaSpecies: 1.}}) noSpecies = model.SilentState('no-species') extNone = model.VarFilterState('ext-none', model.EmissionDict({None: 1.}), filter_f=filter_from_node) extSG = model.StateGraph({ peaSpecies: { pstate: 0.9, wstate: 0.1 }, noSpecies: { extNone: 1. } }) stop = model.StopState(useObsLabel=False) term = model.StateGraph({ pstate: { stop: 1. }, wstate: { stop: 1. }, extNone: { stop: 1. } }) sct = robomendel.SpeciesCrossTransition(noneState=noSpecies) moms = model.BranchGenerator('mom', prior, iterTag='matingID') dads = model.BranchGenerator('dad', prior, iterTag='matingID') dg = model.DependencyGraph( { 'START': { moms: {}, dads: {} }, 'mom': { 'ext': extSG }, 'dad': { 'ext': extSG }, ('mom', 'dad'): { 'child': sct }, 'child': { 'ext': extSG }, 'ext': { 'STOP': term } }, joinTags=('matingID', )) return dg