**R snippets**, and kindly contributed to R-bloggers)

During ESSA2013 conference I had a discussion about Cont model I have commented a year ago.

In original paper Cont highlights that his model produces distribution of returns characterized by positive excess kurtosis. In this post I want to investigate this assertion.

Cont simulation has three key parameters (check out the paper for details):

- d: standard deviation of new public information incoming to the investors
- l: market depth (this implies that maximum percentage price change is in range [-1/l,1/l])
- s: percentage of investors updating their decision threshold

We want to simulate the model with 1000 agents for 11000 periods (dropping first 1000 as warm up) and calculate kurtosis of observed returns distribution. The selected ranges of d, l and s parameters follow Cont recommendations. Here is the code that generates the data:

**(**e1071

**)**

**(**mgcv

**)**

**<-**

**function**

**(**burn.in, reps, n, d, l ,s

**)**

**{**

**<-**rep

**(**0, n

**)**

**<-**rnorm

**(**reps, 0, d

**)**

**<-**rep

**(**0, reps

**)**

**for**

**(**i

**in**1

**:**reps

**)**

**{**

**[**i

**]**

**<-**

**(**sum

**(**sig

**[**i

**]**

**>**tr

**)**

**–**sum

**(**sig

**[**i

**]**

**<**

**(-**tr

**)))**

**/**

**(**l

*****n

**)**

**[**runif

**(**n

**)**

**<**s

**]**

**<-**abs

**(**r

**[**i

**])**

**}**

**(**r

**[**burn.in

**:**reps

**])**

**}**

**<-**60000

**<-**runif

**(**sim.points,0.001,0.01

**)**

**<-**runif

**(**sim.points,5,20

**)**

**<-**runif

**(**sim.points,0.01,0.1

**)**

**<-**mapply

**(**

**function**

**(**d, l, s

**)**

**{**

**(**cont.run

**(**1000, 10000, 1000, d, l ,s

**))**

**}**, d, l, s**)**

Warning! The simulation takes a lot of time so I have uploaded here the file sim_output.txt containing the results. It contains four columns: d, l, s, k, where last column holds calculated kurtosis.

Now let us turn to the code that allows us to visually explore the data. I have saved the generated plots to GIF file using animation package. They show the probability of encountering excess kurtosis in generated data conditional on d, l and s.

**(**ggplot2

**)**

**<-**read.table

**(**“sim_output.txt”, head

**=**T,

**=**rep

**(**“numeric”, 4

**))**

**$**cd

**<-**cut

**(**data.set

**$**d, seq

**(**0.001, 0.01, len

**=**10

**))**

**$**cl

**<-**cut

**(**data.set

**$**l, seq

**(**5, 20, len

**=**16

**))**

**$**cs

**<-**cut

**(**data.set

**$**s, seq

**(**0.01, 0.1, len

**=**10

**))**

**$**p.excess

**<-**as.numeric

**(**data.set

**$**k

**>**0

**)**

**<-**aggregate

**(**p.excess

**~**cd

**+**cl

**+**cs, data

**=**data.set, mean

**)**

**for**

**(**i

**in**levels

**(**sum.data

**$**cs

**)[**c

**(**1

**:**9, 8

**:**2

**)])**

**{**

**(**ggplot

**()**

**+**

**(**data

**=**sum.data

**[**sum.data

**$**cs

**==**i,

**]**,

**(**x

**=**cl, y

**=**cd, colour

**=**p.excess

**)**,

**=**15, size

**=**10

**)**

**+**

**(**low

**=**“blue”, high

**=**“red”

**)**

**+**

**(**panel.background

**=**element_blank

**()**,

**=**element_blank

**()**,

**=**element_blank

**()**,

**=**element_text

**(**colour

**=**“black”, size

**=**14

**)**,

**=**element_text

**(**angle

**=**

**–**90

**))**

**+**

**(**paste

**(**“cs:”, i

**)))**

**}**

And here is the result (d is on y-axis and l on x-axis):

We can see that s is the least important parameter and there is a non linear interaction between d and l. But most importantly – in the given range parameters excess kurtosis is not guaranteed to appear. In particular it does not happen when both d and l are large.

**leave a comment**for the author, please follow the link and comment on their blog:

**R snippets**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...