def model_exposed(): return ExposedSumSPE( spe_weights=(Nationality >> choice({ 'India': 0.5, 'USA': 0.5 })), children={ # American student. 'USA': ExposedSumSPE(spe_weights=(Perfect >> choice({ 'True': 0.01, 'False': 0.99 })), children={ 'False': GPA >> uniform(loc=0, scale=4), 'True': GPA >> atomic(loc=4), }), # Indian student. 'India': ExposedSumSPE(spe_weights=(Perfect >> choice({ 'True': 0.01, 'False': 0.99 })), children={ 'False': GPA >> uniform(loc=0, scale=10), 'True': GPA >> atomic(loc=10), }) }, )
def model_perfect_nested(): Nationality = Id('Nationality') Perfect = Id('Perfect') GPA = Id('GPA') command = Sequence( Sample(Nationality, choice({ 'India': 0.5, 'USA': 0.5 })), IfElse( Nationality << {'India'}, Sequence( Sample(Perfect, choice({ 'True': 0.01, 'False': 0.99 })), IfElse(Perfect << {'True'}, Sample(GPA, atomic(loc=10)), True, Sample(GPA, uniform(scale=10)))), Nationality << {'USA'}, Sequence( Sample(Perfect, choice({ 'True': 0.01, 'False': 0.99 })), IfElse( Perfect << {'True'}, Sample(GPA, atomic(loc=4)), True, Sample(GPA, uniform(scale=4)), )))) return command.interpret()
def model_no_latents(): return \ 0.5 * ( # American student 0.99 * (GPA >> uniform(loc=0, scale=4)) | \ 0.01 * (GPA >> atomic(loc=4))) | \ 0.5 * ( # Indian student 0.99 * (GPA >> uniform(loc=0, scale=10)) | \ 0.01 * (GPA >> atomic(loc=10)))
def test_logpdf_lexicographic_both(): spe = .75*(X >> norm() & Y >> atomic(loc=0) & Z >> discrete({1:.2, 2:.8})) \ | .25*(X >> discrete({1:.5, 2:.5}) & Y >> norm() & Z >> atomic(loc=2)) # Lexicographic, Mix assignment = {X:1, Y:0, Z:2} assert allclose( spe.logpdf(assignment), logsumexp([ log(.75) + norm().dist.logpdf(1) + log(1) + log(.8), log(.25) + log(.5) + norm().dist.logpdf(0) + log(1)])) assert isinstance(spe.constrain(assignment), SumSPE)
def test_logpdf_lexicographic_either(): spe = .75*(X >> norm() & Y >> atomic(loc=0) & Z >> discrete({1:.1, 2:.9})) \ | .25*(X >> atomic(loc=0) & Y >> norm() & Z >> norm()) # Lexicographic, Branch 1 assignment = {X:0, Y:0, Z:2} assert allclose( spe.logpdf(assignment), log(.75) + norm().dist.logpdf(0) + log(1) + log(.9)) assert isinstance(spe.constrain(assignment), ProductSPE) # Lexicographic, Branch 2 assignment = {X:0, Y:0, Z:0} assert allclose( spe.logpdf(assignment), log(.25) + log(1) + norm().dist.logpdf(0) + norm().dist.logpdf(0)) assert isinstance(spe.constrain(assignment), ProductSPE)
def model_ifelse_exhuastive(): command = Sequence( Sample(Nationality, choice({ 'India': 0.5, 'USA': 0.5 })), Sample(Perfect, choice({ 'True': 0.01, 'False': 0.99 })), IfElse((Nationality << {'India'}) & (Perfect << {'False'}), Sample(GPA, uniform(loc=0, scale=10)), (Nationality << {'India'}) & (Perfect << {'True'}), Sample(GPA, atomic(loc=10)), (Nationality << {'USA'}) & (Perfect << {'False'}), Sample(GPA, uniform(loc=0, scale=4)), (Nationality << {'USA'}) & (Perfect << {'True'}), Sample(GPA, atomic(loc=4)))) return command.interpret()