## Modelling Chaotic Systems using Support Vector Regression and Python

Similar to Neural Networks, Support Vector Machines (SVM) are powerful modelling techniques in machine learning that can be applied to a variety of tasks. SVMs are primarily used to classify data while one of its variants, Support Vector Regression (SVR), can be used for time series analysis. In this post, we will perform SVR on chaotic time series using common Python libraries and a simple wrapper program.

The following libraries will be required to use the script I will demonstrate using Python 2.7:

1. Numpy
2. Scipy
3. Scikits.Learn (a toolbox that hosts a variety of machine learning algorithms and requires Numpy and Scipy)
4. MatPlotLib (used for graphing and visualization)
5. Other libraries such as SetupTools may be required depending on your system

You can download these from their websites or if you are using Windows “Unofficial Windows Binaries for Python Extension Packages

With the packages installed, we can begin to build a program that fits an SVR model to chaotic data. First, we define a time series. In this case, I will use the Delayed Henon Map with a delay of 4, for more information please see my posts on that system.

The model will be embedded in a d dimension space.

def TimeSeries(cmax, transient, d):
return DelayedHenon(cmax, transient, d)
def DelayedHenon(cmax, transient, d):
temp = []
rF = [] # Features
rT = [] # Targets
dX = []
i = 0
while i < d:
dX.append(0.1)
i = i + 1
while i <= cmax + transient:
x = 1 - 1.6 * dX[i-1] ** 2 + 0.1 * dX[i-4]
if i > transient-d:
temp.append(x)
if i > transient:
rT.append(x)
dX.append(x)
i = i + 1
rF = SlidingWindow(temp, d)
return {"F":rF, "T":rT}

I added a function that formats the data properly so it can be used by the SVR Fit function:

def SlidingWindow(x, d):
i = d
y = []
while i < len(x):
temp = []
j = 1
while j <= d:
temp.append(x[i - d + j])
j = j + 1
y.append(temp)
i = i + 1
return y

We then define the model, note that SVR has a few user defined parameters which can be chosen to fit the data better.

  clf = svm.SVR(kernel='rbf', degree=d, tol=tolerance, gamma=gam, C=c, epsilon=eps)

In this case, you can choose the following parameters to produce a model with an error near 0.0035 for 1024 points taken from the time series. In practice, you can create a training algorithm to find these values.

  gam = 0.117835847192
c = 1.02352954164
eps = 0.00163811344978
tolerance = 0.00539604526663
d = 5
cmax = 1024

Finally, we create functions to fit the time series and plot the results:

def Fit(cmax, d, tolerance, gam, c, eps):
data = TimeSeries(cmax, random.randint(0,10000), d)
test = TimeSeries(cmax, random.randint(0,10000), d)

x = data["F"]
y = data["T"]

sout = test["T"]

clf = svm.SVR(kernel='rbf', degree=d, tol=tolerance, gamma=gam, C=c, epsilon=eps)

try:
m = clf.fit(x, y)
except:
return [1000, clf, sout, sout]

mout = clf.predict(test["F"])

# Determine the error for the system
err = 0
i = 0
while i < len(mout):
err += (mout[i] - sout[i])**2
i = i + 1
err = math.sqrt(err / (len(mout)+.0))

return [err, clf, mout, sout]

def Plotter(err, mout, sout, SaveGraph):
plt.title("SVR: Err=" + str(err))
plt.scatter(sout[0:len(sout)-2], sout[1:len(sout)-1], s=1, marker='o', edgecolors='none')
plt.scatter(mout[0:len(mout)-2], mout[1:len(mout)-1], s=1, marker='o', edgecolors='none', facecolor='r')

plt.show()
  if SaveGraph:
try:
plt.savefig("svr")
except:
# Do Nothing
SaveGraph = SaveGraph
plt.clf()

Here is the final program after putting all of the functions together (and moving some stuff around) with example output:

#!/usr/bin/env python
from scikits.learn import svm
import random
import math
import matplotlib.pyplot as plt

d = 5       # Degree of the SVR model
cmax = 1024 # Number of points to run

def TimeSeries(cmax, transient, d):
return DelayedHenon(cmax, transient, d)

