Linear regression custom fit function, calculate A and B using system of linear equations

46 Views Asked by At

Good afternoon! As a part of solved examples from previous year examination, there is a following bi-dimensional table of frequencies:

|    X\Y   |   0 |   1 |  2 |   3 |   4 |
|----------|-----|-----|----|-----|-----|
|  [0, 10] |   0 |   0 |  0 |  77 | 222 |
| (10, 20] |   0 |   0 | 73 | 555 |  51 |
| (20, 25] | 163 | 125 | 65 |   0 |   0 |
| (25, 30] | 130 |  50 |  0 |   0 |   0 |
| (30, ∞)  |  56 |   6 |  0 |   0 |   0 |

One of the tasks is to adjust the fit to the function $y=ax+b*sin(x)$. In the solution, the system of linear equations is formed, like this:

$$\begin{pmatrix}\begin{array}{cc|c}\sum n_ix_i &\sum n_ix_isin(x) & \sum n_iy_ix_i \\ \sum n_ix_isin(x) &\sum n_isin(x)^2 &\sum n_iy_isin(x)\end{array}\end{pmatrix}$$

Solving it yields the constants $a$ and $b$ for the aforementioned function, substituting results in:

$$y^{est}=-1.3831+9.1636*sin(x)$$

Could someone please explain to me, how the system of linear equations was formed for this task, and how is the method called, so I can read more? There are two more exercises of this with similar operations, but I cannot wrap my head around it. If it is unclear, or someone is interested, I can post the other two systems of linear equations too. Thank you!

1

There are 1 best solutions below

2
On BEST ANSWER

The model you want to fit on the basis of $p$ data points $(x_i,y_i)$ is $$y=ax+b\sin(x)$$ To make life easier, define $z_i=\sin(x_i)$ so the model becomes $$y=ax+bz$$ which corresponds to a multilinear regression with no intercept.

What you want to minimize is $$F=\sum_{i=1}^p n_i(a x_i+b z_i-y_i)^2$$ So, the derivatives are $$\frac{dF}{da}=2\sum_{i=1}^p n_ix_i(a x_i+b z_i-y_i)$$ $$\frac{dF}{db}=2\sum_{i=1}^p n_i z_i(a x_i+b z_i-y_i)$$ and you want them to be equal to zero. So, expanding, we have (these are the so-called normal equations) $$a \sum_{i=1}^p n_i x_i^2+b\sum_{i=1}^p n_i x_i z_i=\sum_{i=1}^p n_i x_i y_i$$ $$a \sum_{i=1}^p n_i x_i z_i+b\sum_{i=1}^p n_i z_i^2=\sum_{i=1}^p n_i z_i y_i$$