Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Sunspots Dataset

To this sunspots dataset, we shall fit the sinusoidal model:

yt=β0+β1cos⁡(2πft)+β2sin⁡(2πft)+ϵty_t = \beta_0 + \beta_1 \cos(2 \pi f t) + \beta_2 \sin(2 \pi f t) + \epsilon_t

with ϵt\epsilon_t being i.i.d N(0,σ2)N(0, \sigma^2). We focus on estimation and uncertainty quantification for the important parameter in this model is ff. We do this in two ways: (a) using Fourier frequencies, and (b) using a dense grid of frequencies, and we compare the results.

<Figure size 640x480 with 1 Axes>

When restricting to Fourier frequencies, computation of RSS(f)RSS(f) proceeds via the DFT and periodogram. Here is the code for computing the periodogram.

Below we plot the periodogram.

<Figure size 640x480 with 1 Axes>

Below is the code for computing the RSS using the periodogram.

The following is the plot for the RSS (restricted to Fourier frequencies in the range (0,0.5)(0, 0.5)):

<Figure size 640x480 with 1 Axes>

Below is the estimate of ff (along with the corresponding period) based on this RSS. It is simply the minimizer of RSS (or equivalently, maximizer of the periodogram):

0.09202453987730061
10.866666666666667

The estimate of the period is somewhat close to 11 but not very close. Let us now do uncertainty quantification (again with the restriction to Fourier frequencies) to see how wide the uncertainty interval for ff is. The following function calculates the posterior for ff on the log-scale. Recall that the posterior is given by:

posterior(j/n)∝(1RSS(j/n))(n−3)/2I{0<j/n<0.5}.\begin{align*} \text{posterior}(j/n) \propto \left(\frac{1}{RSS(j/n)} \right)^{(n-3)/2} I\{0 < j/n < 0.5\}. \end{align*}

Note that there is no term ∣XfTXf∣−1/2|X_f^T X_f|^{-1/2} as this term is a constant (not depending on ff) when ff is restricted to Fourier frequencies. In the function below, we compute this posterior on the log-scale, and we use the connection between RSS and periodogram to compute the RSS.

Below we plot the log posterior as a function of the fourier frequencies.

<Figure size 640x480 with 1 Axes>

Below we exponentiate the log-posterior to compute the posterior (and subsequently normalize the posterior). We then compute the posterior mode, and the smallest symmetric region around the posterior mode for which the posterior probability exceeds 0.95. This will give our uncertainty interval for ff. Note again that in this analysis, we are restricting ff to the set of Fourier frequencies.

[0.09202454 0.09202454 0.09202454]
[10.86666667 10.86666667 10.86666667]

See that these uncertainty intervals (credible intervals) are just comprised of the single point given by the least squares estimators. This is because the grid is too coarse. At the least squares estimate f^\hat{f}, the posterior probability is much higher than every other frequency in the grid including the nearby frequencies f^±1/n\hat{f} \pm 1/n. This is because the RSS at f^\hat{f} is much lower compared to the RSS at every other frequency, as shown below.

42062.71407990053
154676.63515544275

To get a better estimate of the frequency, as well as a meaningful uncertainty interval, we need to work with a much finer (i.e., more dense) grid.

0.09088999999999671
11.00231048520229
[0.09089 0.09061 0.09117]
[10.96852035 11.00231049 11.03630946]

Now the frequency estimate basically corresponds to the time period of 11 years. Also, now the interval is not concentrated at a single point. For a clearer interpretation of the uncertainty interval, let us convert the uncertainty in the units of days.

array([12.33340059, 12.40962511])

So the uncertainty period can be summarized as:

[11 years −12.33 days,11 years +12.41 days].[11 \text{ years } - 12.33 \text{ days}, 11 \text{ years } + 12.41 \text{ days}].

This interval is quite narrow which can be seen as a feature of the single sinusoid model. Also note that this interval for the period (1/f1/f) is not symmetric because we took inverses (the interval for ff is symmetric).

Orthogonality of Sinusoids at Fourier Frequencies

<Figure size 1000x600 with 1 Axes>
-6.72705371516124e-15
<Figure size 1000x600 with 1 Axes>

Fitting more sinusoids to the sunspots data

We now consider the model with two sinusoids:

yt=β0+β1cos⁡(2πf1t)+β2sin⁡(2πf1t)+β3cos⁡(2πf2t)+β4sin⁡(2πf2t)+ϵty_t = \beta_0 + \beta_1 \cos(2 \pi f_1 t) + \beta_2 \sin(2 \pi f_1 t) + \beta_3 \cos(2 \pi f_2 t) + \beta_4 \sin(2 \pi f_2 t) + \epsilon_t

with both f1f_1 and f2f_2 denoting unknown parameters (along with β0,β1,β2,β3,β4\beta_0, \beta_1, \beta_2, \beta_3, \beta_4 and σ\sigma).

