1

I have written a few functions in python to generate basic time-series data, and add some attributes to it like a trend, seasonality, and white noise and then plot that series using matplotlib. I want to make a python class to call all these functions from one place and add the attributes (trend, attributes, and noise) whenever I want. I am having some trouble making this class as I am not an expert with the self keyword.

Below are the functions.

#Basic Series------------------------------------------------------------------------------------------
def plot_series(t, series, format="-", start=0, end=None, label=None):
    plt.plot(t[start:end], series[start:end], format, label=label,lw=2)
    plt.xlabel("Time")
    plt.ylabel("Value")
    if label:
        plt.legend(fontsize=14)
    plt.grid(True)

#Add a trend--------------------------------------------------------------------------------------------
def trend(t, slope=0):
    return slope * t

#Add Seasonality----------------------------------------------------------------------------------------
def seasonal_pattern(season_time):
    return np.where(season_time < 0.4,
                    np.cos(season_time * 2 * np.pi),
                    1 / np.exp(3 * season_time))

#Repeats the same pattern at each period
def seasonality(t, period, amplitude=1, phase=0):
    season_time = ((t + phase) % period) / period
    return amplitude * seasonal_pattern(season_time)

#Add white noise----------------------------------------------------------------------------------------
def white_noise(t, noise_level=1, seed=None):
    rnd = np.random.RandomState(seed)
    return rnd.randn(len(t)) * noise_level


t = np.arange(4 * 365 + 1)
baseline = 10
amplitude = 40
series = trend(t, 0.2)
slope = 0.05
series = baseline + trend(t, slope) + seasonality(t, period=365, amplitude=amplitude)
noise_level = 5
noise = white_noise(t, noise_level, seed=42)
series += noise

#Plot Time Series
plt.figure(figsize=(10, 6))
plot_series(t, series)
plt.title("Time Series Data")
plt.show()

Currently, I have to call all these functions separately as shown above to generate the series. Basically, I just want to make a class around the above code. I hope my question is clear. Some help will be appreciated.

My try so far,

class GenerateTimeSeries(object):

def __init__(self, t):
    self.t = t

#Add a trend--------------------------------------------------------------------------------------------
def trend(self, t, slope=0):
    return self.slope * self.t

#Add Seasonality--------------------------------------Repeats the same pattern at each period-----------
def seasonal_pattern(self, season_time):
    return np.where(self.season_time < 0.4, np.cos(self.season_time * 2 * np.pi), 1 / np.exp(3 * self.season_time))

def seasonality(self, t, period, amplitude=1, phase=0):
    season_time = ((self.t + self.phase) % self.period) / self.period
    return self.amplitude * seasonal_pattern(season_time)

#Add white noise----------------------------------------------------------------------------------------
def white_noise(self, t, noise_level=1, seed=None):
    rnd = np.random.RandomState(self.seed)
    return rnd.randn(len(self.t)) * self.noise_level

#Plot Series-------------------------------------------------------------------------------------------
def plot_series(self, t, series, start=0, end=None, label=None):
    plt.figure(figsize=(10, 6))
    plt.plot(self.t[self.start:self.end], self.series[self.start:self.end], label=self.label,lw=2)
    plt.xlabel("Time")
    plt.ylabel("Value")
    plt.title("Time Series Data")
    if self.label:
        plt.legend(fontsize=14)
    plt.grid(True)
    plt.show()
advik Maniar
  • 9
  • 1
  • 5
  • Show us what you have tried so far (the class)! – Klaus D. Jul 12 '21 at 10:51
  • well, if You lack understanding of OOP, then You should watch some tutorials on that such as this one: [Python OOP Tutorial by Corey Schafer](https://www.youtube.com/watch?v=ZDa-Z5JzLYM&list=PL-osiE80TeTsqhIuOqKhwlXsIBIdSeYtc) because otherwise us writing a class for You won't really help You – Matiiss Jul 12 '21 at 10:53
  • Yes, I have added it to the question rn. – advik Maniar Jul 12 '21 at 10:54
  • also maybe this helps [What is the purpose of the word 'self'?](https://stackoverflow.com/questions/2709821/what-is-the-purpose-of-the-word-self) – Matiiss Jul 12 '21 at 10:54
  • @Matiss thanks for the tutorial, but I am kind of familiar with OOP. I'm just having trouble making this specific class. – advik Maniar Jul 12 '21 at 10:59

0 Answers0