代写 R 


MAT 4376F/5313F

R – Value-at-Risk, Expected Shortfall
• Value-at-Risk
• Comparison of parametric and nonparametric method for Exponential distribution n=1000;
• lambda=2;
• p=seq(1,n,by=1)/(n+1);
• true.VAR=-log(p)/lambda;
• plot(p,true.VAR,type=”l”);
• X=rexp(n,lambda);
• hatlambda=1/mean(X);
• parametric.VAR=-log(p)/hatlambda;
• points(p,parametric.VAR,type=”l”,col=”blue”)
• nonparametric.VAR=sort(X,decreasing=TRUE);
• points(p,nonparametric.VAR,type=”p”,col=”red”);

• p0=0.05;
• -log(p0)/lambda; -log(p0)/hatlambda;
• index=floor(n*p0); nonparametric.VAR[index+1];
• 

• Comparison of parametric and nonparametric method for Pareto distribution n=1000;
• alpha=2;
• p=seq(1,n,by=1)/(n+1);
• true.VAR=p^(-1/alpha);
• plot(p,true.VAR,type=”l”);
• U=runif(n); X=(1-U)^(-1/alpha);
• hatalpha=1/mean(log(X));
• parametric.VAR=p^(-1/hatalpha);
• points(p,parametric.VAR,type=”l”,col=”blue”)
• nonparametric.VAR=sort(X,decreasing=TRUE);
• points(p,nonparametric.VAR,type=”p”,col=”red”);

• p0=0.05;
• p0^(-1/alpha); p0^(-1/hatalpha);
• index=floor(n*p0); nonparametric.VAR[index+1];
• 

• VaR for danish insurance data n=length(danish);
• p=seq(1,n,by=1)/(n+1);
• nonparametric.VAR=sort(danish,decreasing=TRUE);
• hatalpha=1/mean(log(danish));
• Pareto.method=p^(-1/hatalpha);
• Normal.method=mean(danish)+sd(danish)*qnorm(1-p);
• plot(p,Pareto.method,type=”l”);
• points(p,Normal.method,type=”l”,col=”blue”);
• points(p,nonparametric.VAR,type=”p”,col=”red”)


• 
n=length(danish);
• nonparametric.VAR=sort(danish,decreasing=TRUE);

• p0=0.05;
• index=floor(n*p0);
• nonparametric.VAR[index+1]; # nonparametric method
• hatalpha=1/mean(log(danish));
• p0^(-1/hatalpha); # Pareto method
• mean(danish)+sd(danish)*qnorm(1-p0); # normal method

• p0=0.01;
• index=floor(n*p0);
• nonparametric.VAR[index+1]; # nonparametric method
• hatalpha=1/mean(log(danish));
• p0^(-1/hatalpha); # Pareto method
• mean(danish)+sd(danish)*qnorm(1-p0); # normal method


• 

• Expected Shortfall
• Comparison of parametric and nonparametric method for Exponential distribution n=1000;
• lambda=2;
• p=seq(1,n,by=1)/(n+1);
• true.ES=(-log(p)+1)/lambda;
• plot(p,true.ES,type=”l”);
• X=rexp(n,lambda);
• hatlambda=1/mean(X);
• parametric.ES=(-log(p)+1)/hatlambda;
• points(p,parametric.ES,type=”l”,col=”blue”);
• nonparametric.VAR=sort(X,decreasing=TRUE);
• nonparametric.ES=NULL;
• for(i in 1:n)
• {
• value=sum(X[X>=nonparametric.VAR[i]])/n;
• nonparametric.ES=c(nonparametric.ES,value);
• }
• nonparametric.ES=(1/p)*nonparametric.ES;
• points(p,nonparametric.ES,type=”p”,col=”red”);


• plot(p[1:30],true.ES[1:30],type=”l”);
• points(p[1:30],parametric.ES[1:30],type=”l”,col=”blue”);
• points(p[1:30],nonparametric.ES[1:30],type=”p”,col=”red”);