Coverage for src/ensae_teaching_cs/data/data_ts.py: 100%
27 statements
« prev ^ index » next coverage.py v7.1.0, created at 2023-04-28 06:23 +0200
« prev ^ index » next coverage.py v7.1.0, created at 2023-04-28 06:23 +0200
1"""
2@file
3@brief Data about timeseries.
4"""
5from datetime import datetime, timedelta
6import numpy
9def generate_sells(duration=730, end=None,
10 week_coef=None, month_coef=None,
11 trend=1.1):
12 """
13 Generates dummy data and trends and seasonality.
14 """
15 if week_coef is None:
16 week_coef = numpy.array([0.1, 0.12, 0.12, 0.15, 0.20, 0., 0.])
17 week_coef[5] = 1. - week_coef.sum()
18 if month_coef is None:
19 month_coef = [0.8, 1, 1, 1, 1, 1,
20 0.8, 0.6, 1, 1, 1, 1.5]
21 month_coef = numpy.array(month_coef)
22 month_coef /= month_coef.sum()
24 if end is None:
25 end = datetime.now()
26 begin = end - timedelta(duration)
27 day = timedelta(1)
29 rows = []
30 rnd = (numpy.random.randn(duration + 1) * 0.1) + 1
31 exp = (1 + numpy.exp(- numpy.arange(duration + 1) / duration * trend)) ** (-1)
32 pos = 0
33 while begin <= end:
34 month = begin.month
35 weekd = begin.weekday()
36 value = rnd[pos] * week_coef[weekd] * month_coef[month - 1] * exp[pos]
37 pos += 1
38 obs = dict(date=begin, value=value)
39 rows.append(obs)
40 begin += day
41 return rows