Skip to content
DeepForgeby svx
All problems
MediumTime Seriests-213

Dynamic Harmonic Regression

Problem

Fit a harmonic regression y = a + b * cos(2*pi*f*t) + c * sin(2*pi*f*t) by ordinary least squares and return the amplitude sqrt(b^2 + c^2).

Return 0.0 when the series has fewer than three points or the normal equations are singular.

Hint

The amplitude is the Euclidean norm of the cosine and sine coefficients.

Test cases

case 1
in: [[1,0,-1,0],0.25]
expected: 1
case 2
in: [[0,1,0,-1],0.25]
expected: 1
case 3
in: [[1,2,3,4,5],0.1]
expected: 1.9919186279062238
case 4
in: [[5,5,5,5],0.25]
expected: 1.1102230246251565e-16
Starter codePython
import math
def dynamic_harmonic_regression_coefficient(series, freq):
    # Your code here
    pass

4 test cases · run your code against them in the editor