Package mystic :: Module forward_model

Module forward_model

source code

This module contains classes that aid in constructing cost functions. Cost function can easily be created by hand; however, mystic also provides an automated method that allows the dynamic wrapping of forward models into cost function objects.


The basic usage pattern for a cost factory is to generate a cost function from a set of data points and a corresponding set of evaluation points. The cost factory requires a "forward model factory", which is just a generator of forward model instances from a list of coefficients. The following example uses numpy.poly1d, which provides a factory for generating polynomials. An expanded version of the following can be found in `mystic.examples.example12`.

>>> # get a forward model factory, and generate some evaluation points
>>> from numpy import array, sum, poly1d, random
>>> ForwardFactory = poly1d
>>> pts = 0.1*(numpy.array([range(101)])-50.)[0]
>>> # we don't have real data, so generate some fake data from the model
>>> target = [2.,-5.,3.]
>>> datapts = [random.normal(0,1) + i for i in ForwardFactory(target)(pts)]
>>> # get a cost factory
>>> from mystic.forward_model import CostFactory
>>> F = CostFactory()
>>> # generate a cost function for the model factory
>>> costmetric = lambda x: numpy.sum(x*x)
>>> F.addModel(ForwardFactory, name='example', inputs=len(target))
>>> costfunction = F.getCostFunction(evalpts=pts, observations=datapts,
...                                  sigma=1.0, metric=costmetric)
>>> # pass the cost function to the optimizer
>>> initial_guess = [1.,-2.,1.]
>>> solution = fmin_powell(costfunction, initial_guess)

In general, a user will be required to write their own model factory. See the examples contained in `mystic.models` for more information.

The CostFactory can be used to couple models together into a single cost function. For an example, see `mystic.examples.forward_model`.

A simple utility to 'prettify' object representations of forward models.
A cost function generator.