## Least square fit


```python
from jupyterthemes import jtplot
jtplot.style(theme='onedork', context='notebook', ticks=True, grid=False)
```


```python
import sympy as sp
import pandas as pd
import numpy as np
```


```python
c_0,c_1 = sp.symbols('c_0 c_1')
epsilon = sp.IndexedBase("epsilon")
y = sp.IndexedBase("y")
x = sp.IndexedBase("x")
i, N = sp.var("i,N", integer=True)

linear_equation = sp.Eq(y[i], c_0+c_1*x[i]+epsilon[i])
```


```python
linear_equation
```




$\displaystyle {y}_{i} = c_{0} + c_{1} {x}_{i} + {\epsilon}_{i}$




```python
epsilon_equation = sp.Eq(epsilon[i],sp.solve(linear_equation, epsilon[i])[0])
epsilon_equation
```




$\displaystyle {\epsilon}_{i} = - c_{0} - c_{1} {x}_{i} + {y}_{i}$



## Criteria 1
Mean error should be 0. 

$mean(\epsilon_i)=0 $


```python
criteria_1_equation = sp.Eq(1/N*sp.Sum(epsilon[i], (i, 1, N)),0)
criteria_1_equation
```




$\displaystyle \frac{\sum_{i=1}^{N} {\epsilon}_{i}}{N} = 0$




```python
criteria_1_equation = sp.Eq(sp.Sum(sp.solve(epsilon_equation,epsilon[i])[0], (i, 1, N)),0)
criteria_1_equation
```




$\displaystyle \sum_{i=1}^{N} \left(- c_{0} - c_{1} {x}_{i} + {y}_{i}\right) = 0$




```python
criteria_1_equation = sp.Eq(sp.expand(criteria_1_equation.lhs).doit(),0)
criteria_1_equation
```




$\displaystyle - N c_{0} + \sum_{i=1}^{N} - c_{1} {x}_{i} + \sum_{i=1}^{N} {y}_{i} = 0$




```python
criteria_1_equation = sp.Eq(c_0,sp.solve(criteria_1_equation,c_0)[0])
criteria_1_equation
```




$\displaystyle c_{0} = \frac{\sum_{i=1}^{N} \left(- c_{1} {x}_{i} + {y}_{i}\right)}{N}$




```python
criteria_1_equation = criteria_1_equation.expand().doit()
criteria_1_equation
```




$\displaystyle c_{0} = \frac{\sum_{i=1}^{N} - c_{1} {x}_{i}}{N} + \frac{\sum_{i=1}^{N} {y}_{i}}{N}$




```python
S_y,S_x,x_i,y_i = sp.symbols('S_y S_x x_i y_i')
```


```python
criteria_1_equation = criteria_1_equation.subs([(y[i],y_i),
                         (x[i],x_i), 
                         ])
```


```python
criteria_1_equation
```




$\displaystyle c_{0} = \frac{\sum_{i=1}^{N} - c_{1} {x}_{i}}{N} + \frac{\sum_{i=1}^{N} {y}_{i}}{N}$




```python
S_y,S_x = sp.symbols('S_y S_x')
S_y_equation = sp.Eq(S_y, sp.Sum(y_i,(i, 1, N)))
S_y_equation
```




$\displaystyle S_{y} = \sum_{i=1}^{N} y_{i}$




```python
S_x_equation = sp.Eq(S_x, sp.Sum(x_i,(i, 1, N)))
S_x_equation
```




$\displaystyle S_{x} = \sum_{i=1}^{N} x_{i}$




```python
eqs=(S_y_equation,
     S_x_equation,
     criteria_1_equation
)
sp.solve(eqs, c_0, S_y,S_x,)
```




    {S_x: x_i*Sum(1, (i, 1, N)),
     S_y: y_i*Sum(1, (i, 1, N)),
     c_0: Sum(-c_1*x[i] + y[i], (i, 1, N))/N}




```python
criteria_1_equation.subs(S_y_equation.rhs,S_y_equation.lhs)
```




$\displaystyle c_{0} = \frac{\sum_{i=1}^{N} - c_{1} {x}_{i}}{N} + \frac{\sum_{i=1}^{N} {y}_{i}}{N}$



## Square error should be minimized


```python
square_error_equation = sp.Eq(epsilon[i]**2,
                              sp.Sum((sp.solve(epsilon_equation,epsilon[i])[0])**2, (i, 1, N)))
square_error_equation
```




$\displaystyle {\epsilon}_{i}^{2} = \sum_{i=1}^{N} \left(- c_{0} - c_{1} {x}_{i} + {y}_{i}\right)^{2}$




```python
square_error_zero_derivative_equation = sp.Eq(square_error_equation.rhs.diff(c_1),0)
square_error_zero_derivative_equation
```




$\displaystyle \sum_{i=1}^{N} - 2 \left(- c_{0} - c_{1} {x}_{i} + {y}_{i}\right) {x}_{i} = 0$




