import matplotlib.pyplot as plt
import numpy as np

from brancher.distributions import NormalDistribution, LogNormalDistribution
from brancher.variables import DeterministicVariable, RandomVariable, ProbabilisticModel
from brancher.standard_variables import NormalVariable, LogNormalVariable
from brancher import inference
import brancher.functions as BF

# Real model
nu_real = 1.
mu_real = -2.
x_real = NormalVariable(mu_real, nu_real, "x_real")

# Normal model
nu = LogNormalVariable(0., 1., "nu")
mu = NormalVariable(0., 10., "mu")
x = NormalVariable(mu, nu, "x")
model = ProbabilisticModel([x])

print(model)

# Print samples
sample = model.get_sample(10)
print(sample)

# Print samples from single variable
x_sample = x.get_sample(10)
print(x_sample)

# Print samples conditional on an input
示例#2
0
import matplotlib.pyplot as plt
import numpy as np

from brancher.variables import DeterministicVariable, RandomVariable, ProbabilisticModel
from brancher.standard_variables import NormalVariable, LogNormalVariable, BetaVariable
from brancher import inference
import brancher.functions as BF

# Probabilistic model #
T = 100

nu = LogNormalVariable(0.3, 1., 'nu')
x0 = NormalVariable(0., 1., 'x0')
b = BetaVariable(0.5, 1.5, 'b')

x = [x0]
names = ["x0"]
for t in range(1, T):
    names.append("x{}".format(t))
    x.append(NormalVariable(b * x[t - 1], nu, names[t]))
AR_model = ProbabilisticModel(x)

# Generate data #
data = AR_model._get_sample(number_samples=1)
time_series = [float(data[xt].cpu().detach().numpy()) for xt in x]
true_b = data[b].cpu().detach().numpy()
true_nu = data[nu].cpu().detach().numpy()
print("The true coefficient is: {}".format(float(true_b)))

# Observe data #
[xt.observe(data[xt][:, 0, :]) for xt in x]
# Parameters
T = 12.
N = 100
dt = T / float(N)
time_range = np.linspace(0., T, N)
input_pulses = [(1., 2.), (7., 8.)]
deterministic_input = [
    1. if any([t_min < t < t_max for t_min, t_max in input_pulses]) else 0.
    for t in time_range
]
plt.plot(deterministic_input)
plt.plot()

# Probabilistic model
a = LogNormalVariable(0., 1., name="a")
b = LogNormalVariable(0., 1., name="b")
c = NormalVariable(0., 2., name="c")
d = NormalVariable(0., 2., name="d")
e = NormalVariable(0., 10., name="e")
xi = LogNormalVariable(0., 0.1, name="xi")
chi = LogNormalVariable(0., 0.1, name="chi")
x_series = [NormalVariable(0., 1., name="x_0")]
y_series = [NormalVariable(0., 1., name="y_0")]
for n, t in enumerate(time_range):
    x_new_mean = (1 - dt * a) * x_series[-1] + dt * c * y_series[
        -1] + dt * e * deterministic_input[n]
    y_new_mean = (1 - dt * b) * y_series[-1] + dt * d * x_series[-1]
    x_series += [
        NormalVariable(x_new_mean, np.sqrt(dt) * xi, name="x_{}".format(n + 1))
    ]
示例#4
0
from brancher.variables import DeterministicVariable, RandomVariable, ProbabilisticModel
from brancher.standard_variables import NormalVariable, LogNormalVariable, BetaVariable
from brancher import inference
import brancher.functions as BF

# Probabilistic model
T = 2.
N = 100
dt = T/float(N)
time_range = np.linspace(0., T, N)
a = BetaVariable(1., 1., name="a")
b = BetaVariable(1., 1., name="b")
c = NormalVariable(0., 0.1, name="c")
d = NormalVariable(0., 0.1, name="d")
xi = LogNormalVariable(0.1, 0.1, name="xi")
chi = LogNormalVariable(0.1, 0.1, name="chi")
x_series = [NormalVariable(0., 1., name="x_0")]
y_series = [NormalVariable(0., 1., name="y_0")]
for n, t in enumerate(time_range):
    x_new_mean = (1-dt*a)*x_series[-1] + dt*c*y_series[-1]
    y_new_mean = (1-dt*b)*y_series[-1] + dt*d*x_series[-1]
    x_series += [NormalVariable(x_new_mean, np.sqrt(dt)*xi, name="x_{}".format(n+1))]
    y_series += [NormalVariable(x_new_mean, np.sqrt(dt)*chi, name="y_{}".format(n+1))]
dynamic_causal_model = ProbabilisticModel([x_series[-1], y_series[-1]])

# Run dynamics
sample = dynamic_causal_model.get_sample(number_samples=3)

# Observe
observable_data = sample[[x.name for x in x_series] + [y.name for y in y_series]]
# Experimental model
x = RootVariable(x_mesh, name="x")  #TODO: it should create this automatically
y = RootVariable(y_mesh, name="y")
w1 = NormalVariable(0., 1., name="w1")
w2 = NormalVariable(0., 1., name="w2")
b = NormalVariable(0., 1., name="b")
experimental_input = NormalVariable(BF.exp(BF.sin(w1 * x + w2 * y + b)),
                                    0.1,
                                    name="input",
                                    is_observed=True)

# Probabilistic Model
mu_x = NormalVariable(0., 1., name="mu_x")
mu_y = NormalVariable(0., 1., name="mu_y")
v = LogNormalVariable(0., 0.1, name="v")
nu = LogNormalVariable(-1, 0.01, name="nu")
receptive_field = BF.exp((-(x - mu_x)**2 - (y - mu_y)**2) /
                         (2. * v**2)) / (2. * BF.sqrt(np.pi * v**2))
mean_response = BF.sum(BF.sum(receptive_field * experimental_input,
                              dim=1,
                              keepdim=True),
                       dim=2,
                       keepdim=True)  #TODO; not very intuitive
response = NormalVariable(mean_response, nu, name="response")
model = ProbabilisticModel([response, experimental_input])

# Generate data and observe the model
sample = model.get_sample(15,
                          input_values={
                              mu_x: 1.,