Stellar Space Studies
Stellar Space Studies

Extras

Time series

As a bonus, let's do three simple application examples where the parameter is in two dimensions, by adjusting the three following models on a timeseries:

  • a straight line,
  • a periodic model,
  • a bias and a scale factor.

Introduction

Timeseries

Let's suppose we have a timeseries \(y_i = (t_i, y_i)_{i=1,n} \).

Timeseries

\[ (t_i, y_i)_{i=1,n} \]

And we want to approximate the timeseries \(y\) by a function \(f\), which depends on a parameter \(p\).

Function
We want to adjust the value of p so f(p) approximates y well

\[ y_i \sim f(p)_i \]

Three adjustment cases for \(f\)

The first example is the linear regression. We want to adjust a straight line to fit \(y\). The \(f\) function and the parameter \(p\) are such as:

Linear regression

\[ f(p)_i = p_1 t_i + p_2 \]

Note: \(p_1\) will give us the trend of the signal.

The second case is the periodic regression. We want to adjust a sinus and a cosinus of a given period \(T\) on the timeseries (\(\omega=2\pi/T\). For example, we want to adjust an annual signal to the timeseries. The \(f\) function and the parameter \(p\) are such as:

Periodic regression

\[ f(p)_i = p_1 \cos \omega t_i + p_2 \sin \omega t_i \]

Note: The amplitude will be given by \(A = \sqrt{p_1^2+p_2^2}\) and the phase by \( \varphi = \arctan(p_2/p_1) \).

In the third case we have another timeseries \( (t_i, z_i) \) with the same time sampling, and we want to transform it with a bias and a scale factor to fit \(y\). The \(f\) function and the parameter \(p\) are such as:

Bias and scale adjustment on a series z to fit y

\[ f(p)_i = p_1 z_i + p_2 \]

Note

We can notice that in each of our three cases, the function \(f\) is linear in function of \(p\) and can be written as this:

f is linear in function of p

\[ f(p)_i = \alpha_i p_1 + \beta_i p_2 \]

Adjustment of \(p\)

So what we want to do is to use the least squares method to adjust the value of \(p\). And of course, what we want to minimize is the sum of the squares of the values for \(y_i - f(p)_i\).


Solution of linear least squares in dimension 2

Let's solve the general problem in dimension two, and we will immediately get the solution for our application cases.

Least squares formulation

The parameter \(p\) is in \(\mathbb{R}^2\).

Parameter

\[ p=(p_1,p_2) \]

\((y_i)\) are the measurements, \( (f(p)_i) \) are the theoretical measurements, and the least squares criterion is:

Sum of the squares of the residuals

\[ \mathcal{S}(p) = \sum_{ i=1 }^{ n } \left( y_i - f(p)_i \right)^2 \]

In our cases, \(f\) is already linear in function of \(p\). The linear approximation is equal to the function.

f is linear in function of p
Theoretical measurements

\[ \forall i, \exists \, (\alpha_i , \beta_i): f(p)_i = \alpha_i p_1 + \beta_i p_2 \]

Which translates into:

f is linear in function of p
(no approximation required)

\[ \mathcal{S}(p) = \sum_{ i=1 }^{ n } \left( y_i - \alpha_i p_1 -\beta_i p_2 \right)^2 \]

\( \mathcal{S} \) is a simple polynomial of degree two in function of \(p_1\) and \(p_2\). We can directly find its minimum without making any approximations.

If we want to relate to the general formula, this is equivalent to linearizing \(f\) around \(0\). In other words:

\[ f(p) = \ell(p), \, p_0=0, \, f(p_0) = 0, \, \mathcal{S}(p) = Q(p) \] \[ f(p)_i = \alpha_i p_1 + \beta_i p_2, \, \alpha_i = \frac{\partial f_i}{ \partial p_1} ,\, \beta_i = \frac{\partial f_i}{ \partial p_2} \]

Matrix notation

If we introduce the following matrix notations:

Matrix notation

\[ A = \begin{pmatrix} \alpha_1 & \beta_1 \\ \vdots & \vdots \\ \alpha_i & \beta_i \\ \vdots & \vdots \\ \alpha_n & \beta_n \end{pmatrix}, \, X = \begin{pmatrix} p_1 \\ p_2 \end{pmatrix}, \, B = \begin{pmatrix} y_1 \\ \vdots \\ y_i \\ \vdots \\ y_n \end{pmatrix} \]

Then:

Matrix notation

\[ AX - B = \begin{pmatrix} \alpha_1 p_1+ \beta_1 p_2 - y_1 \\ \vdots \\ \alpha_i p_1 + \beta_i p_2- y_i \\ \vdots \\ \alpha_n p_n + \beta_n p_2 - y_n \end{pmatrix} \]

Or also, with our usual notations:

Matrix formulation

\[ \begin{equation} \begin{split} \mathcal{S}(p) & = \sum_{ i=1 }^{ n } \left( y_i - \alpha_i p_1 -\beta_i p_2 \right)^2 \\ & = ||AX-B||^2 \\ &= \,^t(AX - B)(AX-B) \end{split} \end{equation} \]

Or also

Matrix formulation

\[ \mathcal{S}(p) = \,^tXNX - 2\,^tSX + \,^tBB \]

The normal matrix is:

Normal matrix

\[ N = \,^tAA = \begin{pmatrix} \sum_{i=1}^n{ \alpha_i^2 } & \sum_{i=1}^n { \alpha_i \beta_i } \\ \sum_{i=1}^n { \alpha_i \beta_i } & \sum_{i=1}^n{ \beta_i^2 } \end{pmatrix} \]

The second member is:

Second member

\[ S = \,^t A B = \begin{pmatrix} \sum_{i=1}^n { \alpha_i y_i } \\ \sum_{i=1}^n { \beta_i y_i } \end{pmatrix} \]

The constant (i.e. the sum of the squares of the initial residuals) is:

Constant

\[ ^t BB = \sum_{i=1}^n { y_i^2 } \]

Equivalence between the formulations

Just to be sure, let's develop all the terms:

Equivalence

\[ \begin{equation} \begin{split} \mathcal{S}(p) & = \sum_{ i=1 }^{ n } \left( y_i - \alpha_i p_1 -\beta_i p_2 \right)^2 \\ & = p_1^2 \sum_{i=1}^n\alpha_i^2 + 2p_1p_2\sum_{i=1}^n \alpha_i\beta_i +p_2^2 \sum_{i=1}^n\beta_i^2\\ & - 2p_1\sum_{i=1}^n \alpha_i y_i -2 p_2\sum_{i=1}^n \beta_i y_i \\ & + \sum_{i=1}^n { y_i^2 } \\ & = \,^tXNX \\ & - 2\,^tSX \\ & + \,^tBB \end{split} \end{equation} \]

We can indeed see that both formulations match.

Solution

As we saw before, the minimum of \( \,^tXNX - 2\,^tSX + \,^tBB \) is achieved in \(N^{-1} S\).

Solution

\[ X = N^{-1} S \]

Here is a short reminder of how to invert a 2x2 matrix.

Inverse of a matrix in dimension 2

\[ A = \begin{pmatrix} a & b \\ c & d \end{pmatrix} \]

\[ A^{-1} = \frac{1}{det(A)} \,^t com(A) \]

\[ A^{-1} = \frac{1}{ad-bc} \begin{pmatrix} d & -b \\ -c & a \end{pmatrix} \]

So the inverse of the normal matrix is:

Inverse of the normal matrix

\[ N^{-1} = \frac{1}{ \sum{\alpha_i^2} \sum{ \beta_i^2 } - \big(\sum { \alpha_i \beta_i }\big)^2 } \begin{pmatrix} \sum { \beta_i^2 } & -\sum { \alpha_i \beta_i } \\ -\sum { \alpha_i \beta_i } & \sum{ \alpha_i^2 } \end{pmatrix} \]

We just then need to multiply by the vector \(S\).

Second member

\[ S = \begin{pmatrix} \sum { \alpha_i y_i } \\ \sum { \beta_i y_i } \end{pmatrix} \]

And therefore, the solution is finally:

Solution

\[ p_1 = \frac{ \sum { \beta_i^2 } \sum { \alpha_i y_i } - \sum { \alpha_i \beta_i } \sum { \beta_i y_i } }{\sum{\alpha_i^2} \sum{ \beta_i^2 } - \big(\sum { \alpha_i \beta_i }\big)^2 } \]

\[ p_2 = \frac{ \sum { \alpha_i^2 } \sum { \beta_i y_i } - \sum {\alpha_i \beta_i } \sum { \alpha_i y_i } }{\sum{\alpha_i^2} \sum{ \beta_i^2 } - \big(\sum { \alpha_i \beta_i }\big)^2 } \]

This is the minimum of the function \( (p_1,p_2) \mapsto \sum_{i=1}^n \left( y_i - \alpha_i p_1 -\beta_i p_2 \right)^2 \).

We can also write it using the columns of \(A\).

Solution

\[ p_1 = \frac{ \,^tA_2 A_2 \,^tA_1 B - \,^tA_1 A_2 \,^tA_2 B }{ \,^tA_1 A_1 \,^tA_2 A_2 - \,^tA_1 A_2 \,^tA_1 A_2 } \]

\[ p_2 = \frac{ \,^tA_1 A_1 \,^tA_2 B - \,^tA_1 A_2 \,^tA_1 B }{ \,^tA_1 A_1 \,^tA_2 A_2 - \,^tA_1 A_2 \,^tA_1 A_2 } \]

The residuals are:

Residuals

\[ \mathcal{S}(p_{min}) = \,^tBB - \,^tS N^{-1} S \]

\[ \mathcal{S}(p_{min}) = \sum_{i=1}^n { y_i^2 } - p_1\sum_{i=1}^n { \alpha_i y_i } - p_2\sum_{i=1}^n { \beta_i y_i } \]

\[ \mathcal{S}(p_{min}) = \,^tBB - p_1\,^tA_1 B - p_2\,^tA_2 B \]

Coding

Let's code this in Javascript (for example):

function sum(a, b) {
  if (a.length !== b.length) throw "Array sizes don't match"
  return a.reduce((acc, _, i) => acc + a[i] * b[i], 0)
}

function leastSquares2(y, dydp1, dydp2) {
  const a = dydp1
  const b = dydp2
  const aa = sum(a, a)
  const bb = sum(b, b)
  const ab = sum(a, b)
  const ay = sum(a, y)
  const by = sum(b, y)
  const yy = sum(y, y)
  const delta = aa * bb - ab * ab
  const p1 = (bb * ay - ab * by) / delta
  const p2 = (aa * by - ab * ay) / delta
  const res = yy - p1 * ay - p2 * by
  const rms = Math.sqrt(res / y.length)
  return { p1, p2, res, rms }
}

Dimension 3

As a bonus, let's give you the formula for dimension 3. This is the same method used for inverting the matrix (using the co-matrix).

function leastSquares3(y, dydp1, dydp2, dydp3) {
  const a = dydp1
  const b = dydp2
  const c = dydp3
  const aa = sum(a, a)
  const bb = sum(b, b)
  const cc = sum(c, c)
  const ab = sum(a, b)
  const ac = sum(a, c)
  const bc = sum(b, c)
  const ay = sum(a, y)
  const by = sum(b, y)
  const cy = sum(c, y)
  const yy = sum(y, y)
  const delta =
    aa * bb * cc -
    ab * ac * bc -
    ab * ac * bc -
    aa * bc * bc -
    bb * ac * ac -
    cc * ab * ab
  const p1 =
    ((bb * cc - bc * bc) * ay +
      (ac * bc - ab * cc) * by +
      (ab * bc - ac * bb) * cy) /
    delta
  const p2 =
    ((ac * bc - ab * cc) * ay +
      (aa * cc - ac * ac) * by +
      (ac * ab - aa * bc) * cy) /
    delta
  const p3 =
    ((ab * bc - ac * bb) * ay +
      (ac * ab - aa * bc) * by +
      (aa * bb - ab * ab) * cy) /
    delta
  const res = yy - p1 * ay - p2 * by - p3 * cy
  const rms = Math.sqrt(res / y.length)
  return { p1, p2, p3, res, rms }
}

Solution

Now that we have solved the general problem, we can apply the solutions in our particular cases.

Linear regression

We want to adjust a straight line that fits the timeseries \(y\). The function for the theoretical measurements is \( f(p) = p_1 t+ p_2 \).

Linear regression

\[ f(p)_i = p_1 t_i + p_2 \]

\[ \mathcal{S}(p) = \sum \left( y_i - p_1 t_i - p_2 \right)^2 \]

\[ \frac{\partial f_i}{ \partial p_1} = t_i , \, \frac{\partial f_i}{ \partial p_2} = 1 \]

\[ A = \begin{pmatrix} t_1 & 1 \\ \vdots & \vdots \\ t_i & 1 \\ \vdots & \vdots \\ t_n & 1 \end{pmatrix} \]

\[ p_1 = \frac{ n \sum { t_i y_i } - \sum { t_i } \sum { y_i } }{n\sum{t_i^2} - \big(\sum { t_i }\big)^2 } \]

\[ p_2 = \frac{ \sum { t_i^2 } \sum { y_i } - \sum {t_i } \sum { t_i y_i } }{n\sum{t_i^2} - \big(\sum { t_i }\big)^2 } \]

If the timeseries has only two points \((t_1,y_1)\) and \((t_2,y_2)\), we can quickly check that the formula gives \(p_1=\frac{y_2-y_1}{t_2-t_1}\).

Periodic regression

We want to adjust a periodic signal of period \(T = 2\pi/\omega\) that fits the timeseries \(y\). The function for the theoretical measurements is \( f(p) = p_1 \cos \omega t + p_2 \sin \omega t \).

Periodic regression

\[ f(p)_i = p_1 \cos \omega t_i + p_2 \sin \omega t_i \]

\[ \mathcal{S}(p) = \sum \left( y_i - p_1 \cos \omega t_i - p_2 \sin \omega t_i \right)^2 \]

\[ \frac{\partial f_i}{ \partial p_1} = \cos \omega t_i , \, \frac{\partial f_i}{ \partial p_2} = \sin \omega t_i \]

\[ A = \begin{pmatrix} \cos \omega t_1 & \sin \omega t_1 \\ \vdots & \vdots \\ \cos \omega t_i & \sin \omega t_i \\ \vdots & \vdots \\ \cos \omega t_n & \sin \omega t_n \end{pmatrix} \]

\[ p_1 = \frac{ \sum { \sin ^2 \omega t_i} \sum { y_i \cos \omega t_i } - \sum { \cos \omega t_i \sin \omega t_i } \sum { y_i \sin \omega t_i } }{\sum{\cos ^2\omega t_i} \sum{ \sin^2 \omega t_i } - \big(\sum { \cos \omega t_i \sin \omega t_i }\big)^2 } \]

\[ p_2 = \frac{ \sum { \cos ^2 \omega t_i } \sum { y_i \sin \omega t_i } - \sum {\cos \omega t_i \sin \omega t_i } \sum { y_i \cos \omega t_i } }{\sum{\cos ^2\omega t_i} \sum{ \sin^2 \omega t_i } - \big(\sum { \cos \omega t_i \sin \omega t_i }\big)^2 } \]

We can rewrite the formula using amplitude and phase, i.e. \( f(p) = R\cos (\omega t + \varphi) \). We then have \( R = \sqrt { p_1^2+p_2^2 } \) and \( \varphi = \arctan(p_2/p_1) \). The periodic maximum is obtained for \( t = - \varphi / \omega \).

Bias and scale adjustment

We want to transform a given \(z\) timeseries with a bias and a scale factor to fit the timeseries \(y\). The function for the theoretical measurements is \( f(p) = p_1 z+ p_2 \).

Bias and scale adjustment

\[ f(p)_i = p_1 z_i + p_2 \]

\[ \mathcal{S}(p) = \sum \left( y_i - p_1 z_i - p_2 \right)^2 \]

\[ \frac{\partial f_i}{ \partial p_1} = z_i , \, \frac{\partial f_i}{ \partial p_2} = 1 \]

\[ A = \begin{pmatrix} z_1 & 1 \\ \vdots & \vdots \\ z_i & 1 \\ \vdots & \vdots \\ z_n & 1 \end{pmatrix} \]

\[ p_1 = \frac{ n \sum { z_i y_i } - \sum { z_i } \sum { y_i } }{n\sum{z_i^2} - \big(\sum { z_i }\big)^2 } \]

\[ p_2 = \frac{ \sum { z_i^2 } \sum { y_i } - \sum {z_i } \sum { z_i y_i } }{n\sum{z_i^2} - \big(\sum { z_i }\big)^2 } \]

And that's it! Congratulations and thank you for following until the end!