1
1
from hyperopt import fmin , tpe , hp , STATUS_OK
2
2
from luminaire .model import LADStructuralModel , LADStructuralHyperParams , LADFilteringModel , LADFilteringHyperParams
3
3
from luminaire .exploration .data_exploration import DataExploration
4
+ from luminaire .utils .random_state_validation import check_random_state
4
5
import warnings
5
6
warnings .filterwarnings ('ignore' )
6
7
7
-
8
8
class HyperparameterOptimization (object ):
9
9
"""
10
10
Hyperparameter optimization for LAD outlier detection configuration for batch data.
@@ -20,6 +20,7 @@ class HyperparameterOptimization(object):
20
20
:param int min_ts_length: The minimum required length of the time series for training. The input time series will be
21
21
truncated if the length is greater than this value.
22
22
:param int scoring_length: Number of innovations to be scored after training window with respect to the frequency.
23
+ :param int random_state: Turn seed into a np.random.RandomState instance
23
24
24
25
.. _Pandas offset: https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#dateoffset-objects
25
26
"""
@@ -31,6 +32,7 @@ def __init__(self,
31
32
max_ts_length = None ,
32
33
min_ts_length = None ,
33
34
scoring_length = None ,
35
+ random_state = None ,
34
36
** kwargs ):
35
37
self ._target_metric = 'raw'
36
38
self .freq = freq
@@ -48,6 +50,8 @@ def __init__(self,
48
50
self .scoring_length = scoring_length or (scoring_length_dict .get (freq )
49
51
if freq in scoring_length_dict .keys () else 30 )
50
52
53
+ self .random_state = random_state
54
+
51
55
def _mape (self , actuals , predictions ):
52
56
"""
53
57
This function computes the mean absolute percentage error for the observed vs the predicted values.
@@ -93,7 +97,8 @@ def _synthetic_anomaly_check(self, observation, prediction, std_error):
93
97
94
98
# Anomaly detection based on synthetic anomalies generated through a given intensity list
95
99
for prop in self .anomaly_intensity_list :
96
- trial_prob = np .random .uniform (0 , 1 , 1 )
100
+ rnd = check_random_state (self .random_state )
101
+ trial_prob = rnd .uniform (0 , 1 , 1 )
97
102
if trial_prob < 0.4 :
98
103
synthetic_value = observation + (prop * observation )
99
104
anomaly_flags .append (1 )
@@ -227,7 +232,8 @@ def _objective_part(self, data, smoothed_series, args):
227
232
anomaly_probabilities_list = []
228
233
local_model = copy .deepcopy (stable_model )
229
234
for i , row in scoring_data .iterrows ():
230
- trial_prob = np .random .uniform (0 , 1 , 1 )
235
+ rnd = check_random_state (self .random_state )
236
+ trial_prob = rnd .random .uniform (0 , 1 , 1 )
231
237
observed_value = row .raw
232
238
synthetic_actual = observed_value
233
239
if trial_prob < 0.4 :
@@ -263,7 +269,7 @@ def _optimize(self, data, objective_part, algo=tpe.suggest, max_evals=50):
263
269
:return: Optimal hyperparameters
264
270
:rtype: dict
265
271
"""
266
-
272
+ import numpy as np
267
273
from functools import partial
268
274
from pykalman import KalmanFilter
269
275
@@ -288,7 +294,7 @@ def _optimize(self, data, objective_part, algo=tpe.suggest, max_evals=50):
288
294
289
295
try :
290
296
series = data [self ._target_metric ].values
291
- kf = KalmanFilter ()
297
+ kf = KalmanFilter (random_state = self . random_state )
292
298
smoothed_series , cov_series = kf .em (series ).smooth (series )
293
299
except :
294
300
raise ValueError ('Kalman Smoothing requires more than one data point' )
@@ -299,7 +305,7 @@ def _optimize(self, data, objective_part, algo=tpe.suggest, max_evals=50):
299
305
raise ValueError ('Only `detection_type=OutlierDetection` is supported in hyperparameter optimization right now' )
300
306
301
307
# Calling the optimization function
302
- hyper_param = fmin (objective , space = space , algo = algo , max_evals = max_evals , show_progressbar = True )
308
+ hyper_param = fmin (objective , space = space , algo = algo , max_evals = max_evals , show_progressbar = True , rstate = np . random . default_rng ( self . random_state ) )
303
309
hyper_param ['LuminaireModel' ] = hyper_param_list [hyper_param ['LuminaireModel' ]]['model' ]
304
310
if 'max_ft_freq' in hyper_param :
305
311
hyper_param ['max_ft_freq' ] = hyper_param ['max_ft_freq' ] + 2
0 commit comments