1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
use crate::forecasting::ExpSmoothing;
use crate::time_series::{TimeSeries, Style};
use crate::regression::LinearRegression;
use crate::plotable::Plotable;
use crate::time_series::Grouper;
use plotlib::view::{View, ContinuousView};
use plotlib::repr::Plot;
use plotlib::page::Page;
const DEFAULT_ALPHA: f64 = 0.4;
pub struct Dumb {
original: TimeSeries,
expsmooth: Option<ExpSmoothing>,
linear_reg: Option<LinearRegression>,
season: usize,
prediction: Vec<(f64, f64)>,
}
impl Dumb {
pub fn new(time_series: &TimeSeries) -> Self {
let mut this = Self {
original: time_series.clone(),
expsmooth: None,
linear_reg: None,
season: 0,
prediction: Vec::new(),
};
this.expsmooth = Some(
ExpSmoothing::new(time_series).with_alpha(DEFAULT_ALPHA)
);
this.linear_reg = Some(LinearRegression::new(time_series));
this
}
pub fn with_season(mut self, season: usize) -> Self {
self.season = season;
self.update_data();
self
}
fn update_data(&mut self) {
if self.season == 0 {
panic!("No season length set for Dumb")
}
let mut sm = self.exp_smooth().as_time_series().get_data();
sm.insert(0, (1.0, -1.0));
let mut sm = TimeSeries::from_pairs_vec(sm);
if let Some(expsmooth) = &self.expsmooth {
self.prediction = Vec::new();
let alpha = expsmooth.alpha();
let len = sm.len();
let r = alpha * sm.get_range_at(len - 1) + (1.0 - alpha) * expsmooth.as_time_series().get_range_at(len - 1);
self.prediction.push((1.0, r));
let mut past = Vec::new();
let mut c = 1;
for seas in 0..(sm.len() / self.season) as usize {
past.push(vec![0.0; self.season]);
for i in 0..self.season {
past[seas][i] = sm.get_range_at(c);
c += 1;
}
}
let mut months = Vec::new();
for month in 0..self.season {
months.push(vec![0.0; past.len()]);
for season in 0..past.len() {
months[month][season] = past[season][month];
}
}
let mut x_points = Vec::new();
for i in 0..months.len() + 1 {
x_points.push(vec![0.0; past.len()]);
for season in 0..months[0].len() {
x_points[i][season] = (i + season*self.season) as f64;
}
}
let regression_points: Vec<Vec<f64>> = x_points[1..].iter().map(
|x| {x.iter().map(
|y| {
self.get_linear_regression().calculate(y.clone())
}
).collect()}
).collect();
let smooth = self.exp_smooth().as_time_series();
let x_points = (&x_points[2..]).to_vec();
let regression_points = (®ression_points).to_vec();
let mut distances = Vec::new();
for i in 0..x_points.len() {
distances.push(Vec::new());
for j in 0..x_points[i].len() {
distances[i].push(smooth.get_range_at(x_points[i][j] as usize) - regression_points[i][j]);
}
}
let mut factors = Vec::new();
for i in 0..distances.len() {
factors.push(Vec::new());
for j in 0..distances[i].len() - 1 {
let mut growth = distances[i][j + 1] / distances[i][j];
if distances[i][j] < 0.0 && distances[i][j + 1] > 0.0 {
growth = -1.0 * growth / 2.0;
}
if growth > 10.0 || growth < -10.0 {
growth = DEFAULT_ALPHA * growth;
}
factors[i].push(growth);
}
}
let mut pro_factors = Vec::new();
for i in 0..factors.len() {
if factors[i].len() != 2 {
panic!("HARDCODED FOR 2 FACTORS IN 3 YEARS");
}
pro_factors.push(factors[i][0] * 0.8 + factors[i][1]);
}
let mut last_d = Vec::new();
for i in 0..distances.len() {
last_d.push(distances[i][2] * pro_factors[i]);
}
println!("d = {:?}", distances);
println!("lastd = {:?}", last_d);
let mut finales = Vec::new();
for i in 0..last_d.len() {
finales.push(last_d[i] + self.get_linear_regression().calculate(i as f64 + 36.0));
}
let mut counter = 38.0;
for element in finales.iter() {
self.prediction.push((counter, element.clone()));
counter += 1.0;
}
self.prediction[0].0 = 37.0;
println!("finales: {:?}", self.prediction());
} else {
panic!("Can't update Dumbs data, no expsmooth set.");
}
}
pub fn prediction(&self) -> Vec<(f64, f64)> {
self.prediction.clone()
}
pub fn get_linear_regression(&self) -> LinearRegression {
if let Some(reg) = &self.linear_reg {
reg.clone()
} else {
panic!("No linear regression calculated for Dumb");
}
}
fn exp_smooth(&self) -> ExpSmoothing {
if let Some(ex) = &self.expsmooth {
ex.clone()
} else {
panic!("Dumb has no exponential smoothing set")
}
}
fn plot_to_file(&self, filename: String) {
Page::single(
self.plot().as_ref()
).save(filename).unwrap();
}
}
impl Plotable for Dumb {
fn plot(&self) -> Box<dyn View> {
let mut tm = self.original.clone();
let smooth = self.exp_smooth();
let linear = self.get_linear_regression();
let pred = TimeSeries::from_pairs_vec(self.prediction());
let mut group = Grouper::new(&tm)
.last_with_style(Style::from_color("#000000"))
.add(&smooth.as_time_series())
.last_with_style(Style::from_color("#af0af6"))
.add(&linear.as_time_series())
.last_with_style(Style::from_color("#87faa4"))
.add(&pred)
.last_with_style(Style::from_color("#ff00a2"));
group.plot()
}
fn as_plot(&self) -> Plot {
unimplemented!()
}
}