<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Ellipsoidal nested sampling
This example demonstrates how to use ellipsoidal nested rejection sampling [1] to sample from the posterior distribution for a logistic model fitted to model-simulated data.
[1] "A nested sampling algorithm for cosmological model selection", Pia Mukherjee, David Parkinson and Andrew R. Liddle, arXiv
Step1:  Create the nested sampler that will be used to sample from the posterior.
Step2:  Run the sampler!
Step3:  Plot posterior samples versus true parameter values (dashed lines)
Step4:  Plot posterior predictive simulations versus the observed data
Step5:  Marginal likelihood estimate
Step6:  Effective sample size
<ASSISTANT_TASK:>
Python Code:
import pints
import pints.toy as toy
import numpy as np
import matplotlib.pyplot as plt
# Load a forward model
model = toy.LogisticModel()
# Create some toy data
r = 0.015
k = 500
real_parameters = [r, k]
times = np.linspace(0, 1000, 100)
signal_values = model.simulate(real_parameters, times)
# Add independent Gaussian noise
sigma = 10
observed_values = signal_values + pints.noise.independent(sigma, signal_values.shape)
# Plot
plt.plot(times,signal_values,label = 'signal')
plt.plot(times,observed_values,label = 'observed')
plt.xlabel('Time')
plt.ylabel('Values')
plt.legend()
plt.show()
Explanation: Ellipsoidal nested sampling
This example demonstrates how to use ellipsoidal nested rejection sampling [1] to sample from the posterior distribution for a logistic model fitted to model-simulated data.
[1] "A nested sampling algorithm for cosmological model selection", Pia Mukherjee, David Parkinson and Andrew R. Liddle, arXiv:astro-ph/0508461v2.
First create fake data.
End of explanation
# Create an object with links to the model and time series
problem = pints.SingleOutputProblem(model, times, observed_values)
# Create a log-likelihood function (adds an extra parameter!)
log_likelihood = pints.GaussianLogLikelihood(problem)
# Create a uniform prior over both the parameters and the new noise variable
log_prior = pints.UniformLogPrior(
    [0.01, 400, sigma * 0.5],
    [0.02, 600, sigma * 1.5])
# Create a nested ellipsoidal rejectection sampler
sampler = pints.NestedController(log_likelihood, log_prior, method=pints.NestedEllipsoidSampler)
# Set number of iterations
sampler.set_iterations(8000)
# Set the number of posterior samples to generate
sampler.set_n_posterior_samples(1600)
# Do proposals in parallel
sampler.set_parallel(True)
# Use dynamic enlargement factor
sampler._sampler.set_dynamic_enlargement_factor(1)
Explanation: Create the nested sampler that will be used to sample from the posterior.
End of explanation
samples = sampler.run()
print('Done!')
Explanation: Run the sampler!
End of explanation
# Plot output
import pints.plot
pints.plot.histogram([samples], ref_parameters=[r, k, sigma])
plt.show()
Explanation: Plot posterior samples versus true parameter values (dashed lines)
End of explanation
pints.plot.series(samples[:100], problem)
plt.show()
Explanation: Plot posterior predictive simulations versus the observed data
End of explanation
print('marginal log-likelihood = ' + str(sampler.marginal_log_likelihood())
      + ' ± ' + str(sampler.marginal_log_likelihood_standard_deviation()))
Explanation: Marginal likelihood estimate
End of explanation
print('effective sample size = ' + str(sampler.effective_sample_size()))
Explanation: Effective sample size
End of explanation
<END_TASK>