[Returnanalytics-commits] r2377 - in pkg/Meucci: . R demo man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Jun 19 18:23:02 CEST 2013
Author: xavierv
Date: 2013-06-19 18:23:02 +0200 (Wed, 19 Jun 2013)
New Revision: 2377
Added:
pkg/Meucci/R/NormalCopulaPdf.R
pkg/Meucci/demo/S_DisplayNormalCopulaCdf.R
pkg/Meucci/demo/S_DisplayNormalCopulaPdf.R
pkg/Meucci/man/NormalCopulaPdf.Rd
Modified:
pkg/Meucci/DESCRIPTION
pkg/Meucci/NAMESPACE
pkg/Meucci/R/LognormalCopulaPdf.R
pkg/Meucci/R/LognormalMoments2Parameters.R
pkg/Meucci/demo/S_BivariateSample.R
pkg/Meucci/demo/S_DisplayLognormalCopulaPdf.R
pkg/Meucci/man/LognormalMoments2Parameters.Rd
Log:
- fixed error and added demos for displaying Normal distribution Copula Cdf and pdf
Modified: pkg/Meucci/DESCRIPTION
===================================================================
--- pkg/Meucci/DESCRIPTION 2013-06-19 12:07:16 UTC (rev 2376)
+++ pkg/Meucci/DESCRIPTION 2013-06-19 16:23:02 UTC (rev 2377)
@@ -63,3 +63,4 @@
'LognormalMoments2Parameters.R'
'LognormalParameters2Statistics.R'
'LognormalCopulaPdf.R'
+ 'NormalCopulaPdf.R'
Modified: pkg/Meucci/NAMESPACE
===================================================================
--- pkg/Meucci/NAMESPACE 2013-06-19 12:07:16 UTC (rev 2376)
+++ pkg/Meucci/NAMESPACE 2013-06-19 16:23:02 UTC (rev 2377)
@@ -16,6 +16,7 @@
export(LognormalParam2Statistics)
export(MvnRnd)
export(NoisyObservations)
+export(NormalCopulaPdf)
export(normalizeProb)
export(PanicCopula)
export(PartialConfidencePosterior)
Modified: pkg/Meucci/R/LognormalCopulaPdf.R
===================================================================
--- pkg/Meucci/R/LognormalCopulaPdf.R 2013-06-19 12:07:16 UTC (rev 2376)
+++ pkg/Meucci/R/LognormalCopulaPdf.R 2013-06-19 16:23:02 UTC (rev 2377)
@@ -23,7 +23,7 @@
x = qlnorm( u, Mu, s );
Numerator = ( 2 * pi ) ^ ( -N / 2 ) * ( (det ( Sigma ) ) ^ ( -0.5 ) ) /
- prod(x) * exp( -.5 * t(log(x) - Mu) %*% mldivide( Sigma , ( log( x ) - Mu ), pinv=FALSE ) );
+ prod(x) * exp( -0.5 * t(log(x) - Mu) %*% mldivide( Sigma , ( log( x ) - Mu ), pinv=FALSE ) );
fs = dlnorm( x, Mu, s);
Modified: pkg/Meucci/R/LognormalMoments2Parameters.R
===================================================================
--- pkg/Meucci/R/LognormalMoments2Parameters.R 2013-06-19 12:07:16 UTC (rev 2376)
+++ pkg/Meucci/R/LognormalMoments2Parameters.R 2013-06-19 16:23:02 UTC (rev 2377)
@@ -1,5 +1,5 @@
#' Compute the mean and standard deviation of a lognormal distribution from its parameters, as described in
-#' A. Meucci, "Risk and Asset Allocation", Springer, 2005, Chapter 1.
+#' A. Meucci, "Risk and Asset Allocation", Springer, 2005.
#'
#' @param e : [scalar] expected value of the lognormal distribution
#' @param v : [scalar] variance of the lognormal distribution
@@ -7,11 +7,11 @@
#' @return mu : [scalar] expected value of the normal distribution
#' @return sig2 : [scalar] variance of the normal distribution
#'
-#' @note Inverts the formulas (1.98)-(1.99) in Risk and Asset Allocation", Springer, 2005.
+#' @note Inverts the formulas (1.98)-(1.99) in "Risk and Asset Allocation", Springer, 2005.
#'
#' @references
#' \url{http://}
-#' See Meucci's script for "LognormalMoments2Parameters"
+#' See Meucci's script for "LognormalMoments2Parameters.m"
#'
#' @author Xavier Valls \email{flamejat@@gmail.com}
#' @export
Added: pkg/Meucci/R/NormalCopulaPdf.R
===================================================================
--- pkg/Meucci/R/NormalCopulaPdf.R (rev 0)
+++ pkg/Meucci/R/NormalCopulaPdf.R 2013-06-19 16:23:02 UTC (rev 2377)
@@ -0,0 +1,33 @@
+#' Computes the pdf of the copula of the normal distribution at the generic point u in the unit hypercube,
+#' as described in A. Meucci, "Risk and Asset Allocation", Springer, 2005.
+#'
+#' @param u : [vector] (J x 1) grade
+#' @param Mu : [vector] (N x 1) mean
+#' @param Sigma : [matrix] (N x N) covariance
+#'
+#' @return F_U : [vector] (J x 1) PDF values
+#'
+#' @references
+#' \url{http://}
+#' See Meucci's script for "LognormalCopulaPdf.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @export
+
+NormalCopulaPdf = function( u, Mu, Sigma )
+{
+ N = length( u );
+ s = sqrt( diag( Sigma ));
+
+ x = qnorm( u, Mu, s );
+
+ Numerator = ( 2 * pi ) ^ ( -N / 2 ) * ( (det ( Sigma ) ) ^ ( -0.5 ) ) * exp( -0.5 * t(x - Mu) %*% mldivide( Sigma , ( x - Mu ), pinv = FALSE ) );
+
+ fs = dnorm( x, Mu, s);
+
+ Denominator = prod(fs);
+
+ F_U = Numerator / Denominator;
+
+ return ( F_U );
+}
\ No newline at end of file
Modified: pkg/Meucci/demo/S_BivariateSample.R
===================================================================
--- pkg/Meucci/demo/S_BivariateSample.R 2013-06-19 12:07:16 UTC (rev 2376)
+++ pkg/Meucci/demo/S_BivariateSample.R 2013-06-19 16:23:02 UTC (rev 2377)
@@ -38,7 +38,7 @@
NormCorrMatrix = rbind( c( 1, NormCorr ), c( NormCorr, 1 ));
NormCovMatrix = diag( c( NormStDev ) ) %*% NormCorrMatrix %*% diag( c( NormStDev) );
-Z = rvnorm( nSim, NormExpVal, NormCovMatrix );
+Z = rmvnorm( nSim, NormExpVal, NormCovMatrix );
Z_1 = Z[, 1];
Z_2 = Z[, 2];
Modified: pkg/Meucci/demo/S_DisplayLognormalCopulaPdf.R
===================================================================
--- pkg/Meucci/demo/S_DisplayLognormalCopulaPdf.R 2013-06-19 12:07:16 UTC (rev 2376)
+++ pkg/Meucci/demo/S_DisplayLognormalCopulaPdf.R 2013-06-19 16:23:02 UTC (rev 2377)
@@ -3,7 +3,7 @@
#'
#' @references
#' \url{http://}
-#' See Meucci's script for "S_DisplayLognormalCoulaPdf.m"
+#' See Meucci's script for "S_DisplayLognormalCopulaPdf.m"
#'
#' @author Xavier Valls \email{flamejat@@gmail.com}
#' @export
@@ -38,6 +38,6 @@
#mesh representation
-persp(GridSide1,GridSide2, f_U,
+persp( GridSide1, GridSide2, f_U,
theta = 7 * 45, phi = 30, expand=0.6, col='lightblue', shade=0.75, ltheta=120,
ticktype='detailed', xlab = "U_1", ylab = "U_2", zlab = "copula pdf" );
Added: pkg/Meucci/demo/S_DisplayNormalCopulaCdf.R
===================================================================
--- pkg/Meucci/demo/S_DisplayNormalCopulaCdf.R (rev 0)
+++ pkg/Meucci/demo/S_DisplayNormalCopulaCdf.R 2013-06-19 16:23:02 UTC (rev 2377)
@@ -0,0 +1,43 @@
+#'This script displays the cdf of the copula of a normal distribution, as described
+#' in A. Meucci, "Risk and Asset Allocation", Springer, 2005, Chapter 2.
+#'
+#' @references
+#' \url{http://}
+#' See Meucci's script for "S_DisplayNormalCopulaPdf.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @export
+
+#############################################################################################################
+### Input parameters
+Mu = c( 0, 0 );
+r = -0.999;
+sigmas = c(1, 1 );
+Sigma = diag( c( sigmas ) ) %*% rbind( c( 1, r ), c( r, 1 ) ) %*% diag( c( sigmas ) );
+
+#############################################################################################################
+### Grid
+GridSide1 = seq( 0.05, 0.95, 0.05 );
+GridSide2 = GridSide1;
+nMesh = length(GridSide1);
+
+#############################################################################################################
+### Compute cdf of copula
+
+F_U = matrix( NaN, nMesh, nMesh);
+
+for ( j in 1 : nMesh )
+{
+ for ( k in 1 : nMesh)
+ {
+ u = c( GridSide1[ j ], GridSide2[ k ] );
+ x= qnorm( u, Mu, sigmas );
+ F_U[ j, k ] = pmvnorm( lower = -Inf, upper = x, mean = Mu, corr = Sigma );
+ }
+}
+
+#mesh representation
+
+persp( GridSide1, GridSide2, F_U,
+ theta = 7 * 45, phi = 30, expand=0.6, col='lightblue', shade=0.75, ltheta=120,
+ ticktype='detailed', xlab = "U_1", ylab = "U_2", zlab = "copula cdf" );
\ No newline at end of file
Added: pkg/Meucci/demo/S_DisplayNormalCopulaPdf.R
===================================================================
--- pkg/Meucci/demo/S_DisplayNormalCopulaPdf.R (rev 0)
+++ pkg/Meucci/demo/S_DisplayNormalCopulaPdf.R 2013-06-19 16:23:02 UTC (rev 2377)
@@ -0,0 +1,42 @@
+#'This script displays the pdf of the copula of a normal distribution, as described
+#' in A. Meucci, "Risk and Asset Allocation", Springer, 2005, Chapter 2.
+#'
+#' @references
+#' \url{http://}
+#' See Meucci's script for "S_DisplayNormalCopulaPdf.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @export
+
+#############################################################################################################
+### input parameters
+Mu = rbind( 1, -1 );
+r = 0.7;
+sigmas = rbind( 1, 1 );
+Sigma = diag( c( sigmas ) ) %*% rbind( c( 1, r ), c( r, 1 ) ) %*% diag( c( sigmas ) );
+
+#############################################################################################################
+### Grid
+GridSide1 = seq( 0.05, 0.95, 0.05 );
+GridSide2 = GridSide1;
+nMesh = length(GridSide1);
+
+#############################################################################################################
+### Compute pdf of copula
+
+f_U = matrix( NaN, nMesh, nMesh);
+
+for ( j in 1 : nMesh )
+{
+ for ( k in 1 : nMesh)
+ {
+ u = c( GridSide1[ j ], GridSide2[ k ] );
+ f_U[ j, k ] = NormalCopulaPdf(u, Mu, Sigma);
+ }
+}
+
+#mesh representation
+
+persp( GridSide1, GridSide2, f_U,
+ theta = 7 * 45, phi = 30, expand=0.6, col='lightblue', shade=0.75, ltheta=120,
+ ticktype='detailed', xlab = "U_1", ylab = "U_2", zlab = "copula pdf" );
Modified: pkg/Meucci/man/LognormalMoments2Parameters.Rd
===================================================================
--- pkg/Meucci/man/LognormalMoments2Parameters.Rd 2013-06-19 12:07:16 UTC (rev 2376)
+++ pkg/Meucci/man/LognormalMoments2Parameters.Rd 2013-06-19 16:23:02 UTC (rev 2377)
@@ -1,7 +1,7 @@
\name{LognormalMoments2Parameters}
\alias{LognormalMoments2Parameters}
\title{Compute the mean and standard deviation of a lognormal distribution from its parameters, as described in
-A. Meucci, "Risk and Asset Allocation", Springer, 2005, Chapter 1.}
+A. Meucci, "Risk and Asset Allocation", Springer, 2005.}
\usage{
LognormalMoments2Parameters(e, v)
}
@@ -20,11 +20,10 @@
\description{
Compute the mean and standard deviation of a lognormal
distribution from its parameters, as described in A.
- Meucci, "Risk and Asset Allocation", Springer, 2005,
- Chapter 1.
+ Meucci, "Risk and Asset Allocation", Springer, 2005.
}
\note{
- Inverts the formulas (1.98)-(1.99) in Risk and Asset
+ Inverts the formulas (1.98)-(1.99) in "Risk and Asset
Allocation", Springer, 2005.
}
\author{
@@ -32,6 +31,6 @@
}
\references{
\url{http://} See Meucci's script for
- "LognormalMoments2Parameters"
+ "LognormalMoments2Parameters.m"
}
Added: pkg/Meucci/man/NormalCopulaPdf.Rd
===================================================================
--- pkg/Meucci/man/NormalCopulaPdf.Rd (rev 0)
+++ pkg/Meucci/man/NormalCopulaPdf.Rd 2013-06-19 16:23:02 UTC (rev 2377)
@@ -0,0 +1,31 @@
+\name{NormalCopulaPdf}
+\alias{NormalCopulaPdf}
+\title{Computes the pdf of the copula of the normal distribution at the generic point u in the unit hypercube,
+as described in A. Meucci, "Risk and Asset Allocation", Springer, 2005.}
+\usage{
+ NormalCopulaPdf(u, Mu, Sigma)
+}
+\arguments{
+ \item{u}{: [vector] (J x 1) grade}
+
+ \item{Mu}{: [vector] (N x 1) mean}
+
+ \item{Sigma}{: [matrix] (N x N) covariance}
+}
+\value{
+ F_U : [vector] (J x 1) PDF values
+}
+\description{
+ Computes the pdf of the copula of the normal distribution
+ at the generic point u in the unit hypercube, as
+ described in A. Meucci, "Risk and Asset Allocation",
+ Springer, 2005.
+}
+\author{
+ Xavier Valls \email{flamejat at gmail.com}
+}
+\references{
+ \url{http://} See Meucci's script for
+ "LognormalCopulaPdf.m"
+}
+
More information about the Returnanalytics-commits
mailing list