Spaces:
Configuration error
Configuration error
Create funcoes_modelos
Browse files- funcoes_modelos +105 -0
funcoes_modelos
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import urllib.request
|
5 |
+
import json
|
6 |
+
import plotly.express as px
|
7 |
+
import matplotlib.pyplot as plt
|
8 |
+
import yfinance as yf
|
9 |
+
import pandas as pd
|
10 |
+
import numpy as np
|
11 |
+
import matplotlib.pyplot as plt
|
12 |
+
import seaborn as sns
|
13 |
+
from datetime import datetime
|
14 |
+
import statsmodels.api as sm
|
15 |
+
|
16 |
+
from sklearn.linear_model import LinearRegression
|
17 |
+
from statsmodels.tsa.seasonal import seasonal_decompose
|
18 |
+
from sklearn.model_selection import TimeSeriesSplit
|
19 |
+
from sklearn.metrics import mean_squared_error
|
20 |
+
|
21 |
+
from statsforecast.models import HistoricAverage
|
22 |
+
from statsforecast.models import Naive
|
23 |
+
from statsforecast.models import RandomWalkWithDrift
|
24 |
+
from statsforecast.models import SeasonalNaive
|
25 |
+
from statsforecast.models import SimpleExponentialSmoothing
|
26 |
+
from statsforecast.models import HoltWinters
|
27 |
+
from statsforecast.models import AutoARIMA
|
28 |
+
from statsforecast.models import ARIMA
|
29 |
+
from statsforecast.models import GARCH
|
30 |
+
from statsforecast.models import ARCH
|
31 |
+
|
32 |
+
from statsmodels.graphics.tsaplots import plot_pacf
|
33 |
+
from statsmodels.graphics.tsaplots import plot_acf
|
34 |
+
|
35 |
+
from scipy.stats import shapiro
|
36 |
+
from datetime import datetime
|
37 |
+
import matplotlib.pyplot as plt
|
38 |
+
from meteostat import Point, Daily
|
39 |
+
|
40 |
+
from statsmodels.graphics.tsaplots import plot_pacf
|
41 |
+
from statsmodels.graphics.tsaplots import plot_acf
|
42 |
+
from statsmodels.tsa.statespace.sarimax import SARIMAX
|
43 |
+
from statsmodels.tsa.holtwinters import ExponentialSmoothing
|
44 |
+
from statsmodels.tsa.stattools import adfuller
|
45 |
+
import matplotlib.pyplot as plt
|
46 |
+
from tqdm import tqdm_notebook
|
47 |
+
from itertools import product
|
48 |
+
|
49 |
+
|
50 |
+
import warnings
|
51 |
+
warnings.filterwarnings('ignore')
|
52 |
+
|
53 |
+
def read_data():
|
54 |
+
# Set time period
|
55 |
+
start = datetime(2010, 1, 1)
|
56 |
+
end = pd.to_datetime(datetime.now().strftime("%Y-%m-%d"))
|
57 |
+
# Create Point for Vancouver, BC
|
58 |
+
vancouver = Point(49.2497, -123.1193, 70)
|
59 |
+
#campinas = Point(-22.9056, -47.0608, 686)
|
60 |
+
#saopaulo = Point(-23.5475, -46.6361, 769)
|
61 |
+
|
62 |
+
# Get daily data for 2018
|
63 |
+
data = Daily(vancouver, start, end)
|
64 |
+
data = data.fetch()
|
65 |
+
data = data[['tavg', 'prcp']]
|
66 |
+
|
67 |
+
return data
|
68 |
+
|
69 |
+
data = read_data()
|
70 |
+
returns = data['tavg']
|
71 |
+
|
72 |
+
def montar_dataframe_temp(returns):
|
73 |
+
|
74 |
+
temp = pd.DataFrame(returns)
|
75 |
+
temp['precip_ontem'] = data['prcp'].shift(1)
|
76 |
+
temp['precip_media_semana'] = temp['precip_ontem'].rolling(7).mean()
|
77 |
+
temp = temp.dropna(axis = 0)
|
78 |
+
return temp
|
79 |
+
|
80 |
+
def predict_ARIMA_GARCH(models, temp_train, n):
|
81 |
+
model = models[0]
|
82 |
+
model2 = models[1]
|
83 |
+
|
84 |
+
sarimax = sm.tsa.statespace.SARIMAX(temp_train['tavg'] , order=(1,0,1),
|
85 |
+
enforce_stationarity=False, enforce_invertibility=False, freq='D').fit()
|
86 |
+
|
87 |
+
resid = sarimax.resid.values
|
88 |
+
|
89 |
+
garch = model2.fit(resid)
|
90 |
+
|
91 |
+
pred1 = sarimax.forecast(n, exog = return_exog(temp_train, n).values).values
|
92 |
+
pred2 = garch.predict(n)
|
93 |
+
|
94 |
+
predictions = pred1 - pred2['mean']
|
95 |
+
|
96 |
+
return predictions
|
97 |
+
def return_exog(temp, n):
|
98 |
+
|
99 |
+
exog = pd.DataFrame(columns = ['precip_ontem', 'precip_media_semana'])
|
100 |
+
|
101 |
+
exog['precip_ontem'] = np.ones(n)*temp.iloc[-1]['precip_ontem']
|
102 |
+
|
103 |
+
exog['precip_media_semana'] = np.ones(n)*temp.iloc[-1]['precip_media_semana']
|
104 |
+
|
105 |
+
return exog
|