Skip to content

In implementation of Seasonal Decompose (STL) with FourierSeries, max_cycle=np.max(seasonal_cycle) is wrong? #33

Description

@Norlandz

For the Textbook's implementation of Seasonal Decompose (STL) with FourierSeries & RidgeCV.

(if I understood correctly)
In .\src\decomposition\seasonal.py

class FourierDecomposition(BaseDecomposition):
...
    def _calculate_fourier_terms(self, seasonal_cycle: np.ndarray, max_cycle: int):
        """Calculates Fourier Terms given the seasonal cycle and max_cycle"""
        sin_X = np.empty((len(seasonal_cycle), self.n_fourier_terms), dtype="float64")
        cos_X = np.empty((len(seasonal_cycle), self.n_fourier_terms), dtype="float64")
        for i in range(1, self.n_fourier_terms + 1):
            sin_X[:, i - 1] = np.sin((2 * np.pi * seasonal_cycle * i) / max_cycle)
            cos_X[:, i - 1] = np.cos((2 * np.pi * seasonal_cycle * i) / max_cycle)
        return np.hstack([sin_X, cos_X])
...

    def _prepare_X(self, detrended, **seasonality_kwargs):
...
            seasonal_cycle = (getattr(date_index, self.seasonality_period)).values
        return self._calculate_fourier_terms(
            seasonal_cycle, max_cycle=np.max(seasonal_cycle)
        )

max_cycle=np.max(seasonal_cycle) is wrong

ie,eg:
seasonal_cycle = df.index.dayofweek.values => we know: dayofweek gives 0,1,2,3,4,5,6
max_cycle=np.max(seasonal_cycle) => it takes 6 -- this is wrong
it should take 7 as the max_cycle (-- the period in FourierSeries)

eg:
For a comparison of a simple ex
image
using 7 clearly fits better than using 6.

eg:
For a comparison of a the textbook energy_consumption data:
image
(this is not so obvious)
(but you can see, if use 6, there are always 2 days sticks to the same level,, in each period)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions