class_docstr: A cost function generator.
      
docstr: Get a cost function that allows simultaneous evaluation of all forward models
  for the same set of evaluation points and observation points.
  
  Inputs:
      evalpts -- a list of evaluation points
      observations -- a list of data points
      sigma   -- a scaling factor applied to the raw cost
      metric  -- the cost metric object
  
  The cost metric should be a function of one parameter (possibly an array)
  that returns a scalar. The default is L2. When called, the "misfit" will
  be passed in.
  
  NOTE: Input parameters WILL go through filters registered as inputCheckers.
  
  Example:
      >>> import numpy as np
      >>> C = CostFactory()
      >>> C.addModel(np.poly, inputs=3)
      >>> x = np.array([-2., -1., 0., 1., 2.])
      >>> y = np.array([-4., -2., 0., 2., 4.])
      >>> F = C.getCostFunction(x, y, metric=lambda x: np.sum(x))
      >>> F([1,0,0])
      0.0
      >>> F([2,0,0])
      10.0
      >>> F = C.getCostFunction(x, y)
      >>> F([2,0,0])
      34.0
          
docstr_tok: ["Get","a","cost","function","that","allows","simultaneous","evaluation","of","all","forward","models","for","the","same","set","of","evaluation","points","and","observation","points",".","Inputs",":","evalpts","--","a","list","of","evaluation","points","observations","--","a","list","of","data","points","sigma","--","a","scaling","factor","applied","to","the","raw","cost","metric","--","the","cost","metric","object","The","cost","metric","should","be","a","function","of","one","parameter","(","possibly","an","array",")","that","returns","a","scalar",".","The","default","is","L2",".","When","called",",","the","``","misfit","''","will","be","passed","in",".","NOTE",":","Input","parameters","WILL","go","through","filters","registered","as","inputCheckers",".","Example",":",">",">",">","import","numpy","as","np",">",">",">","C","=","CostFactory","(",")",">",">",">","C.addModel","(","np.poly",",","inputs=3",")",">",">",">","x","=","np.array","(","[","-2.",",","-1.",",","0.",",","1.",",","2",".","]",")",">",">",">","y","=","np.array","(","[","-4.",",","-2.",",","0.",",","2.",",","4",".","]",")",">",">",">","F","=","C.getCostFunction","(","x",",","y",",","metric=lambda","x",":","np.sum","(","x",")",")",">",">",">","F","(","[","1,0,0","]",")","0.0",">",">",">","F","(","[","2,0,0","]",")","10.0",">",">",">","F","=","C.getCostFunction","(","x",",","y",")",">",">",">","F","(","[","2,0,0","]",")","34.0"]
code: def getCostFunction(
      self,
      evalpts,
      observations,
      sigma=None,
      metric=lambda x: sum(x * x),
  ):
      """
      Get a cost function that allows simultaneous evaluation of all forward models
      for the same set of evaluation points and observation points.
  
      Inputs:
          evalpts -- a list of evaluation points
          observations -- a list of data points
          sigma   -- a scaling factor applied to the raw cost
          metric  -- the cost metric object
  
      The cost metric should be a function of one parameter (possibly an array)
      that returns a scalar. The default is L2. When called, the "misfit" will
      be passed in.
  
      NOTE: Input parameters WILL go through filters registered as inputCheckers.
  
      Example:
          >>> import numpy as np
          >>> C = CostFactory()
          >>> C.addModel(np.poly, inputs=3)
          >>> x = np.array([-2., -1., 0., 1., 2.])
          >>> y = np.array([-4., -2., 0., 2., 4.])
          >>> F = C.getCostFunction(x, y, metric=lambda x: np.sum(x))
          >>> F([1,0,0])
          0.0
          >>> F([2,0,0])
          10.0
          >>> F = C.getCostFunction(x, y)
          >>> F([2,0,0])
          34.0
      """
  
      # XXX: better interface for sigma?
      def _(params):
          ind = 0
          for F, n, ofilt, icheck in zip(
              self._forwardFactories,
              self._inputs,
              self._outputFilters,
              self._inputCheckers,
          ):
              # check input  #XXX: is this worthwile to do?
              my_params = params[ind : ind + n]
              checkQ = icheck(my_params, evalpts)
              if checkQ is not None:
                  # some parameters are out of range... returns "cost"
                  return checkQ
  
              Gm = F(params[ind : ind + n])
              if ind == 0:
                  x = ofilt(Gm(evalpts))
              else:
                  x = x + ofilt(Gm(evalpts))
              ind = ind + n
          if sigma is None:
              x = x - observations
          else:
              x = (x - observations) / sigma
          # return sum(real((conjugate(x)*x)))
          # return sum(x*x)
          return metric(x)
  
      return _
  
file_path: mystic/mystic/forward_model.py
imports: from mystic.filters import identity, component
  from mystic.filters import null_check
  from numpy import pi, sqrt, array, mgrid, random, real, conjugate, arange, sum
label_desc: Use image node_id 5 for calling the CostFactory obj's underlying member method code with example usage: obj.getCostFunction(evalpts, observations, sigma, metric) and returns: _, metric, checkQ