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

It has been just announced that during ESSA2013 conference I am planning to organize a special track on “Statistical analysis of simulation models”. I hope to get some presentations using GNU R to promote it in social simulation community.

It is obvious that GNU R excels in analysis of simulation data. However, very often it can be neatly used to implement simulations themselves.

For instance I have recently implemented a simulation model proposed in Section 4 of *Volatility Clustering in Financial Markets: Empirical Facts and Agent–Based Models* paper by Rama Cont. The model is formulated as follows (I give only its brief description; please refer to the paper for more details).

Consider market with `n`

trading agents and one asset. We simulate market for `times`

periods. In each period each agent can buy asset, sell it or do nothing.

Asset return `r[i]`

in period `i`

equals to number of buy orders minus number of sell orders divided by number of agents `n`

and multiplied by normalizing constant `max.r`

. Thus it will always lie in the interval `[-max.r,max.r]`

.

Agents make buy and sell decisions based on random public information about an asset. The stream of signals are IID normal random variables with mean `0`

and standard deviation `signal.sd`

. Each investor holds an internal non negative decision making threshold. If signal is higher than threshold level buy decision is made. If it is lower than minus threshold level asset is sold. If signal is not strong enough investor does nothing.

After return `r[i]`

is determined each investor with probability `p.update`

performs threshold update to `abs(r[i])`

.

As you can see the description is quite lengthily. However, the implementation of the model in GNU R is a genuine snippet as can be seen below:

**<-**

**function**

**(**times, n, signal.sd, max.r ,p.update

**)**

**{**

**<-**vector

**(**“numeric”, n

**)**

**<-**rnorm

**(**times, 0, signal.sd

**)**

**<-**vector

**(**“numeric”, times

**)**

**for**

**(**i

**in**1

**:**times

**)**

**{**

**[**i

**]**

**<-**max.r

*****

**(**sum

**(**signal

**[**i

**]**

**>**threshold

**)**

**–**sum

**(**signal

**[**i

**]**

**<**

**(-**threshold

**)))**

**/**n

**[**runif

**(**n

**)**

**<**p.update

**]**

**<-**abs

**(**r

**[**i

**])**

**}**

**(**r

**)**

**}**

And an additional benefit is that one can analyze the simulation results in GNU R also. Here is a very simple example showing the relationship between signal.sd and standard deviation of simulated returns (the initial burn in period in the simulation is discarded):

**<-**

**function**

**(**signal.sd

**)**

**{**

**(**cont

**(**10000, 1000, signal.sd, 0.1, 0.05

**)[**1000

**:**10000

**])**

**}**

**<-**runif

**(**100, 0.01, 0.1

**)**

**<-**sapply

**(**sd.in, cont.sd

**)**

**(**sd.in,sd.out

**)**

**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: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...