$$\renewcommand\AA{\unicode{x212B}}$$

# CalculateCostFunction v1¶

## Summary¶

Calculate cost function for a function and a data set in a workspace.

## Properties¶

Name

Direction

Type

Default

Description

Function

InOut

Function

Mandatory

Parameters defining the fitting function and its initial values

InputWorkspace

Input

Workspace

Mandatory

Name of the input Workspace

IgnoreInvalidData

Input

boolean

False

Flag to ignore infinities, NaNs and data with zero errors.

DomainType

Input

string

Simple

The type of function domain to use: Simple, Sequential, or Parallel. Allowed values: [‘Simple’, ‘Sequential’, ‘Parallel’]

EvaluationType

Input

string

CentrePoint

The way the function is evaluated on histogram data sets. If value is “CentrePoint” then function is evaluated at centre of each bin. If it is “Histogram” then function is integrated within the bin and the integrals returned. Allowed values: [‘CentrePoint’, ‘Histogram’]

StepSizeMethod

Input

string

Default

The way the step size is calculated for numerical derivatives. See the section about step sizes in the Fit algorithm documentation to understand the difference between “Default” and “Sqrt epsilon”. Allowed values: [‘Default’, ‘Sqrt epsilon’]

Input

number

0

A value of the peak radius the peak functions should use. A peak radius defines an interval on the x axis around the centre of the peak where its values are calculated. Values outside the interval are not calculated and assumed zeros.Numerically the radius is a whole number of peak widths (FWHM) that fit into the interval on each side from the centre. The default value of 0 means the whole x axis.

CostFunction

InOut

string

Least squares

The cost function to be used for the fit, default is Least squares. Allowed values: [‘Least squares’, ‘Poisson’, ‘Rwp’, ‘Unweighted least squares’]

Value

Output

number

Output value of the cost function.

## Description¶

Calculates a value of any available cost function. Returns the calculated value in the Value property. The input properties have the same meaning and behaviour as in Fit v1.

## Usage¶

Example

import numpy as np

# Create a data set
x = np.linspace(0,1,10)
y = 1.0 + 2.0 * x
e = np.sqrt(y)
ws = CreateWorkspace(DataX=x, DataY=y, DataE=e)

# Define a function
func = 'name=LinearBackground,A0=1.1,A1=1.9'

# Calculate the chi squared by default
value = CalculateCostFunction(func, ws)
print('Value of least squares is {:.13f}'.format(value))

# Calculate the unweighted least squares
value = CalculateCostFunction(func, ws, CostFunction='Unweighted least squares')
print('Value of unweighted least squares is {:.13f}'.format(value))


Output:

Value of least squares is 0.0133014391988
Value of unweighted least squares is 0.0175925925926


Categories: AlgorithmIndex | Optimization