> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ionworks.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 目的関数

> Cost function formulations for parameter fitting, including SSE, MSE, RMSE, and maximum likelihood estimation

Objective functions define how the discrepancy between model predictions and experimental data is quantified during parameter fitting.

## Cost Function Formulations

### Residual Form (Array)

The residual vector represents the difference between model predictions and data:

$$
r(x) = y_{\text{model}}(x) - y_{\text{data}}
$$

where $x$ represents the parameter vector.

### Canonical Form (Scalar)

The canonical cost function aggregates the residuals into a single scalar value:

$$
\Phi(x) \propto r(x)^\top r(x) = \sum_i^N r_i(x)^2
$$

This sum-of-squares formulation is fundamental to least-squares optimization.

## Common Cost Functions

<Tabs>
  <Tab title="Sum Squared Error">
    The basic sum of squared residuals:

    $$
    \Phi_{\text{SSE}}(x) = \sum_i^N r_i^2 = r^\top r
    $$

    This can be represented in both array and scalar forms, making it compatible with all optimization algorithms.
  </Tab>

  <Tab title="Mean Squared Error">
    Normalized by the number of data points:

    $$
    \Phi_{\text{MSE}}(x) = \frac{1}{N} \sum_i^N r_i^2
    $$
  </Tab>

  <Tab title="Root Mean Squared Error">
    The square root of MSE:

    $$
    \Phi_{\text{RMSE}}(x) = \sqrt{\frac{1}{N} \sum_i^N r_i^2}
    $$

    <Warning>
      RMSE cannot be represented in a residual array form because there is no mapping from residuals to the canonical scalar form. This limits its compatibility with some optimization algorithms.
    </Warning>
  </Tab>

  <Tab title="Wasserstein Distance">
    The Wasserstein distance (also known as earth mover's distance) measures the distance between two probability distributions. Intuitively, it is the minimum amount of "work" required to transform one distribution into the other.

    For one-dimensional samples, it is computed by sorting both arrays and comparing them point-wise:

    $$
    \Phi_{\text{W}}(x) = \frac{1}{N} \sum_i^N \left| \tilde{y}_{\text{model},i}(x) - \tilde{y}_{\text{data},i} \right|
    $$

    where $\tilde{y}$ denotes the sorted samples.

    Use Wasserstein when you care about matching the *distribution* of values (for example, a histogram of cell capacities or impedance magnitudes) rather than point-wise agreement in time. Because the inputs are sorted before comparison, the result is invariant to the ordering of the model and data arrays.

    **Weighted point-cloud mode.** When one variable supplies positions and another supplies weights — for example matching a dQ/dV curve where peaks should align in voltage — pass both `position_variable` and `weight_variable` to `iws.costs.Wasserstein`. The cost then computes a single weighted Wasserstein-1 distance per objective:

    $$
    \Phi_{\text{W,weighted}}(x) = W_1\!\left(p_{\text{model}}, p_{\text{data}}, w_{\text{model}}, w_{\text{data}}\right)
    $$

    where positions $p$ come from `position_variable` and the (sign-stripped, renormalised) weights $w$ come from `weight_variable`. Both must be set together. See [Pipelines → Data Fitting → Objective Functions](/ja/pipelines/data-fitting/objective-functions#wasserstein-weighted-point-cloud-mode) for a configuration example.
  </Tab>
</Tabs>

## Maximum Likelihood Estimation (MLE)

Under the assumption of independent, identically distributed Gaussian errors, the MLE cost function is:

$$
r_{\text{MLE}}(x) = y_{\text{model}} - y_{\text{data}}
$$

$$
\Phi_{\text{MLE}}(x) = \sum_i^N r_i^2 = r^\top r
$$

This is equivalent to the sum squared error formulation, providing a probabilistic interpretation of least-squares fitting.

## Usage in ionworkspipeline

```python theme={null}
import ionworkspipeline as iwp

# Default: Sum Squared Error
objective = iwp.objectives.FittingObjective(
    options={"model": model, "data": data},
    cost=iwp.costs.SumSquaredError()
)

# Mean Squared Error
objective = iwp.objectives.FittingObjective(
    options={"model": model, "data": data},
    cost=iwp.costs.MeanSquaredError()
)

# Root Mean Squared Error (scalar-only)
objective = iwp.objectives.FittingObjective(
    options={"model": model, "data": data},
    cost=iwp.costs.RMSE()
)

# Wasserstein distance (distributional fit)
objective = iwp.objectives.FittingObjective(
    options={"model": model, "data": data},
    cost=iwp.costs.Wasserstein()
)
```

<Tip>
  For most optimization algorithms, the default sum-squared-error formulation provides the best compatibility and performance. Use MSE or RMSE when you need interpretable, scale-independent metrics.
</Tip>

<Note>
  データフィットで目的関数とコスト関数を組み合わせる方法については、[Pipelines → Data Fitting → Objective Functions](/ja/pipelines/data-fitting/objective-functions) を参照してください。
</Note>
