OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to use Block Bootstrap for time series?

  • Thread starter Thread starter Amanda
  • Start date Start date
A

Amanda

Guest
I am making predictions for a time series df_int. The target variable is pct_viagem_spot and the only two features are pct_viagem_spot and preco_medio_diesel. Initially, I used the normal and common bootstrap method to obtain the prediction through the mean and the confidence intervals using the formula with the standard deviation:

Code:
pred = []

for j in range(1, n_bootstrap+1):
   np.random.seed(j)

   # Bootstrap Sample
   indices_bootstrap = np.random.normal(range(len(df_int['DTCI'])), size=len(df_int['DTCI']), replace=True)

   feature_1 = df_int['preco_medio_diesel'][indices_bootstrap]
   feature_2 = df_int['DTCI'][indices_bootstrap]
   target_0 = df_int['pct_viagem_spot'][indices_bootstrap] 
        
   model = RandomForestRegressor(criterion="squared_error", max_depth=8, min_weight_fraction_leaf=0.01, max_leaf_nodes=15, min_impurity_decrease=0.0005)

   model.fit(np.column_stack((feature_1, feature_2)), target_0)         

   # Bootstrap result
   predicao_fore = model.predict(X_fore)  
   pred.append(predicao_fore)

predicao = pd.DataFrame(pred)

# Predictions mean and stand deviation
media = predicao.mean().tolist()
desvio = np.std(predicao, axis=0)

However, I believe that I should use Block Bootstrap instead of the simple technique, due to the temporal and sequential nature of the data. I tried using random.normal insted off random.choice but it hasn't worked yet. Does anyone know how to adapt the code to use Block Bootstrap or another technique that is suitable for time series?
<p>I am making predictions for a time series <code>df_int</code>. The target variable is <code>pct_viagem_spot</code> and the only two features are <code>pct_viagem_spot</code> and <code>preco_medio_diesel</code>. Initially, I used the normal and common bootstrap method to obtain the prediction through the mean and the confidence intervals using the formula with the standard deviation:</p>
<pre><code>pred = []

for j in range(1, n_bootstrap+1):
np.random.seed(j)

# Bootstrap Sample
indices_bootstrap = np.random.normal(range(len(df_int['DTCI'])), size=len(df_int['DTCI']), replace=True)

feature_1 = df_int['preco_medio_diesel'][indices_bootstrap]
feature_2 = df_int['DTCI'][indices_bootstrap]
target_0 = df_int['pct_viagem_spot'][indices_bootstrap]

model = RandomForestRegressor(criterion="squared_error", max_depth=8, min_weight_fraction_leaf=0.01, max_leaf_nodes=15, min_impurity_decrease=0.0005)

model.fit(np.column_stack((feature_1, feature_2)), target_0)

# Bootstrap result
predicao_fore = model.predict(X_fore)
pred.append(predicao_fore)

predicao = pd.DataFrame(pred)

# Predictions mean and stand deviation
media = predicao.mean().tolist()
desvio = np.std(predicao, axis=0)
</code></pre>
<p>However, I believe that I should use Block Bootstrap instead of the simple technique, due to the temporal and sequential nature of the data. I tried using <code>random.normal</code> insted off <code>random.choice</code> but it hasn't worked yet. Does anyone know how to adapt the code to use Block Bootstrap or another technique that is suitable for time series?</p>
 

Latest posts

Top