Before fitting this model to the data, let us first compute the fitted values for the best single sinusoidal model (that we fit above).

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      y   R-squared:                       0.305
Model:                            OLS   Adj. R-squared:                  0.301
Method:                 Least Squares   F-statistic:                     70.86
Date:                Thu, 24 Sep 2026   Prob (F-statistic):           3.06e-26
Time:                        19:40:18   Log-Likelihood:                -1747.8
No. Observations:                 326   AIC:                             3502.
Df Residuals:                     323   BIC:                             3513.
Df Model:                           2                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const         78.8656      2.868     27.496      0.000      73.223      84.508
x1           -38.9199      4.055     -9.599      0.000     -46.897     -30.943
x2           -28.4067      4.058     -7.001      0.000     -36.390     -20.424
==============================================================================
Omnibus:                       56.273   Durbin-Watson:                   0.385
Prob(Omnibus):                  0.000   Jarque-Bera (JB):               92.777
Skew:                           1.002   Prob(JB):                     7.14e-21
Kurtosis:                       4.678   Cond. No.                         1.42
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
866099.3693927363
<Figure size 1000x600 with 1 Axes>

Below we write a function for calculating RSS with multiple frequencies.

In the code below, we search over two frequencies f1f_1 and f2f_2 which best fit to the data. For the range of frequencies, we restrict to the range (0,0.15)(0, 0.15). This is because it seems unlikely that sinusoids with frequencies larger than 0.15 will fit well to the data.

x           0.090691
y           0.099850
rss    702659.814908
Name: 421955, dtype: float64
[0.09069069 0.09984985]
[11.02649007 10.01503759]

Below we plot the fitted function with the two best frequencies obtained as above.

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      y   R-squared:                       0.436
Model:                            OLS   Adj. R-squared:                  0.429
Method:                 Least Squares   F-statistic:                     62.06
Date:                Thu, 24 Sep 2026   Prob (F-statistic):           8.28e-39
Time:                        19:43:40   Log-Likelihood:                -1713.7
No. Observations:                 326   AIC:                             3437.
Df Residuals:                     321   BIC:                             3456.
Df Model:                           4                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const         79.0717      2.592     30.508      0.000      73.973      84.171
x1           -43.9682      3.662    -12.005      0.000     -51.174     -36.763
x2           -18.7345      3.667     -5.109      0.000     -25.950     -11.520
x3            30.8208      3.663      8.414      0.000      23.614      38.027
x4            -9.3467      3.667     -2.549      0.011     -16.561      -2.133
==============================================================================
Omnibus:                       33.038   Durbin-Watson:                   0.385
Prob(Omnibus):                  0.000   Jarque-Bera (JB):               45.956
Skew:                           0.697   Prob(JB):                     1.05e-10
Kurtosis:                       4.200   Cond. No.                         1.42
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
702659.8149081466
<Figure size 1000x600 with 1 Axes>

Next we try to find the three best frequencies, again using a grid minimization strategy.

x           0.091206
y           0.093467
z           0.100251
rss    580371.712625
Name: 1268323, dtype: float64
[0.09120603 0.09346734 0.10025126]
[10.96418733 10.69892473  9.97493734]

Next we plot the fitted function using the three best frequencies.

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      y   R-squared:                       0.534
Model:                            OLS   Adj. R-squared:                  0.525
Method:                 Least Squares   F-statistic:                     60.99
Date:                Thu, 24 Sep 2026   Prob (F-statistic):           4.40e-50
Time:                        19:47:16   Log-Likelihood:                -1682.6
No. Observations:                 326   AIC:                             3379.
Df Residuals:                     319   BIC:                             3406.
Df Model:                           6                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const         78.7271      2.363     33.315      0.000      74.078      83.376
x1           -35.2869      3.530     -9.995      0.000     -42.233     -28.341
x2           -40.1092      3.517    -11.404      0.000     -47.029     -33.190
x3           -23.1964      3.535     -6.562      0.000     -30.151     -16.242
x4            23.6205      3.539      6.674      0.000      16.657      30.584
x5            31.9358      3.356      9.516      0.000      25.333      38.539
x6             1.8031      3.353      0.538      0.591      -4.794       8.400
==============================================================================
Omnibus:                       30.303   Durbin-Watson:                   0.395
Prob(Omnibus):                  0.000   Jarque-Bera (JB):               40.499
Skew:                           0.668   Prob(JB):                     1.61e-09
Kurtosis:                       4.094   Cond. No.                         1.73
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
580371.7126249631
<Figure size 1000x600 with 1 Axes>

Below we look at the smallest RSS achieved with one frequency, two frequencies and three frequencies respectively.

[866099.36939274 702659.81490815 580371.71262496]

If, instead of trying to find the best frequencies, we only look at the Fourier frequencies, the RSS will be much higher.

The best Fourier frequencies can be found as follows (these are the maximizers of the periodogram):

