# Predict Chaotic Time-Series

This example shows how to use the command line features of `anfis` on a chaotic time-series prediction example.

Generating an FIS using the ANFIS Editor GUI is quite simple.

However, you need to be cautious about implementing the checking data validation feature of `anfis`. You must check that the checking data error does what it is supposed to. Otherwise, you need to retrain the FIS.

This example uses `anfis` to predict a time series that is generated by the following Mackey-Glass (MG) time-delay differential equation.

This time series is chaotic with no clearly defined period. The series does not converge or diverge, and the trajectory is highly sensitive to initial conditions. This benchmark problem is used in the neural network and fuzzy modeling research communities.

To obtain the time series value at integer points, the fourth-order Runge-Kutta method was used to find the numerical solution to the previous MG equation. It was assumed that , , and for . The result was saved in the file `mgdata.dat`.

Plot the MG time series.

load mgdata.dat time = mgdata(:,1); x = mgdata(:, 2); figure(1) plot(time,x) title('Mackey-Glass Chaotic Time Series') xlabel('Time (sec)')

In time-series prediction, you need to use known values of the time series up to the point in time, , to predict the value at some point in the future, . The standard method for this type of prediction is to create a mapping from sample data points, sampled every units in time, (), to a predicted future value . Following the conventional settings for predicting the MG time series, set and . For each , the input training data for `anfis` is a four-column vector of the following form.

The output training data corresponds to the trajectory prediction.

For each , ranging in values from 118 to 1117, the training input/output data is a structure whose first component is the four-dimensional input , and whose second component is the output . There are 1000 input/output data values. You use the first 500 data values for the `anfis` training (these become the training data set), while the others are used as checking data for validating the identified fuzzy model. This division of data values results in two 500-point data structures, `trnData` and `chkData`.

for t = 118:1117 Data(t-117,:) = [x(t-18) x(t-12) x(t-6) x(t) x(t+6)]; end trnData = Data(1:500,:); chkData = Data(501:end,:);

To start the training, specify an initial FIS structure using the `genfis` command. By default, `genfis` creates the FIS structure using grid partitioning.

fismat = genfis(trnData(:,1:end-1),trnData(:,end));

Because you did not specify numbers and types of membership functions used in the FIS, default values are assumed. These defaults provide two generalized bell membership functions on each of the four inputs, eight altogether. The generated FIS structure contains 16 fuzzy rules with 104 parameters. To achieve good generalization capability, it is important that the number of training data points be several times larger than the number parameters being estimated. In this case, the ratio between data and parameters is about five (500/104).

The `genfis` command generates initial membership functions that are equally spaced and cover the whole input space.

figure(2) subplot(2,2,1) plotmf(fismat,'input',1) subplot(2,2,2) plotmf(fismat,'input',2) subplot(2,2,3) plotmf(fismat,'input',3) subplot(2,2,4) plotmf(fismat,'input',4)

To configure training options, create an `anfisOptions` option set, specifying the initial FIS and validation data.

opt = anfisOptions('InitialFIS',fismat,'ValidationData',chkData);

Suppress the display of training information to the Command Window.

opt.DisplayANFISInformation = 0; opt.DisplayErrorValues = 0; opt.DisplayStepSize = 0; opt.DisplayFinalResults = 0;

Train the FIS using the specified training data and options.

[fismat1,error1,ss,fismat2,error2] = anfis(trnData,opt);

Because the `ValidationData` options is set, the final FIS you choose is the one associated with the minimum checking error. This result is stored in `fismat2`. Plots these new membership functions.

figure(3) subplot(2,2,1) plotmf(fismat2,'input',1) subplot(2,2,2) plotmf(fismat2,'input',2) subplot(2,2,3) plotmf(fismat2,'input',3) subplot(2,2,4) plotmf(fismat2,'input',4)

Plot the error signals.

figure(4) plot([error1 error2]) hold on plot([error1 error2],'o') legend('error1','error2') xlabel('Epochs') ylabel('RMSE (Root Mean Squared Error)') title('Error Curves')

In addition to these error plots, you may want to plot the FIS output versus the training or checking data. To compare the original MG time series and the fuzzy prediction side by side, try:

figure(5) anfis_output = evalfis([trnData(:,1:4); chkData(:,1:4)],fismat2); index = 125:1124; subplot(2,1,1) plot(time(index),[x(index) anfis_output]) xlabel('Time (sec)') title('MG Time Series and ANFIS Prediction') subplot(2,1,2) plot(time(index),x(index) - anfis_output) xlabel('Time (sec)') title('Prediction Errors')

The difference between the original MG time series and the values estimated using `anfis` is very small. Therefore, you can only see one curve in the first plot. The prediction error appears in the second plot with a much finer scale. You trained for only 10 epochs. If you apply more extensive training, you get better performance.