[Returnanalytics-commits] r2345 - pkg/Meucci/demo
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Jun 17 19:12:41 CEST 2013
Author: xavierv
Date: 2013-06-17 19:12:41 +0200 (Mon, 17 Jun 2013)
New Revision: 2345
Added:
pkg/Meucci/demo/S_LognormalSample.R
pkg/Meucci/demo/S_NonAnalytical.R
pkg/Meucci/demo/S_NormalSample.R
pkg/Meucci/demo/S_StudentTSample.R
Log:
-added CH1 scripts from Meucci's book as demo
Added: pkg/Meucci/demo/S_LognormalSample.R
===================================================================
--- pkg/Meucci/demo/S_LognormalSample.R (rev 0)
+++ pkg/Meucci/demo/S_LognormalSample.R 2013-06-17 17:12:41 UTC (rev 2345)
@@ -0,0 +1,57 @@
+#' This script simulate univariate lognormal variables, as described in
+#' A. Meucci, "Risk and Asset Allocation", Springer, 2005, Chapter 1.
+#'
+#' @references
+#' \url{http://}
+#' See Meucci's script for "S_LognormalSample.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @export
+
+
+
+##################################################################################################################
+### Input parameters
+
+nSim = 10000;
+ExpX = 3;
+VarX = 5;
+
+##################################################################################################################
+### Generate lognormal sample with above parameters
+
+LP = LognormalMoments2Parameters( ExpX, VarX );
+sigma = sqrt( LP$sigma_square );
+
+X = rlnorm( nSim, LP$mu, sigma );
+
+##################################################################################################################
+### Plots
+
+# plot over time
+plot( X, main = "lognormal sample vs observation time" );
+
+# plot histogram
+NumBins = round( 10 * log( nSim ) );
+hist( X, NumBins, main = "histogram of lognormal sample" );
+
+# plot empirical cdf
+f = ecdf( X );
+plot( f, col = "red", main = "cdf of lognormal distribution" );
+
+# plot exact cdf
+F = plnorm( 1:10000, LP$mu, sigma );
+lines ( 1:10000, F, col = "blue" );
+legend( "bottomright", 1.9, c("empirical", "exact"), col = c("red", "blue"), lty = 1, bg = "gray90" );
+
+
+##################################################################################################################
+# plot empirical quantile
+u= seq( 0.01, 0.99, 0.01 ); # range of quantiles (values between zero and one)
+q = quantile( X, u );
+plot( u, q, type = "l", xlab="Grade", ylab="Quantile", lty = 1, col = "red", main = "quantile of lognormal distribution" );
+
+# plot exact quantile
+Q = qlnorm( u, LP$mu, sigma );
+lines( u, Q, type = "l", lty = 1, col = "blue" );
+legend( "bottomright", 1.9, c( "empirical", "exact" ), col = c( "red", "blue" ), lty = 1, bg = "gray90" );
Added: pkg/Meucci/demo/S_NonAnalytical.R
===================================================================
--- pkg/Meucci/demo/S_NonAnalytical.R (rev 0)
+++ pkg/Meucci/demo/S_NonAnalytical.R 2013-06-17 17:12:41 UTC (rev 2345)
@@ -0,0 +1,54 @@
+#' This script generates draws for the sum of random variables, as described in
+#' A. Meucci, "Risk and Asset Allocation", Springer, 2005, Chapter 1.
+#'
+#' @references
+#' \url{http://}
+#' See Meucci's script for "S_NonAnalytical.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @export
+
+##################################################################################################################
+### Input parameters
+
+nSim = 10000;
+mu_St = 0;
+s2_St = 0.1;
+nu_St = 8; # NOTE: see how the final results change if you increase nu (=4 is enough)
+
+mu_LN = 0.1;
+s2_LN = 0.2;
+
+##################################################################################################################
+### Generate draws
+
+# generate Student sample with above parameters
+s_St = sqrt( s2_St );
+X = mu_St + s_St * rt( nSim, nu_St );
+
+# generate lognormal sample with above parameters
+s_LN = sqrt( s2_LN );
+Y = rlnorm( nSim, mu_LN, s_LN );
+
+# sum samples
+Z = X + Y;
+
+##################################################################################################################
+### Plot the sample Z
+plot( Z, xlab="simulations", ylab="Z", main = "sample vs observation time" );
+
+##################################################################################################################
+### Plot the histogram of Z
+NumBins = round( 10 * log( nSim ) );
+hist( Z, NumBins, xlab="Z", main="sample histogram" );
+
+##################################################################################################################
+### Plot the empirical cdf of Z
+f = ecdf( Z );
+plot( f, xlab="Z", main="empirical cdf" );
+
+##################################################################################################################
+### Plot the empirical quantile of Z
+u= seq( 0.01, 0.99, 0.01 ); # range of quantiles (values between zero and one)
+q = quantile( Z, u );
+plot( u, q, type = "l", xlab="Grade", ylab="Quantile", lty = 1, main = "empirical quantile" );
Added: pkg/Meucci/demo/S_NormalSample.R
===================================================================
--- pkg/Meucci/demo/S_NormalSample.R (rev 0)
+++ pkg/Meucci/demo/S_NormalSample.R 2013-06-17 17:12:41 UTC (rev 2345)
@@ -0,0 +1,54 @@
+#' This script simulate univariate normal variables, as described in
+#' A. Meucci, "Risk and Asset Allocation", Springer, 2005, Chapter 1.
+#'
+#' @references
+#' \url{http://}
+#' See Meucci's script for "S_NormalSample.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @export
+
+##################################################################################################################
+### Input parameters
+nSim = 10000;
+mu = 3;
+sigma2 = 5;
+
+##################################################################################################################
+### Generate normal sample with above parameters
+sigma = sqrt( sigma2 );
+X = rnorm( nSim, mu, sigma);
+
+##################################################################################################################
+### Plot the sample# plot over time
+plot( X, main = "normal sample vs observation time" );
+
+
+##################################################################################################################
+### Plot the histogram
+NumBins = round( 10 * log( nSim ) );
+hist( X, NumBins, main = "histogram of normal sample" );
+
+##################################################################################################################
+### Compare empirical with exact cdfs
+
+# plot empirical cdf
+f = ecdf( X );
+plot( f, col = "red", main = "cdf of normal distribution" );
+
+# plot exact cdf
+F = pnorm( 1:10000, mu, sigma );
+lines ( 1:10000, F, col = "blue" );
+legend( "bottomright", 1.9, c("empirical", "exact"), col = c("red", "blue"), lty = 1, bg = "gray90" );
+
+##################################################################################################################
+### Compare empirical and exact quantiles
+# plot empirical quantile
+u= seq( 0.01, 0.99, 0.01 ); # range of quantiles (values between zero and one)
+q = quantile( X, u );
+plot( u, q, type = "l", xlab="Grade", ylab="Quantile", lty = 1, col = "red", main = "quantile of normal distribution" );
+
+# plot exact quantile
+Q = qnorm( u, mu, sigma );
+lines( u, Q, type = "l", lty = 1, col = "blue" );
+legend( "bottomright", 1.9, c( "empirical", "exact" ), col = c( "red", "blue" ), lty = 1, bg = "gray90" );
Added: pkg/Meucci/demo/S_StudentTSample.R
===================================================================
--- pkg/Meucci/demo/S_StudentTSample.R (rev 0)
+++ pkg/Meucci/demo/S_StudentTSample.R 2013-06-17 17:12:41 UTC (rev 2345)
@@ -0,0 +1,74 @@
+#' This script simulate univariate Student-t variables as described in
+#' A. Meucci, "Risk and Asset Allocation", Springer, 2005, Chapter 1.
+#'
+#' @references
+#' \url{http://}
+#' See Meucci's script for "S_StudentTSample.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @export
+
+##################################################################################################################
+### Input parameters
+
+nSim = 10000;
+sigma2 = 6;
+ExpX = 2;
+VarX = 7;
+
+##################################################################################################################
+### Determine mu, nu and sigma
+
+mu = ExpX;
+nu = 2 / (1 - sigma2 / VarX );
+sigma = sqrt( sigma2 );
+
+##################################################################################################################
+### Generate Student t sample with above parameters using built-in generator
+
+X_a = mu + sigma * rt( nSim, nu );
+
+##################################################################################################################
+### Generate Student t sample with above parameters using stochastic representation
+
+Y = rnorm( nSim, 0, sigma );
+Z = rchisq( nSim, nu );
+X_b = mu + Y / sqrt( Z / nu );
+
+##################################################################################################################
+### Generate Student t sample with above parameters using grade inversion
+
+U = runif( nSim );
+X_c = mu + sigma * qt( U, nu );
+
+##################################################################################################################
+### Plot histograms
+NumBins = round(10 * log(nSim));
+
+par( mfrow = c( 3, 1) );
+
+hist( X_a, NumBins, main = "built-in generator" );
+hist( X_b, NumBins, main = "stoch. representation" );
+hist( X_c, NumBins, main = "grade inversion" );
+
+
+#axisLimits = [min(axisLimits(:, 1)), max(axisLimits(:, 2)), min(axisLimits(:, 3)), max(axisLimits(:, 4))];
+#subplot(3, 1, 1), axis(axisLimits);
+#subplot(3, 1, 2), axis(axisLimits);
+#subplot(3, 1, 3), axis(axisLimits);
+
+##################################################################################################################
+### Compare empirical quantiles of the three simuations
+u= seq( 0.01, 0.99, 0.01 ); # range of quantiles (values between zero and one) = 0.01 : 0.01 : 0.99; # range of quantiles (values between zero and one)
+q_a = quantile( X_a, u );
+q_b = quantile( X_b, u );
+q_c = quantile( X_c, u );
+
+##################################################################################################################
+### Superimpose the the plots of the empirical quantiles
+
+plot( u, q_a, type = "l", xlab="Grade", ylab="Quantile", lty = 1, col = "red", main = "quantile of Student-t distribution" );
+lines( u, q_b, type = "l", lty = 1, col = "blue" );
+lines( u, q_c, type = "l", lty = 1, col = "green" );
+legend( "bottomright", 1.9, c( "built-in generator", "stoch. representation", "grade inversion" ), col = c( "red" , "blue", "green"),
+ lty = 1, bg = "gray90" );
\ No newline at end of file
More information about the Returnanalytics-commits
mailing list