[ 30.  31.   3.  32.  29.  33.  27.   6.  40.  28.   2.  25.   5.   4.
  39.  37.  15.  57.  35.  38.   1.  26.   8.  12.  22.  34.  23.  44.
  60.  59.  10.   9.  62.  55.   7.  19.  11.  68.  45.  14.  20.  21.
  43.  17.  65.  49.  24.  41.  64.  54.  47.  67.  71.  56. 102.  77.
  46.  82.  89.  90.  52.  76.  18.  80. 116.  58.  95.  16. 141. 100.
  73.  42.  63.  70.  66. 101.  78.  75.  87. 105.  84.  99. 115. 154.
  91.  61.  69. 117. 112. 160. 146.  93.  13. 153. 120.  53.  79. 156.
 132.  74. 138. 142. 137. 139.  48. 124.  51. 150. 135. 152. 104. 131.
 147. 148.  83. 159. 158. 118.  50. 130. 161. 128.  36. 129. 125. 111.
  85. 106.  94. 140. 119. 123. 143.  98.  92. 162. 110. 107. 113. 122.
 151. 114. 144.  97.  72.  86. 126. 134. 127. 155. 157. 133.  81. 145.
 149. 103.  88. 108. 136.  96. 121. 109.]
727514.7507267644
563413.645247653
580371.7126249631
[0.09202453987730061, 0.0950920245398773, 0.009202453987730062, 0.09815950920245399, 0.08895705521472393]
[0.09120603 0.09346734 0.10025126]

Below we plot the best fitted function with the top 5 Fourier frequencies.

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      y   R-squared:                       0.548
Model:                            OLS   Adj. R-squared:                  0.534
Method:                 Least Squares   F-statistic:                     38.17
Date:                Thu, 24 Sep 2026   Prob (F-statistic):           1.28e-48
Time:                        19:49:48   Log-Likelihood:                -1677.7
No. Observations:                 326   AIC:                             3377.
Df Residuals:                     315   BIC:                             3419.
Df Model:                          10                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const         78.8963      2.342     33.683      0.000      74.288      83.505
x1            18.1908      3.313      5.491      0.000      11.673      24.708
x2           -33.2872      3.313    -10.049      0.000     -39.805     -26.770
x3           -14.7861      3.313     -4.464      0.000     -21.304      -8.269
x4           -31.0203      3.313     -9.364      0.000     -37.538     -24.503
x5           -23.6981      3.313     -7.154      0.000     -30.216     -17.181
x6             0.1053      3.313      0.032      0.975      -6.412       6.623
x7            -9.1331      3.313     -2.757      0.006     -15.651      -2.616
x8           -20.8167      3.313     -6.284      0.000     -27.334     -14.299
x9            -2.6816      3.313     -0.810      0.419      -9.199       3.836
x10           21.9730      3.313      6.633      0.000      15.455      28.491
==============================================================================
Omnibus:                       62.289   Durbin-Watson:                   0.462
Prob(Omnibus):                  0.000   Jarque-Bera (JB):              119.231
Skew:                           1.021   Prob(JB):                     1.29e-26
Kurtosis:                       5.146   Cond. No.                         1.41
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
580371.7126249631
<Figure size 1000x600 with 1 Axes>

Below we compare the prediction accuracy for the best models based on Fourier frequencies (compared to the frequencies chosen over grids). We use the first n−50n - 50 observations for training the model, and check the prediction accuracy on the test dataset (last 50 observations).

Five Fourier frequencies:       1648.24
Three frequencies (f_opt_3):  1236.05
Two frequencies (f_opt_2):    2751.11
Three Fourier frequencies:    2628.22
<Figure size 1000x600 with 1 Axes>

Audio Dataset

Consider the audio dataset that we have used in the past couple of lectures. We add a small amount of noise to the actual piano note.

301272
22050
13.66312925170068
Loading...

The following function computes the periodogram of this dataset.

<Figure size 640x480 with 1 Axes>

Below we identify the top 10 frequencies having high periodogram values. Then fit the linear model with sines and cosines at those frequencies.

Top 10 frequencies: [0.01179997 0.01177341 0.01180329 0.01172694 0.01179665 0.01180661
 0.01177009 0.01177673 0.01180993 0.01172363]

Below we plot the fitted values corresponding to the linear model.

Loading...
Loading...
Loading...

The fitted sound seems noiseless but also quite different from the original sound. Below we repeat the exercise with a much larger value of kk. The code below again computes the fitted values for the linear model corresponding to the top kk sinusoids, but the code does not directly fit the linear model but instead uses inbuilt functions from the fft library for efficient computation.

Now the fitted sound is affected by noise. This is the classic bias-variance tradeoff. When kk is chosen small, the fitted sound is noiseless but is biased (i.e., far in nature from the original sound) but when kk is chosen large, the fitted sound resembles the original sound but is affected by noise.

Loading...
Loading...
Loading...