MATLAB Examples

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.

$$\dot{x}(t) = \frac{0.2x(t-\tau)}{1+x^{10}(t-\tau)}=0.1x(t)$$

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 $x(0)= 1.2$, $\tau=17$, and $x(t)=0$ for $t<0$. The result was saved in the file mgdata.dat.

Plot the MG time series.

load mgdata.dat
time = mgdata(:,1);
x = mgdata(:, 2);
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, $t$, to predict the value at some point in the future, $t + P$. The standard method for this type of prediction is to create a mapping from $D$ sample data points, sampled every $\Delta$ units in time, ($x(t - (D - 1)\Delta ), \ldots ,x(t - \Delta ),x(t)$), to a predicted future value $x = (t + P)$. Following the conventional settings for predicting the MG time series, set $D = 4$ and $\Delta  = P = 6$. For each $t$, the input training data for anfis is a four-column vector of the following form.

$$w(t) = [x(t-19), x(t-12), x(t-6), x(t)]$$

The output training data corresponds to the trajectory prediction.

$$s(t) =  x(t+6)$$

For each $t$, ranging in values from 118 to 1117, the training input/output data is a structure whose first component is the four-dimensional input $w$, and whose second component is the output $s$. 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)];
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.


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.


Plot the error signals.

plot([error1 error2])
hold on
plot([error1 error2],'o')
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:

anfis_output = evalfis([trnData(:,1:4); chkData(:,1:4)],fismat2);
index = 125:1124;
plot(time(index),[x(index) anfis_output])
xlabel('Time (sec)')
title('MG Time Series and ANFIS Prediction')
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.