Least squares regression of sine wave Let's say that a sine-like function of a fixed frequency and zero-mean can only vary in amplitude and offset.
That is, for variables $a \in R^+$ and $z \in [0..2\pi]$
$$ f(t) = a(\sin(kt + z))$$
(where $k$ is a constant proportional to the fixed frequency)
For some set of $n$ samples $(t_1,y_1),(t_2,y_2)...(t_n, y_n)$ we want to fit $a$ and $z$ such that they minimize:
$$ L(a,z) = \sum(f(t_i)-y_i)^2 $$
If f was a linear function we could just use ordinary least squares regression.
Is there some similar technique for the above function?  What approach would you use to minimize $L$ ?
 A: In general because of the presence of periodic functions, this is not the simplest problem in the domain of curve fitting. However, this case is simple because $k$ is not a tunable parameter but a fixed constant.
You have $n$ data points $(t_i,y_i)$ and you want to perform a least square fit based on the model
$$y=a \sin(kt+z)$$
Rewrite is as
$$y=a \cos (z) \sin (k t)+a \sin (z) \cos (k t)$$ and define
$$A=a \cos (z)\qquad B=a \sin (z)\qquad S_i=\sin (k t_i)\qquad C_i=\cos (k t_i)$$ which makes the model to be
$$y=A \,S+B \,C$$ which is just a bilinear regression without intercept.
When solved for $(A,B)$, you have
$$a^2=A^2+B^2\qquad\qquad\text{and}\qquad\qquad z=\tan ^{-1}\left(\frac{B}{A}\right)$$
I tried an example : using $n=21$ and $k=2$, I generated the following table
$$\left(
\begin{array}{cc}
t & y \\
 0.0 & 42 \\
 0.5 & 121 \\
 1.0 & 89 \\
 1.5 & -25 \\
 2.0 & -115 \\
 2.5 & -99 \\
 3.0 & 8 \\
 3.5 & 108 \\
 4.0 & 109 \\
 4.5 & 10 \\
 5.0 & -98 \\
 5.5 & -115 \\
 6.0 & -27 \\
 6.5 & 87 \\
 7.0 & 121 \\
 7.5 & 44 \\
 8.0 & -73 \\
 8.5 & -123 \\
 9.0 & -59 \\
 9.5 & 59 \\
10.0 & 124
\end{array}
\right)$$
This gives
$$A=116.1789094\qquad\qquad\text{and}\qquad\qquad B=41.88715050$$ from which
$$a=123.4992808\qquad\qquad\text{and}\qquad\qquad z=0.3460335933$$
In fact the data  were generated using
$$y_i=\lceil 123.456 \sin (2 t+0.345678)\rceil$$
