################################################################ ## Initialization ################################################################ # context cleanup rm(list=ls()) # external libraries loading library(survival) library(e1071) library(tseries) library(fBasics) library(fSeries) library(nlme) ################################################################ ## Data Loading ################################################################ # load prices from file and compute returns table<-read.csv("bulle_100fundamentalist_0speculators_12345678_macro.dat", header=FALSE, sep=" ", dec=".") prices<-table$V2 date<-table$V1 returns<-diff(log(prices)) normalized_returns<-(returns-mean(returns))/sd(returns) day<-date # load world from file (optional) table_world<-read.csv("bulle_100fundamentalist_0speculators_12345678_world.dat", header=FALSE, sep=" ", dec=".") prices_world<-table_world$V2 date_world<-table_world$V1 prices_normalized<-0 prices_world_normalized<-0 k<-1 for(i in 1:length(prices)) { for(j in 1:length(prices_world)) { if(date[i] == date_world[j]) { prices_normalized[k]<-prices[i] prices_world_normalized[k]<-prices_world[j] k<-k+1 break } } } # compute a fake price series taken from a normal with same mean and deviation than 'prices' serie fake_prices<-0 fake_prices[1]<-prices[1] fake_returns<-0 fake_returns<-rnorm((length(prices)-1),mean(diff(log(prices))), sd(diff(log(prices)))) for(i in 2:(length(prices)-1)) fake_prices[i]<-fake_prices[i-1]*(1+fake_returns[i]) ################################################################ ## General ################################################################ # prices and returns postscript(file="bulle_100fundamentalist_0speculators_12345678_macro_prices_returns.eps", horizontal=FALSE, onefile=FALSE) par(cex=2.3) m <- matrix(1:2, 2, 1) layout(m, widths=c(1, 1),heights=c(3,2)) plot(day, prices, type="l", xlab="date", ylab="prices") lines(date_world, prices_world, type="l", col="red") grid(10,10) plot(day[1:length(day)-1] ,diff(log(prices)), type="l", xlab="date", ylab="returns", ylim=c(min(returns), max(returns))) grid(10,10) dev.off() ################################################################ ## Returns distribution shape ################################################################ # compare empirical distribution with a normal of same mean and variance postscript(file="bulle_100fundamentalist_0speculators_12345678_macro_distribution.eps", horizontal=FALSE, onefile=FALSE, paper='special', width=10, height=10) par(cex=2.3) hist(diff(log(prices)), nclass=200, freq=FALSE,xlab="return", ylab="frequency", main="") lines(density(fake_returns),col = "red", lwd=3) dev.off() # empirical vs theoretical qqplot postscript(file="bulle_100fundamentalist_0speculators_12345678_macro_qqplot.eps", horizontal=FALSE, onefile=FALSE, paper='special', width=10, height=10) #layout(1) par(cex=2.3) qqnorm(diff(log(prices)), main="", xlab="theoretical quantiles", ylab="empirical quantiles") grid(10,10) qqline(fake_returns, col="red", lwd=3, cex=3) dev.off() # computes kurtosis of returns cat("Kurtosis :\n", kurtosis(returns), "\n\n", sep="", file="bulle_100fundamentalist_0speculators_12345678_macro_numericalTests.txt", append=FALSE) ################################################################ ## Survival function ################################################################ #layout(1) #postscript(file="experiments/bulle_100fundamentalists_0speculators//analysis//images/bulle_100fundamentalist_0speculators_12345678_macro_survival.eps", horizontal=FALSE, onefile=FALSE, paper='special', width=10, height=10) #pos=0;neg=0;m=1; n=0 #for (i in 1:NROW(normalized_returns)) {{if (returns[i]>0) {pos[m]=returns[i];m=m+1}}; {if (returns[i]<0) {neg[m]=returns[i];m=m+1}}} #pos=na.omit(pos);neg=na.omit(neg) #npos=(pos-mean(pos))/sd(pos) #nneg=(abs(neg)-mean(abs(neg)))/sd(abs(neg)) #n=rnorm(NROW(npos), mean(npos),sd(npos)) #n=na.omit(n) #x <- Surv(abs(n)) #plot(survfit(x),xlim=c(0.0001,100),log="xy",main="", ylim=c(0.00001, 1), xlab="Absolute Returns", ylab="Probability >= x") #legend(0.0002,0.0004,"Negative Returns",col="blue",lty=1:7,bty="n") #legend(0.0002,0.0002,"Positive Returns",col="red",lty=1:7,bty="n") #legend(0.0002,0.0001,"Positive Returns Fitted Gaussian",col="black",lty=1:7,bty="n") #legend(0.0002,0.00005,"Negative Returns Fitted Gaussian",col="green",lty=1:7,bty="n") #x <- Surv(abs(nneg)) #lines(survfit(x),xlim=c(0.0001,100),col="blue",lwd=0.5) #x <- Surv(abs(npos)) #lines(survfit(x),xlim=c(0.0001,100),col="red",lwd=0.5) #n=rnorm(NROW(npos), mean(nneg),sd(nneg)) #x <- Surv(abs(n)) #lines(survfit(x),xlim=c(0.0001,100),col="green",lwd=0.5) #dev.off() ################################################################ ## Returns dependency ################################################################ # auto correlation function of returns postscript(file="bulle_100fundamentalist_0speculators_12345678_macro_ACF.eps", horizontal=FALSE, onefile=FALSE, paper='special', width=10, height=10) par(cex=2.3, lwd=3) acf<-acf(returns) plot(acf, type='l', main='', lwd=3) dev.off() # auto correlation function of absolute returns postscript(file="bulle_100fundamentalist_0speculators_12345678_macro_ACFabs.eps", horizontal=FALSE, onefile=FALSE, paper='special', width=10, height=10) par(cex=2.3, lwd=3) acf<-acf(abs(returns)) plot(acf, type='l', main='', lwd=3) dev.off() # Jarque-Bera test capture.output(jarque.bera.test(returns), file="bulle_100fundamentalist_0speculators_12345678_macro_numericalTests.txt", append=TRUE) cat("\n", sep="", file="bulle_100fundamentalist_0speculators_12345678_macro_numericalTests.txt", append=TRUE) # Augmented-Dickey Fuller capture.output(adf.test(returns), file="bulle_100fundamentalist_0speculators_12345678_macro_numericalTests.txt", append=TRUE) cat("\n", sep="", file="bulle_100fundamentalist_0speculators_12345678_macro_numericalTests.txt", append=TRUE) # BDS capture.output(bds.test(returns), file="bulle_100fundamentalist_0speculators_12345678_macro_numericalTests.txt", append=TRUE) cat("\n", sep="", file="bulle_100fundamentalist_0speculators_12345678_macro_numericalTests.txt", append=TRUE) # Box test capture.output(Box.test(returns), file="bulle_100fundamentalist_0speculators_12345678_macro_numericalTests.txt", append=TRUE) cat("\n", sep="", file="bulle_100fundamentalist_0speculators_12345678_macro_numericalTests.txt", append=TRUE) # Hurst exponen capture.output(rsFit(returns), file="bulle_100fundamentalist_0speculators_12345678_macro_numericalTests.txt", append=TRUE) cat("\n", sep="", file="bulle_100fundamentalist_0speculators_12345678_macro_numericalTests.txt", append=TRUE) # Correlation between world (fundamental value) and prices capture.output(correlationTest(prices_normalized, prices_world_normalized), file="bulle_100fundamentalist_0speculators_12345678_macro_numericalTests.txt", append=TRUE) cat("\n", sep="", file="bulle_100fundamentalist_0speculators_12345678_macro_numericalTests.txt", append=TRUE)