Пример #1
0
               theta = 0,K = 5000 , mean_trait=0,dev_trait=20,mean_pop=50,dev_pop=20, num_time=2000,replicate=0)

# a node is defined by giving a distribution from scipy.stats together with any arguments (here 0 and 2)
gamma = elfi.Prior(scipy.stats.uniform, 0, 1)

# ELFI also supports giving the scipy.stats distributions as strings
a = elfi.Prior('uniform', 0, 1)

def single_trait_sim(gamma,a):
    sim = traitsim(h = 1, num_iteration=1,num_species=10,gamma1=gamma,gamma_K2=gamma,a = a,r = 1,theta = 0,K = 5000
                   , mean_trait=0,dev_trait=20,mean_pop=50,dev_pop=20, num_time=2000,replicate = 0)
    return sim

Y = elfi.Simulator(single_trait_sim,gamma,a,observed=obs)

def summary1(x):
    trait = x[0]
    return trait

simdata = elfi.Summary(summary1, Y)

d =  elfi.Distance('euclidean', simdata, obs[0])

elfi.draw(d)
print(d)

import elfi
from elfi.examples import ma2
model = ma2.get_model()
elfi.draw(model)
Пример #2
0
# Generate some data (using a fixed seed here)
np.random.seed(20170525)
y0 = simulator(mean0, std0)
print(y0)

mu = elfi.Prior('uniform', -2, 4)
sigma = elfi.Prior('uniform', 1, 4)

# Add the simulator node and observed data to the model
sim = elfi.Simulator(simulator, mu, sigma, observed=y0)

# Add summary statistics to the model
S1 = elfi.Summary(mean, sim)
S2 = elfi.Summary(var, sim)

# Specify distance as euclidean between summary vectors (S1, S2) from simulated and
# observed data
d = elfi.Distance('euclidean', S1, S2)

# Plot the complete model (requires graphviz)
elfi.draw(d)

rej = elfi.Rejection(d, batch_size=10000, seed=30052017)
res = rej.sample(1000, threshold=.5)
print(res)

import matplotlib.pyplot as plt
res.plot_marginals()
plt.show()
Пример #3
0
def autocov(x, lag=1):
    C = np.mean(x[:, lag:] * x[:, :-lag], axis=1)
    return C


# %%
S1 = elfi.Summary(autocov, Y)
S2 = elfi.Summary(autocov, Y,
                  2)  # the optional keyword lag is given the value 2

# %%
# Finish the model with the final node that calculates the squared distance (S1_sim-S1_obs)**2 + (S2_sim-S2_obs)**2
d = elfi.Distance('euclidean', S1, S2)

# %%
elfi.draw(d)  # just give it a node in the model, or the model itself (d.model)


# %%
# define prior for t1 as in Marin et al., 2012 with t1 in range [-b, b]
class CustomPrior_t1(elfi.Distribution):
    def rvs(b, size=1, random_state=None):
        u = scipy.stats.uniform.rvs(loc=0,
                                    scale=1,
                                    size=size,
                                    random_state=random_state)
        t1 = np.where(u < 0.5,
                      np.sqrt(2. * u) * b - b, -np.sqrt(2. * (1. - u)) * b + b)
        return t1