def main():
global cmax, d, perturbation
gam = 0.117835847192
c = 1.02352954164
eps = 0.00163811344978
tolerance = 0.00539604526663
# Create and Fit a model to data, we just need to create support vectors
[err, svr, mout, sout] = Fit(cmax, d, tolerance, gam, c, eps)
Plotter(err, mout, sout, False)
print "error: ", err
def DelayedHenon(cmax, transient, d):
temp = []
rF = [] # Features
rT = [] # Targets
dX = []
i = 0
while i < d:
dX.append(0.1)
i = i + 1
while i <= cmax + transient:
x = 1 - 1.6 * dX[i-1] ** 2 + 0.1 * dX[i-4]
if i > transient-d:
temp.append(x)
if i > transient:
rT.append(x)
dX.append(x)
i = i + 1
rF = SlidingWindow(temp, d)
return {"F":rF, "T":rT}
def Fit(cmax, d, tolerance, gam, c, eps):
data = TimeSeries(cmax, random.randint(0,10000), d)
test = TimeSeries(cmax, random.randint(0,10000), d)

x = data["F"]
y = data["T"]

sout = test["T"]

clf = svm.SVR(kernel='rbf', degree=d, tol=tolerance, gamma=gam, C=c, epsilon=eps)

try:
m = clf.fit(x, y)
except:
return [1000, clf, sout, sout]

mout = clf.predict(test["F"])

# Determine the error for the system
err = 0
i = 0
while i < len(mout):
err += (mout[i] - sout[i])**2
i = i + 1
err = math.sqrt(err / (len(mout)+.0))

return [err, clf, mout, sout]

def Plotter(err, mout, sout, SaveGraph):
plt.title("Delayed Henon Map and SVR Model")
p1 = plt.scatter(sout[0:len(sout)-2], sout[1:len(sout)-1], s=1, marker='o', edgecolors='none')
p2 = plt.scatter(mout[0:len(mout)-2], mout[1:len(mout)-1], s=1, marker='o', edgecolors='none', facecolor='r')
plt.legend([p2, p1], ["Model (Err="+ str(round(err,4))+")", "Map"], loc=8)

plt.draw()
plt.show()
if SaveGraph:
try:
plt.savefig("svr")
except:
# Do Nothing
SaveGraph = SaveGraph
plt.clf()

def SlidingWindow(x, d):
i = d
y = []
while i < len(x):
temp = []
j = 1
while j <= d:
temp.append(x[i - d + j])
j = j + 1
y.append(temp)
i = i + 1
return y

def WriteFile(err, BestTol, BestGam, BestC, BestEps, s):
# Write the model, sensitivities to a file
f = open("sensitivities", "w")
f.write("Model Parameters:\n")
f.write("d=" + str(d) + "\n")
f.write("gam=" + str(BestGam) + "\n")
f.write("c=" + str(BestC) + "\n")
f.write("eps=" + str(BestEps) + "\n")
f.write("tolerance=" + str(BestTol) + "\n")
f.write("Model Data:\n")
f.write("err=" + str(err) + "\n")
f.write("s=" + str(s) + "\n")
f.close()
main()

Example Output:

err:  0.00381722720161

To fit data taken from other systems, simply change the Timeseries function.

## Modelling Sensitivity using Neural Networks

Artificial neural networks can be applied to the delayed Henon map[1] and shown to replicate the sensitivities[2] of the map surprisingly well. Models such as neural networks have a rich history with numerous resources available that describe there use in tasks that range from automated driving to medical diagnosis.

The network I will describe is much simpler and only estimates the sensitivities of the delayed Henon map. This network is a single-layer feedforward network that is optimized on next-step prediction. The network, shown below, involves a layer of inputs that connect to a single layer of hidden nodes with some weight . The weighted inputs are then transformed by an activation function, in this case a hyperbolic tangent, within each node and the output is the sum of these hidden node values weighted by . The network shown schematically,

The weights,  and are an n d matrix and n x 1 vector of real numbers, respectively. The 1 is a bias term that shifts neuron’s value around without being tied to an input. The neural network can be represented by,