```python
square_error_zero_derivative_equation = square_error_zero_derivative_equation.expand().doit()
square_error_zero_derivative_equation
```




$\displaystyle \sum_{i=1}^{N} 2 c_{0} {x}_{i} + \sum_{i=1}^{N} 2 c_{1} {x}_{i}^{2} + \sum_{i=1}^{N} - 2 {x}_{i} {y}_{i} = 0$




```python
square_error_zero_derivative_equation = square_error_zero_derivative_equation.subs(c_0, 
                                                 sp.solve(criteria_1_equation, c_0)[0]).doit().expand()
square_error_zero_derivative_equation
```




$\displaystyle \sum_{i=1}^{N} 2 c_{1} {x}_{i}^{2} + \sum_{i=1}^{N} - 2 {x}_{i} {y}_{i} + \sum_{i=1}^{N} \frac{2 {x}_{i} \sum_{i=1}^{N} - c_{1} {x}_{i}}{N} + \sum_{i=1}^{N} \frac{2 {x}_{i} \sum_{i=1}^{N} {y}_{i}}{N} = 0$




```python
sp.solve(square_error_zero_derivative_equation,c_1)
```




    []




```python
solution = sp.solve(eqs,c_0,c_1)[0]
```


```python
c0_equation = sp.Eq(c_0,solution[0].simplify())
c0_equation
```




$\displaystyle c_{0} = \frac{\sum_{i=1}^{N} \left(- c_{1} {x}_{i} + {y}_{i}\right)}{N}$




```python
c1_equation = sp.Eq(c_1,solution[1].simplify())
c1_equation
```




$\displaystyle \text{True}$




```python
solution[1]
```




$\displaystyle - \frac{\sum_{i=1}^{N} \frac{\left(\sum_{i=1}^{N} - 2 c_{1} {y}_{i} + \sum_{i=1}^{N} 2 c_{1}^{2} {x}_{i}\right) {y}_{i}}{\sum_{i=1}^{N} \left(- c_{1} {x}_{i} + {y}_{i}\right)} + \sum_{i=1}^{N} \frac{\left(\left(\sum_{i=1}^{N} - 2 c_{1} {y}_{i} + \sum_{i=1}^{N} 2 c_{1}^{2} {x}_{i}\right)^{2}\right) {x}_{i}}{2 \left(\sum_{i=1}^{N} \left(- c_{1} {x}_{i} + {y}_{i}\right)\right)^{2}}}{2 \sum_{i=1}^{N} \left(\frac{\left(\sum_{i=1}^{N} - 2 c_{1} {y}_{i} + \sum_{i=1}^{N} 2 c_{1}^{2} {x}_{i}\right) {x}_{i}}{2 \sum_{i=1}^{N} \left(- c_{1} {x}_{i} + {y}_{i}\right)} + {y}_{i}\right)}$




```python
s = sp.Sum(c_0+c_1*x[i], (i, 1, N))

```


```python
sp.solve(epsilon_equation,epsilon[i])[0]
```




$\displaystyle - c_{0} - c_{1} {x}_{i} + {y}_{i}$




```python

```


```python
sp.Vector()
```


```python
linear_equation
```




$\displaystyle y = c_{0} + c_{1} x + \epsilon$




```python
from sympy import IndexedBase
from sympy.tensor.array import Array

a,b,c,d,e,f = sp.symbols("a b c d e f")

X = Array([[a, b, c], [d, e, f]])
X
```




$\displaystyle \left[\begin{matrix}a & b & c\\d & e & f\end{matrix}\right]$




```python
Array([[1]])
```




$\displaystyle \left[\left[\begin{matrix}1\end{matrix}\right]\right]$




```python
X = IndexedBase("X")

W = IndexedBase("W")

i, j, M, K = sp.var("i,j,M,K", integer=True)
s = sp.Sum(X[i, j]*W[j], (i, 1, M), (j, 1, K))
```


```python
s
```




$\displaystyle \sum_{\substack{1 \leq i \leq M\\1 \leq j \leq K}} {W}_{j} {X}_{i,j}$




```python

```


```python
s
```




$\displaystyle \sum_{i=1}^{N} \left(c_{0} + c_{1} {x}_{i}\right)$




```python

```

8c1791c94d74ce782e6820e0f1a50802f6f7e73c
ipynb
Jupyter Notebook
assignments/assignment1/least_square_fit.ipynb
martinlarsalbert/Reliability-analysis-of-marine-structures
9195519f7645761a20beb9c401e40bdf27740394
MIT
assignments/assignment1/least_square_fit.ipynb
martinlarsalbert/Reliability-analysis-of-marine-structures
9195519f7645761a20beb9c401e40bdf27740394
MIT
assignments/assignment1/least_square_fit.ipynb
martinlarsalbert/Reliability-analysis-of-marine-structures
9195519f7645761a20beb9c401e40bdf27740394
MIT
Qwen/Qwen-72B
1. YES
2. YES