Changeset 750
- Timestamp:
- 09/07/14 16:12:20 (21 months ago)
- Location:
- mystic
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
mystic/examples/NOTES
r470 r750 77 77 x = (array([range(101)])-50.)[0] 78 78 F = CostFactory() 79 F.addModel(ForwardPolyFactory, 'poly',3)79 F.addModel(ForwardPolyFactory,3,'poly') 80 80 myCost = F.getCostFunction(evalpts=x, observations=datapts) 81 81 -
mystic/examples/example12.py
r722 r750 38 38 from numpy import sum 39 39 F = CostFactory() 40 F.addModel(ForwardPolyFactory, "noisy_polynomial",ndim)40 F.addModel(ForwardPolyFactory,ndim,"noisy_polynomial") 41 41 return F.getCostFunction(evalpts=evalpts,observations=datapts, \ 42 42 metric=lambda x: sum(x*x),sigma=1000.) -
mystic/examples/forward_model.py
r713 r750 21 21 22 22 A = CostFactory() 23 A.addModel(ForwardMogiFactory, 'mogi1', 4, outputFilter=PickComponent(2))24 A.addModel(ForwardMogiFactory, 'mogi2', 4, outputFilter=PickComponent(2))23 A.addModel(ForwardMogiFactory, 4, 'mogi1', outputFilter=PickComponent(2)) 24 A.addModel(ForwardMogiFactory, 4, 'mogi2', outputFilter=PickComponent(2)) 25 25 fe = A.getForwardEvaluator(stations) 26 26 p = [random.random() for i in range(8)] -
mystic/examples/test_getCost.py
r722 r750 72 72 ##myCost = PolyCostFactory(target,x) 73 73 F = CostFactory() 74 F.addModel(ForwardPolyFactory, 'poly',len(target))74 F.addModel(ForwardPolyFactory,len(target),'poly') 75 75 myCost = F.getCostFunction(evalpts=x, observations=datapts) 76 76 -
mystic/examples/test_lorentzian.py
r721 r750 101 101 from mystic.forward_model import CostFactory 102 102 CF = CostFactory() 103 CF.addModel(F, 'lorentz', ND)103 CF.addModel(F, ND, 'lorentz') 104 104 myCF = CF.getCostFunction(binsc, histo/N) 105 105 sol, steps = de_solve(myCF) -
mystic/examples/test_mogi3.py
r713 r750 36 36 if __name__ == '__main__': 37 37 F = CostFactory() 38 F.addModel(ForwardMogiFactory, 'mogi1', 4, outputFilter = PickComponent(2))38 F.addModel(ForwardMogiFactory, 4, 'mogi1', outputFilter = PickComponent(2)) 39 39 myCostFunction = F.getCostFunction(evalpts = stations, observations = data_z) 40 40 print F -
mystic/examples/test_mogi4.py
r713 r750 42 42 43 43 F = CostFactory() 44 F.addModel(ForwardMogiFactory, 'mogi1', 4, outputFilter = PickComponent(2, -1))45 F.addModel(ForwardMogiFactory, 'mogi2', 4, outputFilter = PickComponent(2, -1))44 F.addModel(ForwardMogiFactory, 4, 'mogi1', outputFilter = PickComponent(2, -1)) 45 F.addModel(ForwardMogiFactory, 4, 'mogi2', outputFilter = PickComponent(2, -1)) 46 46 myCostFunction = F.getCostFunction(evalpts = stations, observations = data_z) 47 47 print F -
mystic/models/abstract_model.py
r713 r750 104 104 datapts = self.evaluate(target,pts) 105 105 F = CF() 106 F.addModel(self.ForwardFactory, self.__name__,len(target))106 F.addModel(self.ForwardFactory,len(target),self.__name__) 107 107 self.__cost__ = F.getCostFunction(evalpts=pts,observations=datapts,sigma=self.__sigma__,metric=self.__metric__) 108 108 return self.__cost__ … … 112 112 and evaluation points""" 113 113 F = CF() 114 F.addModel(self.ForwardFactory, self.__name__,nparams)114 F.addModel(self.ForwardFactory,nparams,self.__name__) 115 115 self.__cost__ = F.getCostFunction(evalpts=pts,observations=datapts,sigma=self.__sigma__,metric=self.__metric__) 116 116 return self.__cost__ -
mystic/models/br8.py
r713 r750 51 51 datapts = self.evaluate(target,pts) 52 52 F = CF() 53 F.addModel(self.ForwardFactory, self.__name__,len(target))53 F.addModel(self.ForwardFactory,len(target),self.__name__) 54 54 self.__cost__ = F.getCostFunction(evalpts=pts,observations=datapts,sigma=sqrt(datapts),metric=self.__metric__) 55 55 return self.__cost__ … … 58 58 """generates a cost function instance from datapoints & evaluation points""" 59 59 F = CF() 60 F.addModel(self.ForwardFactory, self.__name__,nparams)60 F.addModel(self.ForwardFactory,nparams,self.__name__) 61 61 self.__cost__ = F.getCostFunction(evalpts=pts,observations=datapts,sigma=sqrt(datapts),metric=self.__metric__) 62 62 return self.__cost__ -
mystic/mystic/forward_model.py
r721 r750 17 17 The basic usage pattern for a cost factory is to generate a cost function 18 18 from a set of data points and a corresponding set of evaluation points. 19 The cost factory requires a " forwardmodel factory", which is just a generator20 of forward modelinstances from a list of coefficients. The following example19 The cost factory requires a "model factory", which is just a generator 20 of model function instances from a list of coefficients. The following example 21 21 uses numpy.poly1d, which provides a factory for generating polynomials. An 22 22 expanded version of the following can be found in `mystic.examples.example12`. 23 23 24 >>> # get a forward model factory, and generate some evaluation points 25 >>> from numpy import array, sum, poly1d, random 26 >>> ForwardFactory = poly1d 27 >>> pts = 0.1*(array([range(101)])-50.)[0] 24 >>> # get a model factory 25 >>> import numpy as np 26 >>> FunctionFactory = np.poly1d 28 27 >>> 29 >>> # we don't have real data, so generate some fake data from the model 28 >>> # generate some evaluation points 29 >>> xpts = 0.1 * np.arange(-50.,51.) 30 >>> 31 >>> # we don't have real data, so generate fake data from target and model 30 32 >>> target = [2.,-5.,3.] 31 >>> datapts = [random.normal(0,1) + i for i in ForwardFactory(target)(pts)] 33 >>> ydata = FunctionFactory(target)(xpts) 34 >>> noise = np.random.normal(0,1,size=len(ydata)) 35 >>> ydata = ydata + noise 32 36 >>> 33 37 >>> # get a cost factory 34 38 >>> from mystic.forward_model import CostFactory 35 >>> F= CostFactory()39 >>> C = CostFactory() 36 40 >>> 37 41 >>> # generate a cost function for the model factory 38 >>> costmetric = lambda x:sum(x*x)39 >>> F.addModel(ForwardFactory, name='example', inputs=len(target))40 >>> cost function = F.getCostFunction(evalpts=pts, observations=datapts,41 ... sigma=1.0, metric=costmetric)42 >>> metric = lambda x: np.sum(x*x) 43 >>> C.addModel(FunctionFactory, inputs=len(target)) 44 >>> cost = C.getCostFunction(evalpts=xpts, observations=ydata, 45 ... sigma=1.0, metric=metric) 42 46 >>> 43 47 >>> # pass the cost function to the optimizer 44 48 >>> initial_guess = [1.,-2.,1.] 45 >>> solution = fmin_powell(costfunction, initial_guess) 49 >>> solution = fmin_powell(cost, initial_guess) 50 >>> print solution 51 [ 2.00495233 -5.0126248 2.72873734] 52 46 53 47 54 In general, a user will be required to write their own model factory. … … 59 66 from numpy import pi, sqrt, array, mgrid, random, real, conjugate, arange, sum 60 67 #from numpy.random import rand 61 62 63 class ForwardModel1(object):64 """65 A simple utility to 'prettify' object representations of forward models.66 """67 def __init__(self, func, inputs, outputs):68 """69 Takes three initial inputs:70 func -- the forward model71 inputs -- the number of inputs72 outputs -- the number of outputs73 74 Example:75 >>> f = lambda x: sum(x*x)76 >>> fwd = ForwardModel1(f,1,1)77 >>> fwd78 func: ['x0'] -> ['y0']79 >>> fwd(1)80 181 >>> fwd(2)82 483 """84 self._func = func85 self._m = inputs86 self._n = outputs87 self._inNames = ['x%d'%i for i in range(self._m)]88 self._outNames = ['y%d'%i for i in range(self._n)]89 90 def __call__(self, *args, **kwds):91 return self._func(*args, **kwds)92 93 def __repr__(self):94 return 'func: %s -> %s' % (self._inNames, self._outNames)95 68 96 69 class CostFactory(object): … … 114 87 pass 115 88 116 def addModel(self, model, name, inputs, outputFilter = Identity, inputChecker =NullChecker):89 def addModel(self, model, inputs, name=None, outputFilter=Identity, inputChecker=NullChecker): 117 90 """ 118 91 Adds a forward model factory to the cost factory. … … 120 93 Inputs: 121 94 model -- a callable function factory object 95 inputs -- number of input arguments to model 122 96 name -- a string representing the model name 123 inputs -- number of input arguments to model 124 """ 125 if name in self._names: 126 print "Model [%s] already in database." % name 127 raise AssertionError 97 98 Example: 99 >>> import numpy as np 100 >>> C = CostFactory() 101 >>> C.addModel(np.poly, inputs=3) 102 """ 103 if name is None: 104 name = dill.source.getname(model) 105 if name is None: 106 for i in range(len(self._names)+1): 107 name = 'model'+str(i) 108 if name not in self._names: break 109 elif name in self._names: 110 print "Model [%s] already in database." % name 111 raise AssertionError 128 112 self._names.append(name) 129 113 self._forwardFactories.append(model) … … 145 129 #NOTE: better to replace "old-style" addModel above? 146 130 if name in self._names: 147 148 131 print "Model [%s] already in database." % name 132 raise AssertionError 149 133 self._names.append(name) 150 134 self._forwardFactories.append(model) … … 162 146 Inputs: 163 147 evalpts -- a list of evaluation points 148 149 Example: 150 >>> import numpy as np 151 >>> C = CostFactory() 152 >>> C.addModel(np.poly, inputs=3) 153 >>> F = C.getForwardEvaluator([1,2,3,4,5]) 154 >>> F([1,0,0]) 155 [array([ 1, 4, 9, 16, 25])] 156 >>> F([0,1,0]) 157 [array([1, 2, 3, 4, 5])] 164 158 """ 165 159 #NOTE: does NOT go through inputChecker … … 187 181 188 182 NOTE: Input parameters do NOT go through filters registered as inputCheckers. 183 184 Example: 185 >>> import numpy as np 186 >>> C = CostFactory() 187 >>> C.addModel(np.poly, inputs=3) 188 >>> x = np.array([-2., -1., 0., 1., 2.]) 189 >>> y = np.array([-4., -2., 0., 2., 4.]) 190 >>> F = C.getVectorCostFunction(x, y) 191 >>> F([1,0,0]) 192 0.0 193 >>> F([2,0,0]) 194 10.0 189 195 """ 190 196 def _(params): 191 197 forward = self.getForwardEvaluator(evalpts) 192 return sum(forward(params) ) - observations198 return sum(forward(params) - observations) 193 199 return _ 194 200 … … 209 215 210 216 NOTE: Input parameters WILL go through filters registered as inputCheckers. 217 218 Example: 219 >>> import numpy as np 220 >>> C = CostFactory() 221 >>> C.addModel(np.poly, inputs=3) 222 >>> x = np.array([-2., -1., 0., 1., 2.]) 223 >>> y = np.array([-4., -2., 0., 2., 4.]) 224 >>> F = C.getCostFunction(x, y, metric=lambda x: np.sum(x)) 225 >>> F([1,0,0]) 226 0.0 227 >>> F([2,0,0]) 228 10.0 229 >>> F = C.getCostFunction(x, y) 230 >>> F([2,0,0]) 231 34.0 211 232 """ 212 233 #XXX: better interface for sigma? … … 250 271 251 272 NOTE: Input parameters do NOT go through filters registered as inputCheckers. 273 274 Example: 275 >>> import numpy as np 276 >>> C = CostFactory() 277 >>> C.addModel(np.poly, inputs=3) 278 >>> x = np.array([-2., -1., 0., 1., 2.]) 279 >>> y = np.array([-4., -2., 0., 2., 4.]) 280 >>> F = C.getCostFunctionSlow(x, y) 281 >>> F([1,0,0]) 282 0.0 283 >>> F([2,0,0]) 284 100.0 252 285 """ 253 286 #XXX: update interface to allow metric?
Note: See TracChangeset
for help on using the changeset viewer.