User:MizardX/formulas

= Approximation of 2D-function with weight coefficients =

Target: Stackoverflow: Approximation of 2D-function with weight coefficients


 * $$\mathbf{\left(X^TWX\right)\hat \boldsymbol \beta=X^TWy}$$

Minimize vertical distance to $$y = m + k x$$

 * $$\begin{array}{lcr}

a = \sum_{i<j}{(x_i - x_j) \cdot (x_i \cdot y_j - x_j \cdot y_i) \cdot w_i \cdot w_j} \\ b = \sum_{i<j}{(x_i - x_j) \cdot (y_i - y_j) \cdot w_i \cdot w_j} \\ c = \sum_{i<j}{(x_i - x_j)^2 \cdot w_i \cdot w_j} \\ m = {a \over c} \\ k = {b \over c} \end{array}$$

Concepts

 * $$\begin{array}{lcr}

\text{distance}_{a x+b y=r}\left(x,y\right) = \left| a \cdot x + b \cdot y - r \right| / \sqrt{ a^2 + b^2 } \\ f\left( a, b, r \right) = \sum_i \left( \text{distance}_{a x+b y=r}\left(x_i,y_i\right) ^ 2 \cdot w_i \right) / \sum_i w_i \\ f\left( a, b, r \right) = \sum_i \left( \left( a \cdot x_i + b \cdot y_i - r \right)^2 \cdot w_i \right) / \left( a^2 + b^2 \right) / \sum_i w_i \\ \end{array}$$

Minimize against $$a x + b y = r$$

 * $$\begin{array}{lcr}

r_\min = { \sum_i \left( \left( a \cdot x_i + b \cdot y_i \right) \cdot w_i \right) } / { \sum_i w_i } \\ f\left( a, b, r_\min \right) = \left( A \cdot a^2 + B \cdot a \cdot b + C \cdot b^2 \right) / \sum_i w_i \\ A = \sum_{i<j}{ \left( x_i - x_j \right) ^2 \cdot w_i \cdot w_j} \\ B = 2 \cdot \sum_{i<j}{ \left( x_i - x_j \right) \cdot \left( y_i - y_j \right) \cdot w_i \cdot w_j} \\ C = \sum_{i<j}{ \left( y_i - y_j \right) ^2 \cdot w_i \cdot w_j} \\ f\left(\cos t,\sin t,r_\min \right) = A \cdot \cos^2 t + B \cdot \sin t \cdot \cos t + C \cdot \sin^2 t = P \cdot \cos {2 t} + Q \cdot \sin {2 t} +  R \\ P = \left( A - C \right) / 2 \\ Q = B / 2 \\ R = \left( A + C \right) / 2 \\ \textbf\text{minimum of } P \cdot \cos{2 t} + Q \cdot \sin{2 t} + R \textbf\text{ is when} \\ \cos{2 t} = -P / \sqrt{ P^2 + Q^2 } = X \textbf\text{ and } \sin{2 t} = -Q / \sqrt{ P^2 + Q^2 } = Y \\ \textbf\text{this gives } \cos{t} = a = \sqrt{ \left( X + 1 \right) / 2 } \textbf\text{ and } \sin{t} = b = Y / \left( 2 \cdot a \right) \end{array}$$

With accumulators

 * $$\begin{array}{lcr}

\textbf\text{New point: } \left(x,y,w\right) \\ A \leftarrow A + \left( x^2 \cdot \sigma_{w} - 2 \cdot x \cdot \sigma_{w x} + \sigma_{w x^2} \right) \cdot w \\ B \leftarrow B + 2 \cdot \left( x \cdot y \cdot \sigma_{w} - x \cdot \sigma_{w y} - y \cdot \sigma_{w x} + \sigma_{w x y} \right) \cdot w \\ C \leftarrow C + \left( y^2 \cdot \sigma_{w} - 2 \cdot y \cdot \sigma_{w y} + \sigma_{w y^2} \right) \cdot w \\ P = \left( A - C \right) / 2 \\ Q = B / 2 \\ X = -P / \sqrt{ P^2 + Q^2 } \\ Y = -Q / \sqrt{ P^2 + Q^2 } \\ a = \sqrt{ \left( X + 1 \right) / 2 } \\ b = Y / \left( 2 \cdot a \right) \\ \sigma_w \leftarrow \sigma_w + w \\ \sigma_{w x} \leftarrow \sigma_{w x} + w \cdot x \\ \sigma_{w x^2} \leftarrow \sigma_{w x^2} + w \cdot x^2 \\ \sigma_{w y} \leftarrow \sigma_{w y} + w \cdot y \\ \sigma_{w y^2} \leftarrow \sigma_{w x^2} + w \cdot y^2 \\ \sigma_{w x y} \leftarrow \sigma_{w x y} + w \cdot x \cdot y \\ r = \left( \sigma_{w x} \cdot a + \sigma_{w y} \cdot b \right) / \sigma_w \end{array}$$

= Linear regression &mdash; Expected zero =

Fit data to line $$y = k \cdot x + m$$, and solve $$y = 0$$.


 * $$\displaystyle{\sum_{i=1}^n{\left( date_{i}^2 \right)} \cdot \sum_{i=1}^n{\left(capacity_{i}\right)} - \sum_{i=1}^n{\left(date_{i}\right)} \cdot \sum_{i=1}^n{\left(date_{i} \cdot capacity_{i}\right)}} \over \displaystyle{\sum_{i=1}^n{\left(date_{i}\right)} \cdot \sum_{i=1}^n{\left(capacity_{i}\right)} - \sum_{i=1}^n{\left(1\right)} \cdot \sum_{i=1}^n{\left(date_{i} \cdot capacity_{i} \right)}}$$