where d is the embedding dimension or number of inputs in the network and n is the number of neurons in the hidden layer. The network is trained on input data  by altering the weights to better fit the target data . For the delayed Henon map, we feed d sequential points from the time series into the network and associate the target as the next point in the time series. The network is trained to fit that next point.

There are numerous network topologies, training methods, and error functions that one can use. One method we use is similar to simulated annealing and hill climbing. In this case, we search a neighborhood of potential solutions with the chance to randomly search a more distant one. If a good solution is found, we move to its neighborhood and start searching again. We slowly shrink the neighborhood size as training progresses to help home in on a good solution. A good solution is one that minimizes the average one-step mean-square distance between predictions from the neural network and the actual data ,

Since the neural network model equations are known, we can easily analyze the sensitivity of a network trained on experimental data such as the delayed Henon map. Following the same procedure detailed in Delayed Henon Map Sensitivities, we can take the partial derivative of the function with respect to each of the inputs j,

We could also use a numerical partial derivative instead by perturbing each input one by one and averaging the change in output through the time series.

After training one neural network, with 4 neurons and 5 dimensions, on 512 points from the delayed Henon map with a delay set to four, the following training error, e=8.4 x 10-6, and sensitivities were found,

S(1) = 1.8762
S(2) = 0.0020
S(3)= 0.0017
S(4)=0.1025
S(5)=0.0004

where S(j) represents the sensitivities for time lag j. The delayed Henon map with a delay of four has the following sensitivities,

S(1) = 1.8980
S(4) = 0.1

The neural network estimates the sensitivities fairly well and one could probably train longer to obtain more accurate sensitivities.

One question we asked in this blog series concerned the inverted delayed Henon map. There are two approaches that we could take to find the sensitivities of the inverted map, (1) invert the neural network trained on the righted map or (2) train a network on the inverted map data.

It is not easy to invert a neural network though there are many papers about training a network on data in a way similar to the original training method. Ref #3 highlights how one would do this by training the network on inputs using gradient descent. However, we can find the sensitivities of the inverted delayed Henon map by simply training on data taken from the righted map and reversing the entire time series. After training a network with the same number of neurons and dimensions as described above, we arrive at (e=0.001572),

S(1)=0.1525
S(2)=0.07741
S(3)=17.1847
S(4)=8.6787
S(5)=0.0237

The inverted delayed Henon map has the following sensitivities calculated here,

S(3)=18.9795
S(4)=10

As we see from the difference in the righted and inverted trained networks, sensitivity accuracy varies. One idea is that the accuracy of the training error is correlated to the error in the sensitivities but I am unaware of any literature exploring this.

With this training method, we do not know when a network is optimized so there is a trade off in accuracy and time spent training the neural network. This particular example trained for several hours. If time is an issue, you could easily trade out the neural network with another model such as Support Vector Regression.

Once the model has been optimized on the data, you could take the partial derivatives of the finalized equation with respect to each of the inputs or perform a numerical partial derivative. In this case, it is also important to calculate the same perturbation in each of the time lags of the original system. For the delayed Henon map with 512 points, this changes the sensitivities to 1.90594 for the first delay and .10000 for the d-th delay.

If you try other models, I would like to hear about it.

A neural network model and simple mathematical systems such as the delayed Henon map help us approach complex systems such as the weather, politics, or economics. We can explore simple systems like the delayed Henon map and look at interesting properties that these systems possess such as the sensitivity of the output to each of the inputs. Aside from this property, we could analyze trained neural networks to see if they replicate the Kaplan-Yorke dimension, fractal dimension, or many others. Creating algorithms that estimate these properties fairly well on simple systems may help us to understand more complex phenomena in the future.

References:

1. Sprott JC. High-dimensional dynamics in the delayed Hénon map. Electron J Theory Phys 2006; 3:19–35.
2. Maus A. and Sprott JC. Neural network method for determining embedding dimension of a time series. Comm Nonlinear Science and Numerical Sim 2011; 16:3294-3302.
3. Dau A. Inversion of Neural Networks. 2000; http://web.mit.edu/profit/PDFS/DuaA.pdf

This post is part of a series:

