Esempio n. 1
0
def residuals(fitparams, params, func, data, uncert, indparams, stepsize,
              pmin, pmax, prior, priorlow, priorup):
  """
  Calculate the weighted residuals between data and a model, accounting
  also for parameter priors.

  Parameters
  ----------
  fitparams: 1D ndarray
     The model free parameters.
  params: 1D ndarray
     Model parameters (including fixed and shared parameters).
  func: Callable
     Function that models data, being called as:
     model = func(params, *indparams)
  data: 1D ndarray
     The data set being modeled.
  uncert: 1D ndarray
     Data uncertainties.
  indparams: Tuple
     Additional arguments for the func function.
  stepsize: 1D ndarray
     Array indicating which params are free params (stepsize > 0), the
     fixed params (stepsize=0) and shared parameters (stepsize < 0).
  pmin: 1D ndarray
     Lower boundaries of params.
  pmax: 1D ndarray
     Upper boundaries of params.
  prior: 1D ndarray
     Priors array.
  priorlow: 1D ndarray
     Priors lower uncertainties.
  priorup: 1D ndarray
     Priors upper uncertainties.

  Returns
  -------
  Array of weighted data-model and prior-params residuals.
  """
  # Get free and shared indices:
  ifree  = np.where(stepsize > 0)[0]
  ishare = np.where(stepsize < 0)[0]

  # Combine fitparams into func params:
  params[ifree] = fitparams

  # Keep parameters within boundaries:
  params = np.clip(params, pmin, pmax)

  # Update shared parameters:
  for s in ishare:
    params[s] = params[-int(stepsize[s])-1]

  # Compute model:
  fargs = [params] + indparams
  model = func(*fargs)

  # Find the parameters that have prior:
  iprior = np.where(priorlow != 0)[0]
  prioroff = params - prior

  # Calculate residuals:
  residuals = cs.residuals(model, data, uncert,
                           prioroff[iprior], priorlow[iprior], priorup[iprior])
  #print("Params: %s"%str(params))
  #print("Prior:  %s"%str(prior))
  #print(prioroff[iprior], priorlow[iprior], priorup[iprior])
  #print(residuals[-4:])
  return residuals
Esempio n. 2
0
def residuals(fitparams, params, func, data, uncert, indparams, stepsize, pmin,
              pmax, prior, priorlow, priorup):
    """
  Calculate the weighted residuals between data and a model, accounting
  also for parameter priors.

  Parameters
  ----------
  fitparams: 1D ndarray
     The model free parameters.
  params: 1D ndarray
     Model parameters (including fixed and shared parameters).
  func: Callable
     Function that models data, being called as:
     model = func(params, *indparams)
  data: 1D ndarray
     The data set being modeled.
  uncert: 1D ndarray
     Data uncertainties.
  indparams: Tuple
     Additional arguments for the func function.
  stepsize: 1D ndarray
     Array indicating which params are free params (stepsize > 0), the
     fixed params (stepsize=0) and shared parameters (stepsize < 0).
  pmin: 1D ndarray
     Lower boundaries of params.
  pmax: 1D ndarray
     Upper boundaries of params.
  prior: 1D ndarray
     Priors array.
  priorlow: 1D ndarray
     Priors lower uncertainties.
  priorup: 1D ndarray
     Priors upper uncertainties.

  Returns
  -------
  Array of weighted data-model and prior-params residuals.
  """
    # Get free and shared indices:
    ifree = np.where(stepsize > 0)[0]
    ishare = np.where(stepsize < 0)[0]

    # Combine fitparams into func params:
    params[ifree] = fitparams

    # Keep parameters within boundaries:
    params = np.clip(params, pmin, pmax)

    # Update shared parameters:
    for s in ishare:
        params[s] = params[-int(stepsize[s]) - 1]

    # Compute model:
    fargs = [params] + indparams
    model = func(*fargs)

    # Find the parameters that have prior:
    iprior = np.where(priorlow != 0)[0]
    prioroff = params - prior

    # Calculate residuals:
    residuals = cs.residuals(model, data, uncert, prioroff[iprior],
                             priorlow[iprior], priorup[iprior])
    #print("Params: %s"%str(params))
    #print("Prior:  %s"%str(prior))
    #print(prioroff[iprior], priorlow[iprior], priorup[iprior])
    #print(residuals[-4:])
    return residuals
