4
$\begingroup$

I have some temperature data gathered over the course of a few days which follow a cyclic pattern. I've fit a linear regression model to it with sine and cosine waves of multiple periods and the result is very close to what it should be.

My question is the following: the model produces a cyclic wave which matches the phase of my data (with peaks and troughs in the same place). How is this possible since I only vary the period length and amplitudes? I don't explicitly model the phase.

My code (Python): note that I adjust the time in hours so that a full day corresponds to 2$\pi$ hours. Is the phase modeled implicitly with the intercept of the linear model?

order = 10
var = temp
T = np.asarray([x.hour*np.pi*(1/12.0) for x in time])
sT = [np.sin((a+1)*T) for a in range(order)]    
cT = [np.cos((a+1)*T) for a in range(order)]
X = np.asarray(sT)
X = np.vstack([np.vstack([X,cT])]).transpose()

from sklearn import linear_model
clf = linear_model.LinearRegression()
clf.fit(X,var)
$\endgroup$
2
  • $\begingroup$ check my question here. stats.stackexchange.com/questions/224990/… $\endgroup$
    – Haitao Du
    Commented Jan 23, 2017 at 20:46
  • $\begingroup$ I think the answer below addresses the question better than the thread above. Basically you ARE modeling the phase and amplitude; check out the "sum and difference formulas" from trigonometry, particularly the one for $cos$ $\endgroup$
    – Taylor
    Commented Jan 24, 2017 at 5:48

1 Answer 1

5
$\begingroup$

The reason you can account for arbitrary phase is that you include both sine and cosine components as regressors.

One way to write a sinusoid with amplitude $a$, frequency $f$ and phase $\phi$ is:

$$a \sin(f t + \phi)$$

But, you can also write it as a linear combination:

$$w_1 \cos(f t) + w_2 \sin(f t)$$

For any choice of $a$ and $\phi$ (in the first representation), there are corresponding weights $w_1$ and $w_2$ (in the second representation) such that the signals are identical. Their relationships are:

$$a = \sqrt{w_1^2 + w_2^2}$$ $$\phi = \tan^{-1}\frac{w_1}{w_2}$$

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.