1. Delayed Henon Map Sensitivities
2. Inverted Delayed Henon Map
3. Modeling Sensitivity using Neural Networks

## Inverted Delayed Henon Map

The delayed Henon map,

offers insight into the high-dimensional dynamics through its adjustable d parameter. In the previous post, Delayed Henon Map Sensitivities, we looked at the sensitivity of the output to perturbations in each of the time lags of this map using partial derivatives. Since this function is known, it is simple to determine the lag space as well as the embedding dimension for the system. However, as we will see later, we can use this method to find the lag space and embedding dimension for unknown systems using artificial neural networks. Some interesting questions arise when you analyze neural networks trained on the delayed Henon map and its inverted counterpart so we will first look at the inverted delayed Henon map.

The delayed henon map can be inverted quite easily by separating the time delayed form into two equations such as the following,

After a little algebra, you get

Finally, we replace  with to obtain the inverted map,

After inversion, the map becomes a repellor and any initial condition on the original attractor will wander off to infinity. Since we cannot directly estimate the sensitivities from this function we can calculate a time series from the righted delayed Henon map and feed that data into the partial derivative equations of the inverted delayed Henon map. Using this process on 10,000 points from the righted delayed Henon map (the first 1,000 points removed) we obtain the following sensitivities,

Here are a few other maps, their sensitivities, their inverted equations, and those sensitivities:

Original Henon map [1]

Equation

Sensitivities

Inverted Equation

Inverted Sensitivities

Discrete map from preface of Ref. #2

Equation

Sensitivities

Inverted Equation

Inverted Sensitivities

References:

1. Henon M. A two-dimensional mapping with a strange attractor. Commun Math Phys 1976;50:69–77.
2. Sprott JC. Chaos and time-series analysis. New York: Oxford; 2003.

This post is part of a series:

1. Delayed Henon Map Sensitivities
2. Inverted Delayed Henon Map
3. Modeling Sensitivity using Neural Networks

## Delayed Henon Map Sensitivities

What previous days’ weather affected today’s weather? Historically, which states’ votes affected the outcome of different presidential elections? How does a single trade affect the price of a stock?

Modelling the weather, politics, and economics would be a very difficult task but we can explore questions like this in less complex mathematical systems such  as the delayed Henon map [1]. The delayed Henon map is a time-delayed system represented by following equation,

So, how are the three questions posed above tied together? They all attempt to answer a common question. What is the sensitivity of each time lag in a function of the system [2]?

For the delayed Henon map, this would be akin to asking, how does affect . We can infer this by taking the partial derivative of with respect to each time lag. Using a partial derivative is like asking, if I vary just slightly, how will change. For the obviously non-zero time lags that would be,

To accurately determine how much the output of the function varies when each time lag is perturbed, we need to find the mean of the absolute values of the partial derivatives around the attractor. Thus we have,

Since the delayed Henon map has a chaotic attractor and the values of vary, you can estimate the value of the sensitivities for 10,000 iterations of the time series. Initializing the map with a vector such as [.1, .1, .1, .1], we get the following strange attractor (with the first 1,000 iterations removed),

Delayed Henon Map with 9,000 points (d=4)

We arrive at the following sensitivities for d=4,

By estimating a system’s sensitivities, we can determine what is known as the lag space [3]. Dimensions with non-zero sensitivities make up this space and the largest dimension with a non-zero sensitivity also determines the embedding dimension. As we will see in another post, a neural network method can be devised to find the lag space of various time series.

References:

1. Sprott JC. High-dimensional dynamics in the delayed Hénon map. Electron J Theory Phys 2006; 3:19–35.
2. Maus A. and Sprott JC. Neural network method for determining embedding dimension of a time series. Comm Nonlinear Science and Numerical Sim 2011; 16:3294-3302.
3. Goutte C. Lag space estimation in time series modelling. In: Werner B, editor. IEEE International Conference on Acoustics, Speech, and SignalProcessing, Munich, 1997, p. 3313.

This post is part of a series:

1. Delayed Henon Map Sensitivities
2. Inverted Delayed Henon Map
3. Modeling Sensitivity using Neural Networks