Esempio n. 3
0
def residuals(fitparams, params, func, data, uncert, indparams, stepsize,
              prior, priorlow, priorup, ifree, ishare, iprior):
    """
  Calculate the weighted residuals between data and a model, accounting
  also for parameter priors.

  Parameters
  ----------
  fitparams: 1D ndarray
     The model free parameters.
  params: 1D ndarray
     The model parameters (including fixed and shared parameters).
  func: Callable
     The fitting function to model the data, called as:
        model = func(params, *indparams)
  data: 1D ndarray
     Dependent data fitted by func.
  uncert: 1D ndarray
     1-sigma uncertainty of data.
  indparams: tuple
     Additional arguments required by func (if required).
  stepsize: 1D ndarray
     Parameters' jump scale (same size as params).
     If the stepsize is positive, the parameter is free for fitting.
     If the stepsize is 0, keep the parameter value fixed.
     If the stepsize is a negative integer, copy (share) the parameter value
       from params[np.abs(stepsize)+1], which can be free or fixed.
  prior: 1D ndarray
     Model parameters' (Gaussian) prior values (same size as params).
     Considered only when priolow != 0.  priorlow and priorup are the
     lower and upper 1-sigma width of the Gaussian prior, respectively.
  priorlow: 1D ndarray
     Parameters' lower 1-sigma Gaussian prior (same size as params).
  priorup: 1D ndarray
     Paraneters' upper 1-sigma Gaussian prior (same size as params).
  ifree: 1D bool ndarray
     Indices of the free parameters in params.
  ishare: 1D bool ndarray
     Indices of the shared parameters in params.
  iprior: 1D bool ndarray
     Indices of the prior parameters in params.

  Returns
  -------
  Array of weighted data-model and prior-params residuals.
  """
    # Update params with fitparams:
    params[ifree] = fitparams

    # Update shared parameters:
    for s in ishare:
        params[s] = params[-int(stepsize[s]) - 1]

    # Compute model:
    model = func(params, *indparams)

    # Find the parameters that have prior:
    prioroff = params - prior

    # Calculate residuals:
    residuals = cs.residuals(model, data, uncert, prioroff[iprior],
                             priorlow[iprior], priorup[iprior])
    return residuals
Esempio n. 4
0
def residuals(fitparams, params, func, data, uncert, indparams, stepsize,
              prior, priorlow, priorup, ifree, ishare, iprior):
  """
  Calculate the weighted residuals between data and a model, accounting
  also for parameter priors.

  Parameters
  ----------
  fitparams: 1D ndarray
     The model free parameters.
  params: 1D ndarray
     The model parameters (including fixed and shared parameters).
  func: Callable
     The fitting function to model the data, called as:
        model = func(params, *indparams)
  data: 1D ndarray
     Dependent data fitted by func.
  uncert: 1D ndarray
     1-sigma uncertainty of data.
  indparams: tuple
     Additional arguments required by func (if required).
  stepsize: 1D ndarray
     Parameters' jump scale (same size as params).
     If the stepsize is positive, the parameter is free for fitting.
     If the stepsize is 0, keep the parameter value fixed.
     If the stepsize is a negative integer, copy (share) the parameter value
       from params[np.abs(stepsize)+1], which can be free or fixed.
  prior: 1D ndarray
     Model parameters' (Gaussian) prior values (same size as params).
     Considered only when priolow != 0.  priorlow and priorup are the
     lower and upper 1-sigma width of the Gaussian prior, respectively.
  priorlow: 1D ndarray
     Parameters' lower 1-sigma Gaussian prior (same size as params).
  priorup: 1D ndarray
     Paraneters' upper 1-sigma Gaussian prior (same size as params).
  ifree: 1D bool ndarray
     Indices of the free parameters in params.
  ishare: 1D bool ndarray
     Indices of the shared parameters in params.
  iprior: 1D bool ndarray
     Indices of the prior parameters in params.

  Returns
  -------
  Array of weighted data-model and prior-params residuals.
  """
  # Update params with fitparams:
  params[ifree] = fitparams

  # Update shared parameters:
  for s in ishare:
    params[s] = params[-int(stepsize[s])-1]

  # Compute model:
  model = func(params, *indparams)

  # Find the parameters that have prior:
  prioroff = params - prior

  # Calculate residuals:
  residuals = cs.residuals(model, data, uncert,
                           prioroff[iprior], priorlow[iprior], priorup[iprior])
  return residuals