Exemplo n.º 1
0
 def test_1latent(self):
     model_code = """
   data {
     int<lower=0> N;
     int<lower=0,upper=1> x[N];
   }
   parameters {
     real<lower=0,upper=1> p;
   }
   model {
     p ~ beta(1.0, 1.0);
     for (n in 1:N)
     x[n] ~ bernoulli(p);
   }
 """
     with self.test_session():
         model = ed.StanModel(model_code=model_code)
         data = {'N': 10, 'x': [0, 1, 0, 1, 0, 1, 0, 1, 1, 1]}
         zs = {'p': np.array(0.5)}
         _test(model, data, zs)
def test_1d():
    model_code = """
        data {
          int<lower=0> N;
          int<lower=0,upper=1> y[N];
        }
        parameters {
          real<lower=0,upper=1> theta;
        }
        model {
          theta ~ beta(0.5, 0.5);  // Jeffreys' prior
          for (n in 1:N)
            y[n] ~ bernoulli(theta);
        }
    """
    pystan_model = pystan.StanModel(model_code=model_code)
    ed_model = ed.StanModel(model=pystan_model)
    data = {'N': 10, 'y': [0, 1, 0, 1, 0, 1, 0, 1, 1, 1]}
    zs = np.array([[0.5]])
    _test(ed_model, pystan_model, data, zs)
    zs = np.array([[0.4], [0.2], [0.2351], [0.6213]])
    _test(ed_model, pystan_model, data, zs)
Exemplo n.º 3
0
data = np.array([0, 1, 0, 0, 0, 0, 0, 0, 0, 1])
npmodel = BetaBernoulli(data)

model_code = """
    data {
      int<lower=0> N;
      int<lower=0,upper=1> y[N];
    }
    parameters {
      real<lower=0,upper=1> theta;
    }
    model {
      theta ~ beta(1.0, 1.0);
      for (n in 1:N)
        y[n] ~ bernoulli(theta);
    }
"""
data = dict(N=10, y=[0, 1, 0, 0, 0, 0, 0, 0, 0, 1])

model = ed.StanModel(model_code=model_code, data=data)

print(npmodel._py_log_prob(np.array([[0.5]], dtype=np.float32)))
print(model._py_log_prob(np.array([[0.5]], dtype=np.float32)))
print()
print(npmodel._py_log_prob(np.array([[0.314]], dtype=np.float32)))
print(model._py_log_prob(np.array([[0.314]], dtype=np.float32)))
print()
print(npmodel._py_log_prob(np.array([[0.682]], dtype=np.float32)))
print(model._py_log_prob(np.array([[0.682]], dtype=np.float32)))
Exemplo n.º 4
0
    Prior: Beta
    Likelihood: Bernoulli
Variational model
    Likelihood: Mean-field Beta
"""
import edward as ed
from edward.models import Variational, Beta

model_code = """
    data {
      int<lower=0> N;
      int<lower=0,upper=1> y[N];
    }
    parameters {
      real<lower=0,upper=1> theta;
    }
    model {
      theta ~ beta(1.0, 1.0);
      for (n in 1:N)
        y[n] ~ bernoulli(theta);
    }
"""
ed.set_seed(42)
model = ed.StanModel(model_code=model_code)
variational = Variational()
variational.add(Beta())
data = ed.Data(dict(N=10, y=[0, 1, 0, 0, 0, 0, 0, 0, 0, 1]))

inference = ed.MFVI(model, variational, data)
inference.run(n_iter=10000)