Esempio n. 1
0
def fixture_model_with_plates(fixture_distribution_parameters,
                              fixture_pm_model_decorate):
    batch_shape, observed = fixture_distribution_parameters
    expected_obs_shape = (() if isinstance(observed, float) else
                          observed.shape[:len(observed.shape) -
                                         len(batch_shape)])
    if fixture_pm_model_decorate:
        expected_rv_shapes = {
            "model/loc": (),
            "model/obs": expected_obs_shape,
        }
    else:
        expected_rv_shapes = {"loc": (), "obs": expected_obs_shape}

    def model():
        loc = yield pm.Normal("loc", 0, 1)
        obs = yield pm.Normal("obs",
                              loc,
                              1,
                              plate=batch_shape,
                              observed=observed)
        return obs

    if fixture_pm_model_decorate:
        model = pm.model(model)

    return model, expected_rv_shapes
Esempio n. 2
0
def mc3_approach(T, width, height, N):
    shape = (height, width)
    x0 = np.random.randint(2, size=shape)
    with pm.model() as model:
        x = pm.Bernoulli('x', 0.5, shape=shape, testval=x0)
        magnetization = pm.Potential(
            'm',
            -get_H(to_spins(x)) / T
        )
        scaling = .0006
        mul = int(height * width * 1.75)
        step = pm.BinaryMetropolis([x], scaling=scaling)
        trace = pm.sample(N * mul * 5, step=step, chains=1, tune=False)
    dataset = [to_two_color(2 * t['x'] - 1) for t in trace[::mul * 5]]
    # Print out the final percent magnetization
    lattice = 2 * trace[-1]['x'] - 1
    print('Finished. Net magnetization: {:3.0%}'
              .format(abs(lattice.sum()) / lattice.size))
    return dataset