Suppose that $z_1, z_2, \dots$ is a time series. A very common approximation of the time series is as a sum
of K sinusoids
$z_t \approx \hat{z}_t = \sum_{k=1}^K a_k \cos(\omega_k t - \phi_k), \quad t = 1, 2, \dots$
The $k$th term in this sum is called a sinusoid signal. The coefficient $a_k \ge 0$ is called the amplitude, $\omega_k > 0$ is called the frequency, and $\phi_k$ is called the phase of the $k$th sinusoid. (The phase is usually
chosen to lie in the range from $-\pi$ to $\pi$.) In many applications the frequencies are multiples of $\omega_1$,
i.e., $\omega_k = k\omega_1$ for $k = 2, \dots, K$, in which case the approximation is called a Fourier approximation,
named for the mathematician Jean-Baptiste Joseph Fourier.
Suppose you have observed the values $z_1, \dots, z_T$, and wish to choose the sinusoid amplitudes $a_1, \dots, a_K$ and phases $\phi_1, \dots, \phi_K$ so as to minimize the RMS value of the approximation error $(\hat{z}_t - z_t), t = 1, \dots, T$. (We assume that the frequencies are given.) Explain how to solve this using least
squares model fitting.
Hint. A sinusoid with amplitude $a$, frequency $\omega$, and phase $\phi$ can be described by its cosine and sine
coefficients $\alpha$ and $\beta$, where
$\alpha \cos(\omega t - \phi) = \alpha \cos(\omega t) + \beta \sin(\omega t)$,
where (using the cosine of sum formula) $\alpha = a \cos \phi$, $\beta = a \sin \phi$. We can recover the amplitude and
phase from the cosine and sine coefficients as
$a = \sqrt{\alpha^2 + \beta^2}, \quad \phi = \arctan(\beta/\alpha)$.
Express the problem in terms of the cosine and sine coefficients.