#!/usr/bin/env python # coding: utf-8 # # Optimisation: CMA-ES # # This example shows you how to run a global optimisation with [CMA-ES](http://pints.readthedocs.io/en/latest/optimisers/cmaes.html). # # For a more elaborate example of an optimisation, see: [basic optimisation example](./optimisation-first-example.ipynb). # In[1]: from __future__ import print_function import pints import pints.toy as toy import numpy as np import matplotlib.pyplot as pl # Load a forward model model = toy.LogisticModel() # Create some toy data real_parameters = [0.015, 500] times = np.linspace(0, 1000, 1000) values = model.simulate(real_parameters, times) # Add noise values += np.random.normal(0, 10, values.shape) # Create an object with links to the model and time series problem = pints.SingleOutputProblem(model, times, values) # Select a score function score = pints.SumOfSquaresError(problem) # Select some boundaries boundaries = pints.RectangularBoundaries([0, 400], [0.03, 600]) # Perform an optimization with boundaries and hints x0 = 0.01, 450 sigma0 = [0.01, 100] found_parameters, found_value = pints.optimise( score, x0, sigma0, boundaries, method=pints.CMAES ) # Show score of true solution print('Score at true solution: ') print(score(real_parameters)) # Compare parameters with original print('Found solution: True parameters:' ) for k, x in enumerate(found_parameters): print(pints.strfloat(x) + ' ' + pints.strfloat(real_parameters[k])) # Show quality of fit pl.figure() pl.xlabel('Time') pl.ylabel('Value') pl.plot(times, values, label='Nosiy data') pl.plot(times, problem.evaluate(found_parameters), label='Fit') pl.legend() pl.show()