Ejemplo n.º 1
0
# Set up the model
sim = DAPSimulator(I, dt, -75, dim_param=2)
stats = DAPSummaryStatsMoments(t_on, t_off, n_summary=n_summary)

# Setup Priors
prior_min = np.array([0, 0])
prior_max = np.array([2, 2])
prior_unif = Uniform(lower=prior_min, upper=prior_max)

# generate desired data
U = dap.simulate(dt, t, I)
y_o = {'data': U.reshape(-1), 'time': t, 'dt': dt, 'I': I}
y = stats.calc([y_o])

# Sample Parameters
params = prior_unif.gen(n_samples=n_samples)

# calculate the distance for each parameters
norms = []

for p in tqdm(params):
    x_o = sim.gen_single(p)
    y_obs = stats.calc([x_o])
    obs_zt = zscore(y_obs, axis=1)
    dist_sum_stats = np.linalg.norm((sum_stats - obs_zt), axis=1)

    norms.append(dist_sum_stats)

# get the scores
scores = N.transpose()[0]
arg_sorted = np.argsort(scores)
M = DAPSimulator(x_o['I'], x_o['dt'], -75)
s = DAPSummaryStatsStepMoments(t_on, t_off, n_summary=n_summary)
G = Default(model=M, prior=prior_unif, summary=s)  # Generator

# Runing the simulation
inf_snpe = SNPE(generator=G, n_components=n_components, n_hiddens=n_hiddens, obs=S,
                # reg_lambda=reg_lambda, pilot_samples=0, prior_norm=True)
                reg_lambda=reg_lambda, pilot_samples=0)

logs, tds, posteriors = inf_snpe.run(n_train=[n_samples], n_rounds=n_rounds,
                                     proposal=prior_unif)


# Analyse results
samples_prior = prior_unif.gen(n_samples=int(5e5))
samples_posterior = posteriors[-1].gen(n_samples=int(5e5))

print('posterior:', posteriors[-1].mean)


x_post = syn_obs_data(I, dt, posteriors[-1].mean)
idx = np.arange(0, len(x_o['data']))
rmse = np.linalg.norm(x_o['data'] - x_post['data']) / len(x_o['data'])

print('RMSE:', rmse)

simulation, axes = plt.subplots(2, 1, figsize=(16,14))
axes[0].plot(idx, x_o['I'], c='g', label='goal')
axes[0].plot(idx, x_post['I'], label='posterior')
axes[0].set_title('current')