コード例 #1
0
ファイル: test_indian_gpa.py プロジェクト: probcomp/sppl
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()
コード例 #2
0
ファイル: test_render.py プロジェクト: probcomp/sppl
def get_model():
    Y = Id('Y')
    X = Id('X')
    Z = Id('Z')
    command = Sequence(
        Sample(Y, choice({
            '0': .2,
            '1': .2,
            '2': .2,
            '3': .2,
            '4': .2
        })), Sample(Z, bernoulli(p=0.1)),
        IfElse(Y << {str(0)} | Z << {0}, Sample(X, bernoulli(p=1 / (0 + 1))),
               Otherwise, Transform(X, Z**2 + Z)))
    return command.interpret()
コード例 #3
0
ファイル: test_indian_gpa.py プロジェクト: probcomp/sppl
def test_prior(get_model):
    model = get_model()
    GPA = Id('GPA')
    assert allclose(model.prob(GPA << {10}), 0.5 * 0.01)
    assert allclose(model.prob(GPA << {4}), 0.5 * 0.01)
    assert allclose(model.prob(GPA << {5}), 0)
    assert allclose(model.prob(GPA << {1}), 0)

    assert allclose(model.prob((2 < GPA) < 4),
                    0.5 * 0.99 * 0.5 + 0.5 * 0.99 * 0.2)
    assert allclose(model.prob((2 <= GPA) < 4),
                    0.5 * 0.99 * 0.5 + 0.5 * 0.99 * 0.2)
    assert allclose(model.prob((2 < GPA) <= 4),
                    0.5 * (0.99 * 0.5 + 0.01) + 0.5 * 0.99 * 0.2)
    assert allclose(model.prob((2 < GPA) <= 8),
                    0.5 * (0.99 * 0.5 + 0.01) + 0.5 * 0.99 * 0.6)
    assert allclose(model.prob((2 < GPA) < 10),
                    0.5 * (0.99 * 0.5 + 0.01) + 0.5 * 0.99 * 0.8)
    assert allclose(model.prob((2 < GPA) <= 10),
                    0.5 * (0.99 * 0.5 + 0.01) + 0.5 * (0.99 * 0.8 + 0.01))

    assert allclose(model.prob(((2 <= GPA) < 4) | (7 < GPA)),
                    (0.5 * 0.99 * 0.5 + 0.5 * 0.99 * 0.2) +
                    (0.5 * (0.99 * 0.3 + 0.01)))

    assert allclose(model.prob(((2 <= GPA) < 4) & (7 < GPA)), 0)
コード例 #4
0
ファイル: test_indian_gpa.py プロジェクト: probcomp/sppl
def model_ifelse_non_exhuastive():
    Nationality = Id('Nationality')
    Perfect = Id('Perfect')
    GPA = Id('GPA')
    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)), True,
               Sample(GPA, atomic(loc=4))))
    return command.interpret()
コード例 #5
0
ファイル: test_indian_gpa.py プロジェクト: probcomp/sppl
def test_condition():
    model = model_no_latents()
    GPA = Id('GPA')
    model_condition = model.condition(GPA << {4} | GPA << {10})
    assert len(model_condition.children) == 2
    assert model_condition.children[0].support == Interval.Ropen(4, 5)
    assert model_condition.children[1].support == Interval.Ropen(10, 11)

    model_condition = model.condition((0 < GPA < 4))
    assert len(model_condition.children) == 2
    assert model_condition.children[0].support \
        == model_condition.children[1].support
    assert allclose(model_condition.children[0].logprob(GPA < 1),
                    model_condition.children[1].logprob(GPA < 1))
コード例 #6
0
ファイル: test_burglary.py プロジェクト: probcomp/sppl
'''
Burglary network example from:

Artificial Intelligence: A Modern Approach (3rd Edition).
Russel and Norvig, Fig 14.2 pp 512.
'''

from sppl.compilers.ast_to_spe import Id
from sppl.compilers.ast_to_spe import IfElse
from sppl.compilers.ast_to_spe import Otherwise
from sppl.compilers.ast_to_spe import Sample
from sppl.compilers.ast_to_spe import Sequence
from sppl.distributions import bernoulli

Burglary    = Id('Burglary')
Earthquake  = Id('Earthquake')
Alarm       = Id('Alarm')
JohnCalls   = Id('JohnCalls')
MaryCalls   = Id('MaryCalls')

