├── ARIMAX ├── README.md ├── README_images │ ├── ARIMAX212.png │ ├── figure_0.png │ ├── figure_1.png │ └── image_0.png ├── my_arimax_predict_fcn.m ├── recursive_arimax_model_update.slx └── time_series_forecasting.mlx ├── GRU ├── README.md ├── README_images │ ├── figure_0.png │ ├── figure_1.png │ ├── figure_2.png │ ├── figure_3.png │ ├── image_0.png │ ├── image_1.png │ ├── image_2.png │ ├── image_3.png │ ├── image_4.png │ └── image_5.png ├── TrainedGRUNetwork.mat ├── my_update_predict_fcn.m ├── recursive_gru_update.slx └── time_series_forecasting.mlx ├── LSTM ├── README.md ├── README_images │ ├── figure_0.png │ ├── figure_1.png │ ├── figure_2.png │ ├── figure_3.png │ ├── image_0.png │ ├── image_1.png │ ├── image_2.png │ ├── image_3.png │ ├── image_4.png │ └── image_5.png ├── TrainedLSTMNetwork.mat ├── my_update_predict_fcn.m ├── recursive_lstm_update.slx └── time_series_forecasting.mlx ├── README.md ├── README_EN.md ├── SSM ├── README.md ├── README_images │ ├── figure_0.png │ ├── figure_1.png │ └── image_0.png ├── my_predict_fcn.m ├── recursive_ssm_update.slx ├── rwAR2ParamMap.m └── time_series_forecasting.mlx └── TimeSeriesForecasting_on_Simulink.pdf /ARIMAX/README.md: -------------------------------------------------------------------------------- 1 | # ARIMAX モデルによる実質 GNP の再帰的予測 2 | 3 | 4 | 当サンプルは Econometrics Toolbox によって提供される ARIMAX モデルを Simulink へ実装する方法を示します。具体的には、ARIMAX(2,1,2) を用いて米国における実質 GNP を 工業生産指数 (`IPI`)、 雇用率 (`E`)、そして実質賃金 (`WR`) の階差を外生変数として再帰的に推定します 5 | 6 | 7 | ![ARIMAX212.png](README_images/ARIMAX212.png) 8 | 9 | 10 | 11 | ただし、 12 | 13 | 14 | 15 | 16 | : Lag operator () 17 | 18 | 19 | 20 | 21 | : 工業生産指数の階差 22 | 23 | 24 | 25 | 26 | : 雇用率の階差 27 | 28 | 29 | 30 | 31 | : 実質賃金の階差 32 | 33 | 34 | 35 | 36 | 37 | 尚、当サンプルは Econometrics Toolbox によって提供されるこちらの例題に基づいております 38 | 39 | 40 | 41 | 42 | [https://www.mathworks.com/help/econ/specify-presample-and-forecast-period-data-to-forecast-arimax-model.html](https://jp.mathworks.com/help/econ/specify-presample-and-forecast-period-data-to-forecast-arimax-model.html) 43 | 44 | 45 | 46 | 47 | [https://www.mathworks.com/help/econ/rolling-window-estimation-of-state-space-models.html](https://www.mathworks.com/help/econ/rolling-window-estimation-of-state-space-models.html) 48 | 49 | 50 | # 1: 年次経済データをインポート 51 | 52 | ```matlab:Code 53 | load Data_NelsonPlosser 54 | ``` 55 | 56 | 57 | 58 | ※) 当経済データは米国のものです 59 | 60 | 61 | # 2: インポートしたデータを前処理 62 | 63 | ```matlab:Code 64 | isNaN = any(ismissing(DataTable),2); % Flag periods containing NaNs 65 | y = DataTable.GNPR(~isNaN); 66 | y(1) = []; % remove the 1st element because of taking the difference of exogenous variables 67 | 68 | IX = DataTable{~isNaN,["IPI" "E" "WR"]}; 69 | X = diff(IX,1); % compute the row-wise difference 70 | X1 = X(:,1); 71 | X2 = X(:,2); 72 | X3 = X(:,3); 73 | ``` 74 | 75 | 76 | # 3: 前処理したデータから時間付き部分時系列配列を作成 77 | 78 | 79 | Simulink の "From Workspace" ブロック用に時間付き部分時系列配列を作成します 80 | 81 | 82 | 83 | ```matlab:Code 84 | P = 2; % degree of AR terms 85 | D = 1; 86 | 87 | WindowSize = 31; 88 | ForecastPeriod = numel(y) - WindowSize - P - D; 89 | 90 | yyT = zeros(ForecastPeriod, WindowSize+P+D+1); 91 | XX1 = zeros(ForecastPeriod, WindowSize+1); 92 | XX2 = zeros(ForecastPeriod, WindowSize+1); 93 | XX3 = zeros(ForecastPeriod, WindowSize+1); 94 | 95 | m = 1; 96 | for nYear = 1:ForecastPeriod 97 | yyT(nYear,:) = transpose(y(m:m+WindowSize+P+D)); 98 | XX1(nYear,:) = transpose(X1(m+P+D:m+WindowSize+P+D)); 99 | XX2(nYear,:) = transpose(X2(m+P+D:m+WindowSize+P+D)); 100 | XX3(nYear,:) = transpose(X3(m+P+D:m+WindowSize+P+D)); 101 | m = m + 1; 102 | end 103 | 104 | yy0 = yyT(:,1:P+D); 105 | yyT = yyT(:,1+P+D:end); 106 | 107 | Time = str2double(DataTable.Properties.RowNames(~isNaN)); 108 | Time = Time((end-ForecastPeriod+1:end)); 109 | 110 | % Targeted Time Series 111 | RealGNP = [Time, yyT]; 112 | PreRealGNP = [Time, yy0]; 113 | 114 | % Exogenous variables 115 | dIPI = [Time, XX1]; 116 | dE = [Time, XX2]; 117 | dWR = [Time, XX3]; 118 | ``` 119 | 120 | # 4: ARIMAX(2,1,2) モデルのパラメータ推定および翌年の実質 GNP の推定を再帰的に実施 (MATLAB編) 121 | 122 | ```matlab:Code 123 | eGNPR = zeros(numel(Time),1); 124 | 125 | for t = 0:numel(Time)-1 126 | sY = yyT(t+1,:)'; 127 | sX = [XX1(t+1,:); XX2(t+1,:); XX3(t+1,:)]'; 128 | Mdl = arima(P,D,2); 129 | Mdl = estimate(Mdl,sY(1:end-1),'Y0',yy0(t+1,:)','X',sX(1:end-1,:),'Display','off'); 130 | eGNPR(t+1) = forecast(Mdl,1,sY(end-P-D:end-1),'X0',sX(end-2:end-1,:),'XF',sX(end,:)); 131 | end 132 | ``` 133 | 134 | # 5: 実際の実質 GNP と MATLAB にて推定された実質 GNP をプロット 135 | 136 | ```matlab:Code 137 | figure; axH = axes; 138 | plot(axH, Time, y(end-numel(Time)+1:end), 'Color', [0.9290 0.6940 0.1250], 'LineWidth', 1.2); 139 | hold(axH, 'on'); grid(axH, 'on'); 140 | plot(axH, Time, eGNPR, 'Color', [0 0.4470 0.7410], 'LineWidth', 1.2); 141 | axH.XLim(1) = Time(1); 142 | axH.Color = [0.5020 0.5020 0.5020]; 143 | axH.Title.String = 'Real GNP in the US'; 144 | legend(["Actual", "Forecasted"]) 145 | ``` 146 | 147 | 148 | ![figure_0.png](README_images/figure_0.png) 149 | 150 | # 6: Simulink モデルの開示とパラメータ設定 151 | 152 | 153 | ![image_0.png](README_images/image_0.png) 154 | 155 | 156 | 157 | ```matlab:Code 158 | mdl = 'recursive_arimax_model_update'; 159 | open_system(mdl); 160 | 161 | set_param(mdl, 'Solver', 'FixedStepAuto'); 162 | set_param(mdl, 'FixedStep', '1'); 163 | set_param(mdl, 'StartTime', num2str(Time(1))); 164 | set_param(mdl, 'StopTime', num2str(Time(end))); 165 | ``` 166 | 167 | # 7: ARIMAX(2,1,2) モデルのパラメータ推定および翌年の実質 GNP の推定を再帰的に実施 (Simulink編) 168 | 169 | ```matlab:Code 170 | out = sim(mdl); 171 | ``` 172 | 173 | # 8: 実際の実質 GNP と Simulink にて推定された実質 GNP をプロット 174 | 175 | ```matlab:Code 176 | figure; axH = axes; 177 | plot(axH, Time, out.ScopeOut.signals(1).values, 'Color', [0.9290 0.6940 0.1250], 'LineWidth', 1.2); 178 | hold(axH, 'on'); grid(axH, 'on'); 179 | plot(axH, Time, out.ScopeOut.signals(2).values, 'Color', [0 0.4470 0.7410], 'LineWidth', 1.2); 180 | axH.XLim(1) = Time(1); 181 | axH.Color = [0.5020 0.5020 0.5020]; 182 | axH.Title.String = 'Real GNP in the US'; 183 | legend(["Actual", "Forecasted"]) 184 | ``` 185 | 186 | 187 | ![figure_1.png](README_images/figure_1.png) 188 | 189 | # 9: MATLAB と Simulink の一致性検証 190 | 191 | ```matlab:Code 192 | isequal(out.ScopeOut.signals(2).values, eGNPR) 193 | ``` 194 | 195 | 196 | ```text:Output 197 | ans = 198 | 1 199 | 200 | ``` 201 | 202 | -------------------------------------------------------------------------------- /ARIMAX/README_images/ARIMAX212.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/ARIMAX/README_images/ARIMAX212.png -------------------------------------------------------------------------------- /ARIMAX/README_images/figure_0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/ARIMAX/README_images/figure_0.png -------------------------------------------------------------------------------- /ARIMAX/README_images/figure_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/ARIMAX/README_images/figure_1.png -------------------------------------------------------------------------------- /ARIMAX/README_images/image_0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/ARIMAX/README_images/image_0.png -------------------------------------------------------------------------------- /ARIMAX/my_arimax_predict_fcn.m: -------------------------------------------------------------------------------- 1 | function yhat = my_arimax_predict_fcn(Degree, y, y0, X) 2 | 3 | p = Degree(1); 4 | D = Degree(2); 5 | q = Degree(3); 6 | 7 | Mdl = arima(p, D, q); 8 | 9 | % Fit ARIMAX model 10 | Mdl = estimate(Mdl,y,'Y0',y0,'X',X(1:end-1,:),'Display','off'); 11 | 12 | % Forecase the real GNP in the next time step 13 | yhat = forecast(Mdl,1,y(end-p-D-1:end),'X0',X(end-2:end-1,:),'XF',X(end,:)); 14 | 15 | end 16 | -------------------------------------------------------------------------------- /ARIMAX/recursive_arimax_model_update.slx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/ARIMAX/recursive_arimax_model_update.slx -------------------------------------------------------------------------------- /ARIMAX/time_series_forecasting.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/ARIMAX/time_series_forecasting.mlx -------------------------------------------------------------------------------- /GRU/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/GRU/README.md -------------------------------------------------------------------------------- /GRU/README_images/figure_0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/GRU/README_images/figure_0.png -------------------------------------------------------------------------------- /GRU/README_images/figure_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/GRU/README_images/figure_1.png -------------------------------------------------------------------------------- /GRU/README_images/figure_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/GRU/README_images/figure_2.png -------------------------------------------------------------------------------- /GRU/README_images/figure_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/GRU/README_images/figure_3.png -------------------------------------------------------------------------------- /GRU/README_images/image_0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/GRU/README_images/image_0.png -------------------------------------------------------------------------------- /GRU/README_images/image_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/GRU/README_images/image_1.png -------------------------------------------------------------------------------- /GRU/README_images/image_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/GRU/README_images/image_2.png -------------------------------------------------------------------------------- /GRU/README_images/image_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/GRU/README_images/image_3.png -------------------------------------------------------------------------------- /GRU/README_images/image_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/GRU/README_images/image_4.png -------------------------------------------------------------------------------- /GRU/README_images/image_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/GRU/README_images/image_5.png -------------------------------------------------------------------------------- /GRU/TrainedGRUNetwork.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/GRU/TrainedGRUNetwork.mat -------------------------------------------------------------------------------- /GRU/my_update_predict_fcn.m: -------------------------------------------------------------------------------- 1 | function yhat = my_update_predict_fcn(lastvalue) 2 | 3 | oldnet = evalin('base', 'net'); 4 | [newnet, yhat] = predictAndUpdateState(oldnet, lastvalue); 5 | assignin('base', 'net', newnet); 6 | 7 | end -------------------------------------------------------------------------------- /GRU/recursive_gru_update.slx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/GRU/recursive_gru_update.slx -------------------------------------------------------------------------------- /GRU/time_series_forecasting.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/GRU/time_series_forecasting.mlx -------------------------------------------------------------------------------- /LSTM/README.md: -------------------------------------------------------------------------------- 1 | # LSTM層を含んだニューラルネットワークによる水痘発生件数推移モデルの構築 2 | # はじめに 3 | 4 | 5 | 当例題は、41.5年分の水痘患者数を収めたMATファイル「chickenpox_dataset.mat」にあるデータに対して、再帰的ニューラルネットワーク向けの層の1つである LSTM (Long Short Term Memory) を適用して予測モデルを構築します。尚、当例題は Deep Learning Toolbox に付随する例題の1つ「[Time Series Forecasting Using Deep Learning](https://www.mathworks.com/help/deeplearning/examples/time-series-forecasting-using-deep-learning.html)」に基づいております 6 | 7 | 8 | # LSTMについて 9 | 10 | 11 | LSTMは、時系列向けの再帰的なニューラルネットワークを表現する層の1つです。旧来の Elman 型ネットワークとは異なり内部構造は複雑で現在時刻 からの遠近に伴って重みを変えているのが特徴です 12 | 13 | 14 | 15 | 16 | 17 | ![image_0.png](README_images/image_0.png) 18 | 19 | 20 | 21 | 22 | ![image_1.png](README_images/image_1.png) 23 | 24 | 25 | 26 | # 1: MAT-ファイルからデータをインポート 27 | 28 | 29 | 月次の水痘患者数を収めた「chickenpox_dataset.mat」からデータをインポートします 30 | 31 | 32 | 33 | ```matlab:Code 34 | matobj = matfile('chickenpox_dataset.mat'); 35 | RawDataCell = matobj.chickenpoxTargets; 36 | RawData = [RawDataCell{:}]'; 37 | T = 1:numel(RawData); 38 | ``` 39 | 40 | # 2: 水痘患者発生件数を月次で可視化 41 | 42 | 43 | 毎月の水痘患者数をグラフ表示します 44 | 45 | 46 | 47 | ```matlab:Code 48 | figure 49 | plot(RawData) 50 | xlabel("Month") 51 | ylabel("Cases") 52 | title("Monthly Cases of Chickenpox") 53 | ``` 54 | 55 | 56 | ![figure_0.png](README_images/figure_0.png) 57 | 58 | # 3: データの正規化 59 | 60 | 61 | ![image_2.png](README_images/image_2.png) 62 | 63 | 64 | 65 | 66 | Statistics and Machine Learning Toolbox が提供する「zscore」関数を利用すると平均を引いて標準偏差で割るという正規化の作業を1行で済ませることができます 67 | 68 | 69 | 70 | ```matlab:Code 71 | [nData, mu, sigma] = zscore(RawData); 72 | ``` 73 | 74 | # 4: データの分割 75 | 76 | 77 | 前半の約90%を学習用のデータとして、後半の約10%を検証用のデータとして分割します 78 | 79 | 80 | 81 | ```matlab:Code 82 | NumTrain = floor(0.9*numel(nData)); 83 | 84 | TrainData = nData(1:NumTrain+1); 85 | TestData = nData(NumTrain+1:end); 86 | 87 | ``` 88 | 89 | # 5: LSTM ネットワークの構築 90 | 91 | 92 | 「アプリ」タブを選択し、一覧の中にある「Deep Network Designer」を選びます。そして、下図のようなネットワークを構築します 93 | 94 | 95 | 96 | 97 | ![image_3.png](README_images/image_3.png) 98 | 99 | 100 | 101 | 102 | 103 | コマンドラインの場合、次のように書くと等価なネットワークを作成することができます。Deep Network Designer には作成したネットワークに対して MATLAB コードを生成する機能が提供されております 104 | 105 | 106 | 107 | ```matlab:Code 108 | layers_1 = [ ... 109 | sequenceInputLayer(1) 110 | lstmLayer(200) 111 | fullyConnectedLayer(1) 112 | regressionLayer]; 113 | ``` 114 | 115 | # 6: 学習オプションの設定 116 | 117 | ```matlab:Code 118 | options = trainingOptions('adam', ... 119 | 'MaxEpochs',250, ... 120 | 'GradientThreshold',1, ... 121 | 'InitialLearnRate',0.005, ... 122 | 'LearnRateSchedule','piecewise', ... 123 | 'LearnRateDropPeriod',125, ... 124 | 'LearnRateDropFactor',0.2, ... 125 | 'Verbose',0, ... 126 | 'Plots','training-progress'); 127 | ``` 128 | 129 | 130 | # 7: 学習の実行 131 | 132 | ```matlab:Code 133 | %% Train LSTM network 134 | 135 | XTrain = TrainData(1:end-1)'; % transpose to make the vector horizontally 136 | YTrain = TrainData(2:end)'; 137 | 138 | doTrain = true; 139 | 140 | if doTrain 141 | % tic; 142 | net = trainNetwork(XTrain,YTrain,layers_1,options); 143 | % toc; % Elapsed time is 113.584624 seconds 144 | save('TrainedLSTMNetwork', 'net') 145 | net0 = net; % backup the trained network for Simulink demo 146 | else 147 | PreTrainedStr = load("TrainedLSTMNetwork"); 148 | net = PreTrainedStr.net; 149 | net0 = net; % backup the pre-trained network for Simulink demo 150 | end 151 | ``` 152 | 153 | 154 | ![figure_1.png](README_images/figure_1.png) 155 | 156 | 157 | ```matlab:Code 158 | 159 | ``` 160 | 161 | # 8: 50か月の発生件数推移を再帰的に予想 (MATLAB編) 162 | 163 | 164 | Simulink によるシミュレーションの実行の前に、MATLAB 上で再帰的に発生件数を推定します。SeriesNetworkクラス「net」のメソッドの1つ「predictAndUpdateState」は、推定値を使ってネットワークを更新する機能を提供します 165 | 166 | 167 | 168 | ```matlab:Code 169 | NumTest = numel(TestData) - 1; 170 | YPred = zeros(1, NumTest); 171 | 172 | for n = 1:NumTest 173 | [net, YPred(n)] = predictAndUpdateState(net, TestData(n)); 174 | end 175 | 176 | RMSE = sqrt(mean((YPred-TestData(2:end)').^2)); 177 | disp(['RMSE = ', num2str(RMSE)]) 178 | ``` 179 | 180 | 181 | ```text:Output 182 | RMSE = 0.44139 183 | ``` 184 | 185 | 186 | 187 | 188 | 推定値の時系列「YPred」は、正規化された値になっていますので元のスケールに戻し「rYPred」とします 189 | 190 | 191 | 192 | ```matlab:Code 193 | rYPred = sigma .* YPred + mu; 194 | ``` 195 | 196 | # 9: MATLAB による推移予想の可視化 197 | 198 | ```matlab:Code 199 | figure; axH = axes; 200 | pH1 = plot(axH, T, RawData); 201 | hold on 202 | pH2 = plot(axH, T(NumTrain+1:end),[RawData(NumTrain+1) rYPred],".-"); 203 | fH = fill(axH, T([1,NumTrain, NumTrain, 1]), [repmat(axH.YLim(1),1,2), repmat(axH.YLim(2),1,2)], [.9, .9, .9]); 204 | uistack(fH,"bottom") 205 | hold off 206 | xlabel("Month") 207 | ylabel("Cases") 208 | title("Forecast the number of chickenpox cases using LSTM") 209 | legend([pH1, pH2, fH], ["Observation" "Forecast", "Training Period"]) 210 | ``` 211 | 212 | 213 | ![figure_2.png](README_images/figure_2.png) 214 | 215 | # 10: 50か月の発生件数推移を再帰的に予想 (Simulink編) 216 | 217 | 218 | 次に、等価な再帰的推定を Simulink 上で実施します。事前学習済みのネットワークを初期化し、Simulink モデルへ時系列データを入力変数として渡すために時刻データ配列「TimeData」および部分時系列配列「SimInVariables」を作成します 219 | 220 | 221 | 222 | 223 | ![image_4.png](README_images/image_4.png) 224 | 225 | 226 | 227 | ```matlab:Code 228 | net = net0; % reset the pre-trained network 229 | 230 | TimeData = T(NumTrain+1:end-1).'; 231 | SimInVariables = [TestData(1:end-1), TestData(2:end)]; 232 | ``` 233 | 234 | 235 | 236 | そして、「set_param」コマンドを使ってソルバーやステップサイズなどを設定した後に「run」を実行するとシミュレーションが開始します 237 | 238 | 239 | 240 | 241 | ![image_5.png](README_images/image_5.png) 242 | 243 | 244 | 245 | ```matlab:Code 246 | mdl = 'recursive_lstm_update'; 247 | open_system(mdl); 248 | 249 | set_param(mdl, 'Solver', 'FixedStepAuto'); 250 | set_param(mdl, 'FixedStep', '1'); 251 | set_param(mdl, 'StartTime', num2str(TimeData(1))); 252 | set_param(mdl, 'StopTime', num2str(TimeData(end))); 253 | 254 | % Run simulation 255 | out = sim(mdl); 256 | ``` 257 | 258 | # 11: Simulink による推移予想の可視化 259 | 260 | 261 | MATLAB の時と同様に Simulink モデル を介した推移予測を可視化します 262 | 263 | 264 | 265 | ```matlab:Code 266 | figure; axH2 = axes; 267 | pH3 = plot(axH2, T, RawData); 268 | hold on 269 | pH4 = plot(axH2, T(NumTrain+1:end),[RawData(NumTrain+1); out.ScopeOut.signals(1).values],".-"); 270 | fH2= fill(axH2, T([1,NumTrain, NumTrain, 1]), [repmat(axH2.YLim(1),1,2), repmat(axH2.YLim(2),1,2)], [.9, .9, .9]); 271 | uistack(fH2,"bottom") 272 | hold off 273 | xlabel("Month") 274 | ylabel("Cases") 275 | title("Forecast the number of chickenpox cases using LSTM") 276 | legend([pH3, pH4, fH2], ["Observation" "Forecast", "Training Period"]) 277 | ``` 278 | 279 | 280 | ![figure_3.png](README_images/figure_3.png) 281 | 282 | # 12: MATLAB と Simulink の一致性検証 283 | 284 | ```matlab:Code 285 | isequal(out.ScopeOut.signals(1).values, rYPred.') 286 | ``` 287 | 288 | 289 | ```text:Output 290 | ans = 291 | 1 292 | 293 | ``` 294 | 295 | -------------------------------------------------------------------------------- /LSTM/README_images/figure_0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/LSTM/README_images/figure_0.png -------------------------------------------------------------------------------- /LSTM/README_images/figure_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/LSTM/README_images/figure_1.png -------------------------------------------------------------------------------- /LSTM/README_images/figure_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/LSTM/README_images/figure_2.png -------------------------------------------------------------------------------- /LSTM/README_images/figure_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/LSTM/README_images/figure_3.png -------------------------------------------------------------------------------- /LSTM/README_images/image_0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/LSTM/README_images/image_0.png -------------------------------------------------------------------------------- /LSTM/README_images/image_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/LSTM/README_images/image_1.png -------------------------------------------------------------------------------- /LSTM/README_images/image_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/LSTM/README_images/image_2.png -------------------------------------------------------------------------------- /LSTM/README_images/image_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/LSTM/README_images/image_3.png -------------------------------------------------------------------------------- /LSTM/README_images/image_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/LSTM/README_images/image_4.png -------------------------------------------------------------------------------- /LSTM/README_images/image_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/LSTM/README_images/image_5.png -------------------------------------------------------------------------------- /LSTM/TrainedLSTMNetwork.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/LSTM/TrainedLSTMNetwork.mat -------------------------------------------------------------------------------- /LSTM/my_update_predict_fcn.m: -------------------------------------------------------------------------------- 1 | function yhat = my_update_predict_fcn(lastvalue) 2 | 3 | oldnet = evalin('base', 'net'); 4 | [newnet, yhat] = predictAndUpdateState(oldnet, lastvalue); 5 | assignin('base', 'net', newnet); 6 | 7 | end -------------------------------------------------------------------------------- /LSTM/recursive_lstm_update.slx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/LSTM/recursive_lstm_update.slx -------------------------------------------------------------------------------- /LSTM/time_series_forecasting.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/LSTM/time_series_forecasting.mlx -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![View Time-Series-Forecasting-Simulink on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://jp.mathworks.com/matlabcentral/fileexchange/75638-time-series-forecasting-simulink) 2 | 3 | # Recursive Forecasting using Arbitrary Time Series Model on Simulink® 4 | 5 | 時系列の予測を Simulink モデル上で!​ 6 | 7 | [Link to README in English](https://github.com/mathworks/Time-Series-Forecasting-Simulink/blob/master/README_EN.md) 8 | 9 | ## Introduction 10 | 11 | このページでは次のようなご要望に対して「どのように実装するか」を具体的に紹介をいたします 12 | 13 | - Deep Learning を Simulink モデルに組み入れたい​ 14 | - 色々な時系列モデルを Simulink 上で試してみたい​ 15 | - MATLAB でサポートされる機能をモデルに実装したい​ 16 | 17 | 各フォルダには対応する時系列モデルまたはニューラネットワークの層を適用したMATLABコードおよびSimulinkモデルが保存されております 18 | 19 | ## Key Takeaways 20 | 21 | 時系列を再帰的に推定する機能を提供する​ 22 | 23 | * Deep Learning Toolbox™​ 24 | * Econometrics Toolbox™​ 25 | 26 | 27 | に焦点を当てて MATLAB® Function ブロックを介して Simulink モデルに実装する方法を示します。しかし、これらの製品に限らず​ 28 | 29 | - Predictive Maintenance Toolbox™​ 30 | - Statistics and Machine Learning Toolbox™​ 31 | - System Identification Toolbox™​ 32 | 33 | 34 | など任意の製品が提供する時系列解析機能(特に回帰)にも応用することが可能です。 35 | 36 | 詳細に関しては同梱されているPDFファイル 37 | [TimeSeriesForecasting_on_Simulink.pdf](https://github.com/mathworks/Time-Series-Forecasting-Simulink/blob/master/TimeSeriesForecasting_on_Simulink.pdf) をご覧ください 38 | 39 | 40 | ## Requirements 41 | 42 | * MATLAB R2020a 43 | * Simulink 44 | * Deep Learning Toolbox (requireed by GRU/LSTM) 45 | * Econometrics Toolbox (required by ARIMAX/SSM) 46 | 47 | -------------------------------------------------------------------------------- /README_EN.md: -------------------------------------------------------------------------------- 1 | # Recursive Forecasting using Arbitrary Time Series Model on Simulink® 2 | 3 | Let's run time series forecasting on the Simulink!!​ 4 | 5 | ## Introduction 6 | 7 | This page introduces how to implement time series models concretely for the following requirements. 8 | 9 | - Build in Deep Learning capabilities on a developing Simulink model​ 10 | - Try various time series models on the Simulink​ 11 | - Implement features supported by MATLAB Product family on a Simulink model​ 12 | 13 | Each folder has MATLAB codes and Simulink model, and their names correspond to time series models or layers of neural network respectively. 14 | 15 | ## Key Takeaways 16 | 17 | This page focuses on the 2 products.​ 18 | 19 | * Deep Learning Toolbox™​ 20 | * Econometrics Toolbox™​ 21 | 22 | They offer features to forecast time series recursively and each example describes how to implement their features on the Simulink and to invoke them via the MATLAB Function block. This technique does not apply only to the above products but can be adopted additional features for time series analysis in particular regression, which are provided by other products such as 23 | 24 | - Predictive Maintenance Toolbox™​ 25 | - Statistics and Machine Learning Toolbox™​ 26 | - System Identification Toolbox™​ 27 | 28 | 29 | ## Requirements 30 | 31 | * MATLAB R2020a 32 | * Simulink 33 | * Deep Learning Toolbox (requireed by GRU/LSTM) 34 | * Econometrics Toolbox (requireed by ARIMAX/SSM) 35 | 36 | -------------------------------------------------------------------------------- /SSM/README.md: -------------------------------------------------------------------------------- 1 | # 状態空間モデルによる失業率の再帰的予測 2 | 3 | 4 | 当サンプルは Econometrics Toolbox によって提供される状態空間モデルを Simulink モデルへ実装する方法を示します。ここでは以下のような線型な状態空間モデルを用いて米国における翌年の失業率を再帰的に推定しす 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 | 尚、当サンプルは Econometrics Toolbox によって提供されるこちらの例題に基づいております 39 | 40 | 41 | 42 | 43 | [https://www.mathworks.com/help/econ/choose-state-space-model-specification-using-backtesting.html](https://www.mathworks.com/help/econ/choose-state-space-model-specification-using-backtesting.html) 44 | 45 | 46 | 47 | 48 | [https://jp.mathworks.com/help/econ/rolling-window-estimation-of-state-space-models.html](https://jp.mathworks.com/help/econ/rolling-window-estimation-of-state-space-models.html) 49 | 50 | 51 | # 1: 年次経済データをインポート 52 | 53 | ```matlab:Code 54 | load Data_NelsonPlosser 55 | ``` 56 | 57 | 58 | 59 | ※) 当経済データは米国のものです 60 | 61 | 62 | # 2: インポートしたデータを前処理 63 | 64 | ```matlab:Code 65 | isNaN = any(ismissing(DataTable),2); % Flag periods containing NaNs 66 | Z = DataTable.GNPN(~isNaN); % Nominal GNP 67 | y = DataTable.UR(~isNaN); % Unemployment rate 68 | ``` 69 | 70 | # 3: 前処理したデータから時間付き部分時系列配列を作成 71 | 72 | 73 | Simulink の "From Workspace" ブロック用に時間付き部分時系列配列を作成します 74 | 75 | 76 | 77 | ```matlab:Code 78 | WindowSize = 31; 79 | ForecastPeriod = numel(y) - WindowSize + 1; 80 | 81 | ZZ = zeros(ForecastPeriod, WindowSize); 82 | yy = zeros(ForecastPeriod, WindowSize); 83 | 84 | m = 1; 85 | for nYear = 1:ForecastPeriod 86 | ZZ(nYear,:) = transpose(Z(m:m+WindowSize-1)); 87 | yy(nYear,:) = transpose(y(m:m+WindowSize-1)); 88 | m = m + 1; 89 | end 90 | 91 | Time = str2double(DataTable.Properties.RowNames(~isNaN)); 92 | Time = Time((end-ForecastPeriod+1:end)); 93 | 94 | % create arrays of sub time-series with time 95 | ObsUnemployRate = [Time, yy]; 96 | nGNP = [Time, ZZ]; 97 | ``` 98 | 99 | # 4: 状態空間モデルによるパラメータ推定および翌年の失業率の推定を再帰的に実施 (MATLAB編) 100 | 101 | 102 | Simulink によるシミュレーションの実行前に、モデルの係数 に対するフィッティングと翌年の失業率の推定を MATLAB 上にて再帰的に実施します 103 | 104 | 105 | 106 | ```matlab:Code 107 | eUR = zeros(numel(Time),1); 108 | param0 = [0.5; 0.1; -20]; % initialize paramters 109 | 110 | for t = 0:numel(Time)-1 111 | dlZ = diff(log(ZZ(t+1,:)))'; 112 | dy = diff(yy(t+1,:))'; 113 | Mdl = ssm(@(c)rwAR2ParamMap(c,dy,dlZ)); 114 | [Mdl, param0] = estimate(Mdl, dy, param0, 'Display', 'off'); 115 | dyhat = forecast(Mdl, 1, dy,'Predictors0',dlZ,'PredictorsF',dlZ(end),'Beta',param0(end)); 116 | eUR(t+1) = dyhat + yy(t+1,end); 117 | end 118 | ``` 119 | 120 | # 5: 実際の失業率と MATLAB にて推定された失業率をプロット 121 | 122 | ```matlab:Code 123 | figure; axH = axes; 124 | plot(axH, Time, y(end-numel(Time)+1:end), 'Color', [0.9290 0.6940 0.1250], 'LineWidth', 1.2); 125 | hold(axH, 'on'); grid(axH, 'on'); 126 | plot(axH, Time, eUR, 'Color', [0 0.4470 0.7410], 'LineWidth', 1.2); 127 | axH.XLim(1) = Time(1); 128 | axH.Color = [0.5020 0.5020 0.5020]; 129 | axH.Title.String = 'Unemployment Rate (%)'; 130 | legend(["Actual", "Forecasted"]) 131 | ``` 132 | 133 | 134 | ![figure_0.png](README_images/figure_0.png) 135 | 136 | # 6: Simulink モデルの開示とパラメータ設定 137 | 138 | 139 | ![image_0.png](README_images/image_0.png) 140 | 141 | 142 | 143 | ```matlab:Code 144 | mdl = 'recursive_ssm_update'; 145 | open_system(mdl); 146 | 147 | set_param(mdl, 'Solver', 'FixedStepAuto'); 148 | set_param(mdl, 'FixedStep', '1'); 149 | set_param(mdl, 'StartTime', num2str(Time(1))); 150 | set_param(mdl, 'StopTime', num2str(Time(end))); 151 | ``` 152 | 153 | # 7: 状態空間モデルによるパラメータ推定および翌年の失業率の推定を再帰的に実施 (Simulink編) 154 | 155 | ```matlab:Code 156 | out = sim(mdl); 157 | ``` 158 | 159 | # 8: 実際の失業率と Simulink にて推定された失業率をプロット 160 | 161 | ```matlab:Code 162 | figure; axH = axes; 163 | plot(axH, Time, out.ScopeOut.signals(1).values, 'Color', [0.9290 0.6940 0.1250], 'LineWidth', 1.2); 164 | hold(axH, 'on'); grid(axH, 'on'); 165 | plot(axH, Time, out.ScopeOut.signals(2).values, 'Color', [0 0.4470 0.7410], 'LineWidth', 1.2); 166 | axH.XLim(1) = Time(1); 167 | axH.Color = [0.5020 0.5020 0.5020]; 168 | axH.Title.String = 'Unemployment Rate (%)'; 169 | legend(["Actual", "Forecasted"]) 170 | ``` 171 | 172 | 173 | ![figure_1.png](README_images/figure_1.png) 174 | 175 | # 9: MATLAB と Simulink の一致性検証 176 | 177 | ```matlab:Code 178 | isequal(out.ScopeOut.signals(2).values, eUR) 179 | ``` 180 | 181 | 182 | ```text:Output 183 | ans = 184 | 1 185 | 186 | ``` 187 | 188 | -------------------------------------------------------------------------------- /SSM/README_images/figure_0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/SSM/README_images/figure_0.png -------------------------------------------------------------------------------- /SSM/README_images/figure_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/SSM/README_images/figure_1.png -------------------------------------------------------------------------------- /SSM/README_images/image_0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/SSM/README_images/image_0.png -------------------------------------------------------------------------------- /SSM/my_predict_fcn.m: -------------------------------------------------------------------------------- 1 | function [yhat, param0] = my_predict_fcn(y,Z,param0) 2 | 3 | Mdl = ssm(@(c)rwAR2ParamMap(c,y,Z)); 4 | [Mdl, param0] = estimate(Mdl, y, param0, 'Display', 'off'); 5 | yhat = forecast(Mdl, 1, y,'Predictors0',Z,'PredictorsF',Z(end),'Beta',param0(end)); 6 | % pt1 = currentValue + yhat; 7 | 8 | end 9 | -------------------------------------------------------------------------------- /SSM/recursive_ssm_update.slx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/SSM/recursive_ssm_update.slx -------------------------------------------------------------------------------- /SSM/rwAR2ParamMap.m: -------------------------------------------------------------------------------- 1 | function [A,B,C,D,Mean0,Cov0,StateType,deflateY] = rwAR2ParamMap(params,y,Z) 2 | 3 | % Parameter-to-matrix mapping function for rolling window example 4 | % using ssm and specifying an ARMA state model 5 | % The state space model specified by rwParamMap contains a stationary 6 | % AR(2) state, the observation model includes a regression component, and 7 | % the variances of the innovation and disturbances are 1. The response y 8 | % is deflated by the regression component specified by the predictor 9 | % variables x. 10 | 11 | A = [params(1) params(2); 1 0]; 12 | B = [1 0;0 0]; 13 | C = [1 0]; 14 | D = 1; 15 | Mean0 = []; 16 | Cov0 = []; 17 | StateType = [0 0]; 18 | deflateY = y - params(3)*Z; 19 | 20 | end -------------------------------------------------------------------------------- /SSM/time_series_forecasting.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/SSM/time_series_forecasting.mlx -------------------------------------------------------------------------------- /TimeSeriesForecasting_on_Simulink.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Time-Series-Forecasting-Simulink/fdaf4eabad1ddd943996df935e0ca24e07bd2561/TimeSeriesForecasting_on_Simulink.pdf --------------------------------------------------------------------------------