# fit function

The least-squares fit of a combination of functions through points.

## How to write it

### fit(points, basis, x)

The combination whose squared distances to the points add up the least.

**points**

The points, as a list of pairs \[x, y\]. Takes a list.

**basis**

The functions to combine, in x, such as \[1, x, x^2\]. Takes a list.

**x**

The variable of the basis. Takes a variable.

### fit(points, basis, x, distance = 1|2|inf)

The combination whose deviations from the points are least in that distance, when exactly one combination is.

**points**

The points, as a list of pairs \[x, y\]. Takes a list.

**basis**

The functions to combine, in x, such as \[1, x, x^2\]. Takes a list.

**x**

The variable of the basis. Takes a variable.

**distance = 1|2|inf**

Which deviations count: 1 their sum, 2 the sum of their squares (the default), inf the largest.

## Examples

- $\displaystyle (0, 1), (1, 3), (2, 5), (3, 8) = \frac{23 x}{10}+\frac{4}{5}$

  `fit([[0, 1], [1, 3], [2, 5], [3, 8]], [1, x], x)`

  In the solver, `fit([[0, 1], [1, 3], [2, 5], [3, 8]], [1, x], x)` gives `23x/10 + 4/5`.

  [Open in the solver](https://hilberon.com/?q=fit%28%5B%5B0%2C%201%5D%2C%20%5B1%2C%203%5D%2C%20%5B2%2C%205%5D%2C%20%5B3%2C%208%5D%5D%2C%20%5B1%2C%20x%5D%2C%20x%29)
- $\displaystyle (0, 1), (1, 3), (2, 5), (3, 8) \text{ by } \left\{ 1, x, x^{2} \right\} = \frac{x^{2}}{4}+\frac{31 x}{20}+\frac{21}{20}$

  `fit([[0, 1], [1, 3], [2, 5], [3, 8]], [1, x, x^2], x)`

  In the solver, `fit([[0, 1], [1, 3], [2, 5], [3, 8]], [1, x, x^2], x)` gives `x^2/4 + 31x/20 + 21/20`.

  [Open in the solver](https://hilberon.com/?q=fit%28%5B%5B0%2C%201%5D%2C%20%5B1%2C%203%5D%2C%20%5B2%2C%205%5D%2C%20%5B3%2C%208%5D%5D%2C%20%5B1%2C%20x%2C%20x%5E2%5D%2C%20x%29)
- $\displaystyle (0, 0), (1, 1), (2, 2), (3, 3), (4, 10) = x$

  `fit([[0, 0], [1, 1], [2, 2], [3, 3], [4, 10]], [1, x], x, distance = 1)`

  In the solver, `fit([[0, 0], [1, 1], [2, 2], [3, 3], [4, 10]], [1, x], x, distance = 1)` gives `x`.

  [Open in the solver](https://hilberon.com/?q=fit%28%5B%5B0%2C%200%5D%2C%20%5B1%2C%201%5D%2C%20%5B2%2C%202%5D%2C%20%5B3%2C%203%5D%2C%20%5B4%2C%2010%5D%5D%2C%20%5B1%2C%20x%5D%2C%20x%2C%20distance%20%3D%201%29)
- $\displaystyle (0, 0), (1, 1), (2, 2), (3, 3), (4, 10) = \frac{5 x}{2}-\frac{9}{4}$

  `fit([[0, 0], [1, 1], [2, 2], [3, 3], [4, 10]], [1, x], x, distance = inf)`

  In the solver, `fit([[0, 0], [1, 1], [2, 2], [3, 3], [4, 10]], [1, x], x, distance = inf)` gives `5x/2 - 9/4`.

  [Open in the solver](https://hilberon.com/?q=fit%28%5B%5B0%2C%200%5D%2C%20%5B1%2C%201%5D%2C%20%5B2%2C%202%5D%2C%20%5B3%2C%203%5D%2C%20%5B4%2C%2010%5D%5D%2C%20%5B1%2C%20x%5D%2C%20x%2C%20distance%20%3D%20inf%29)

## Related functions

- [geometric_brownian_motion_process](https://hilberon.com/library/geometric_brownian_motion_process)

  A process that grows at rate mu with relative noise sigma, started at x0.
- [exponential_distribution](https://hilberon.com/library/exponential_distribution)

  The waiting time for events that come at rate lambda.
- [geometric_sde_path](https://hilberon.com/library/geometric_sde_path)

  The path of dX = mu X dt + sigma X dW, as a function of the Wiener process's value w at time t.
- [expectation](https://hilberon.com/library/expectation)

  The expected value of an expression of a variable with a distribution or a process.
- [ito_formula](https://hilberon.com/library/ito_formula)

  Itô's lemma: the differential of an expression of states that follow an Itô process.
- [distributed](https://hilberon.com/library/distributed)

  A variable and the distribution it follows, as probability and expectation read it.

---
Source: https://hilberon.com/library/fit