program = Sequence(
    Sample(Burglary,   bernoulli(p=0.001)),
    Sample(Earthquake, bernoulli(p=0.002)),
    IfElse(
        Burglary << {1},
            IfElse(
                Earthquake << {1},  Sample(Alarm, bernoulli(p=0.95)),
                Otherwise,          Sample(Alarm, bernoulli(p=0.94))),
        Otherwise,
            IfElse(
コード例 #7
0
ファイル: test_ast_condition.py プロジェクト: probcomp/sppl
# Copyright 2020 MIT Probabilistic Computing Project.
# See LICENSE.txt

import pytest

from sppl.compilers.ast_to_spe import Condition
from sppl.compilers.ast_to_spe import Id
from sppl.compilers.ast_to_spe import Sample
from sppl.compilers.ast_to_spe import Sequence
from sppl.distributions import beta
from sppl.distributions import choice
from sppl.distributions import randint
from sppl.math_util import allclose

Y = Id('Y')
X = Id('X')


def test_condition_nominal():
    command = Sequence(Sample(Y, choice({
        'a': .1,
        'b': .1,
        'c': .8
    })), Condition(Y << {'a', 'b'}))
    model = command.interpret()
    assert allclose(model.prob(Y << {'a'}), .5)
    assert allclose(model.prob(Y << {'b'}), .5)
    assert allclose(model.prob(Y << {'c'}), 0)


def test_condition_real_discrete_range():
コード例 #8
0
ファイル: test_indian_gpa.py プロジェクト: probcomp/sppl
import pytest

from sppl.compilers.ast_to_spe import Id
from sppl.compilers.ast_to_spe import IfElse
from sppl.compilers.ast_to_spe import Sample
from sppl.compilers.ast_to_spe import Sequence
from sppl.compilers.sppl_to_python import SPPL_Compiler
from sppl.distributions import atomic
from sppl.distributions import choice
from sppl.distributions import uniform
from sppl.math_util import allclose
from sppl.sets import Interval
from sppl.spe import ExposedSumSPE

Nationality = Id('Nationality')
Perfect = Id('Perfect')
GPA = Id('GPA')


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 model_exposed():
コード例 #9
0
# Copyright 2020 MIT Probabilistic Computing Project.
# See LICENSE.txt

from math import log

from sppl.compilers.ast_to_spe import Id
from sppl.compilers.ast_to_spe import IfElse
from sppl.compilers.ast_to_spe import Otherwise
from sppl.compilers.ast_to_spe import Sample
from sppl.compilers.ast_to_spe import Sequence
from sppl.compilers.ast_to_spe import Transform
from sppl.distributions import bernoulli
from sppl.distributions import norm
from sppl.math_util import allclose

X = Id('X')
Y = Id('Y')
Z = Id('Z')


def test_simple_transform():
    command = Sequence(Sample(X, norm(loc=0, scale=1)), Transform(Z, X**2))
    model = command.interpret()
    assert model.get_symbols() == {Z, X}
    assert model.env == {Z: X**2, X: X}
    assert (model.logprob(Z > 0)) == 0


def test_if_else_transform():
    model = Sequence(
        Sample(X, norm(loc=0, scale=1)),
コード例 #10
0
# See LICENSE.txt

from math import log

from sppl.compilers.ast_to_spe import For
from sppl.compilers.ast_to_spe import Id
from sppl.compilers.ast_to_spe import IdArray
from sppl.compilers.ast_to_spe import IfElse
from sppl.compilers.ast_to_spe import Otherwise
from sppl.compilers.ast_to_spe import Sample
from sppl.compilers.ast_to_spe import Sequence
from sppl.distributions import bernoulli
from sppl.distributions import choice
from sppl.math_util import allclose

Y = Id('Y')
X = IdArray('X', 5)
Z = IdArray('Z', 5)

def test_simple_model():
    command = Sequence(
        Sample(Y, bernoulli(p=0.5)),
        For(0, 5, lambda i:
            Sample(X[i], bernoulli(p=1/(i+1)))))
    model = command.interpret()

    symbols = model.get_symbols()
    assert len(symbols) == 6
    assert Y in symbols
    assert X[0] in symbols
    assert X[1] in symbols
コード例 #11
0
from sppl.distributions import beta
from sppl.distributions import randint
from sppl.distributions import uniform

from sppl.compilers.ast_to_spe import For
from sppl.compilers.ast_to_spe import Id
from sppl.compilers.ast_to_spe import IdArray
from sppl.compilers.ast_to_spe import IfElse
from sppl.compilers.ast_to_spe import Sample
from sppl.compilers.ast_to_spe import Sequence
from sppl.compilers.ast_to_spe import Switch
from sppl.compilers.ast_to_spe import Transform

from sppl.sym_util import binspace

simAll = Id('simAll')
sim = IdArray('sim', 5)
p1 = IdArray('p1', 5)
p2 = IdArray('p2', 5)
clickA = IdArray('clickA', 5)
clickB = IdArray('clickB', 5)

ns = 5
nd = ns - 1


def get_command_randint():
    return Sequence(
        Sample(simAll, randint(low=0, high=ns)),
        For(
            0, 5, lambda k: Switch(