### Smoothing Class. ### Attach the dataset attach(FinMark) par(mfrow=c(1,1)) ### Plot 1. Monthly returns. plot(Time, VW.Return,main="Monthly returns") ### Plot 2. Monthly returns squared. plot(Time, VW.Return.Sq, main="Monthly returns squared") ### Plot 3. log Monthly returns squared. eps <- 0.00000001 plot(Time, log(VW.Return.Sq + eps),main="Log of the squared returns") ### Plot 4. The spline smooth to log Monthly returns squared. plot(Time, log(VW.Return.Sq + eps),main="Log of the squared returns with spline smooth") par(col=2,lwd=4) lines(smooth.spline(Time, log(VW.Return.Sq + eps))) par(col=1,lwd=1) ### Plot 5. A variety of smooths. par(mfrow=c(3,3)) for(i in 1:9) { plot(smooth.spline(Time, log(VW.Return.Sq + eps),df=2^i),xlab="Date", ylab="Volatility",type="n",main=paste("Spline smooth with e.d.f. = ",2^i)) lines(smooth.spline(Time, log(VW.Return.Sq + eps),df=2^i)) } par(mfrow=c(1,1)) ### Make a function to do Tukey's smoother. t.smooth <- function(x,w=1) { y <- x option.width <- w for(i in (1+w):(length(x) - w)) { y[i] <- median(c(x[(i-w):(i+w)])) } if(sum((y-x)^2) < 0.00000001){return(y);break()} t.smooth(y, option.width) } par(col=1,lwd=1) par(mfrow=c(1,1)) ### Plot 6, Tukey's smooth. plot(Time,t.smooth(log(VW.Return.Sq + eps),w=1),main="Tukey's smooth,w=1") par(col=2,lwd=2) lines(Time,t.smooth(log(VW.Return.Sq + eps),w=1)) ### Plot 7. Another Tukey smooth. par(col=1,lwd=1) plot(Time,t.smooth(log(VW.Return.Sq + eps),w=10),main="Tukey's smooth,w=10") par(col=2,lwd=2) lines(Time,t.smooth(log(VW.Return.Sq + eps),w=10)) par(col=1,lwd=1) ### Another version of Tukey's smooth built into S-Plus ### Graph 8 plot(Time,smooth(log(VW.Return.Sq + eps)),main="S-Plus Tukey smooth") par(col=2,lwd=2) lines(Time,smooth(log(VW.Return.Sq + eps))) par(col=1,lwd=1) ### Graph 9. Lowess smoothing. (Robust local linear fits) plot(Time,log(VW.Return.Sq + eps),main="Lowess smooth, default") par(col=2,lwd=4) lines(lowess(Time,log(VW.Return.Sq + eps))) par(col=1,lwd=1) ### Plot 10. A variety of lowess smooths. par(mfrow=c(3,3)) for(i in 0:8) { plot(lowess(Time, log(VW.Return.Sq + eps),f=1/(2^i)),xlab="Date", ylab="Volatility",main=paste("Lowess smooth, span = 1/",(2^i)),type="n") lines(lowess(Time, log(VW.Return.Sq + eps),f=1/(2^i))) } par(mfrow=c(1,1)) ### Plot 11. Finally the super-smoother. Locally adaptive band-width plot(Time,log(VW.Return.Sq + eps),main="Super smooth, default") par(col=2,lwd=4) lines(supsmu(Time,log(VW.Return.Sq + eps))) par(col=1,lwd=1) ### Plot 12. A variety of supsmu smooths. par(mfrow=c(3,3)) for(i in 0:8) { plot(supsmu(Time, log(VW.Return.Sq + eps),span=1/(2^i)),xlab="Date", ylab="Volatility",main=paste("Super smooth, span=1/",(2^i)),type="n") lines(supsmu(Time, log(VW.Return.Sq + eps),span=1/(2^i))) }