MVN                  package:mixAK                  R Documentation

_M_u_l_t_i_v_a_r_i_a_t_e _n_o_r_m_a_l _d_i_s_t_r_i_b_u_t_i_o_n

_D_e_s_c_r_i_p_t_i_o_n:

     Density and random generation for the multivariate normal
     distribution with mean equal to 'mean', precision matrix equal to
     'Q' (or covariance matrix equal to 'Sigma').

     Function 'rcMVN' samples from the multivariate normal distribution
     with a canonical mean b, i.e., the mean is mu = Q^{-1} * b.

_U_s_a_g_e:

     dMVN(x, mean=0, Q=1, Sigma, log=FALSE)

     rMVN(n, mean=0, Q=1, Sigma)

     rcMVN(n, b=0, Q=1, Sigma)

_A_r_g_u_m_e_n_t_s:

    mean: vector of mean.

       b: vector of a canonical mean.

       Q: precision matrix of the multivariate normal distribution.
          Ignored if 'Sigma' is given.

   Sigma: covariance matrix of the multivariate normal distribution. If
          'Sigma' is supplied, precision is computed from Sigma as Q =
          Sigma^{-1}.

       n: number of observations to be sampled.

       x: vector or matrix of the points where the density should be
          evaluated.

     log: logical; if 'TRUE', log-density is computed

_V_a_l_u_e:

     Some objects.

_V_a_l_u_e _f_o_r _d_M_V_N:

     A vector with evaluated values of the (log-)density

_V_a_l_u_e _f_o_r _r_M_V_N:

     A list with the components:

     _x vector or matrix with sampled values

     _l_o_g._d_e_n_s vector with the values of the log-density evaluated in
          the sampled values

_V_a_l_u_e _f_o_r _r_c_M_V_N:

     A list with the components:

     _x vector or matrix with sampled values

     _m_e_a_n vector or the mean of the normal distribution    

     _l_o_g._d_e_n_s vector with the values of the log-density evaluated in
          the sampled values

_A_u_t_h_o_r(_s):

     Arno&#353t Kom&#225rek arnost.komarek[AT]mff.cuni.cz

_R_e_f_e_r_e_n_c_e_s:

     Rue, H. and Held, L. (2005). _Gaussian Markov Random Fields:
     Theory and Applications_. Boca Raton: Chapman and Hall/CRC.

_S_e_e _A_l_s_o:

     'dnorm', 'Mvnorm'.

_E_x_a_m_p_l_e_s:

     set.seed(1977)

     ### Univariate normal distribution
     ### ==============================
     c(dMVN(0), dnorm(0))
     c(dMVN(0, log=TRUE), dnorm(0, log=TRUE))

     rbind(dMVN(c(-1, 0, 1)), dnorm(c(-1, 0, 1)))
     rbind(dMVN(c(-1, 0, 1), log=TRUE), dnorm(c(-1, 0, 1), log=TRUE))

     c(dMVN(1, mean=1.2, Q=0.5), dnorm(1, mean=1.2, sd=sqrt(2)))
     c(dMVN(1, mean=1.2, Q=0.5, log=TRUE), dnorm(1, mean=1.2, sd=sqrt(2), log=TRUE))

     rbind(dMVN(0:2, mean=1.2, Q=0.5), dnorm(0:2, mean=1.2, sd=sqrt(2)))
     rbind(dMVN(0:2, mean=1.2, Q=0.5, log=TRUE), dnorm(0:2, mean=1.2, sd=sqrt(2), log=TRUE))

     ### Multivariate normal distribution
     ### ================================
     mu <- c(0, 6, 8)
     L <- matrix(1:9, nrow=3)
     L[upper.tri(L, diag=FALSE)] <- 0
     Sigma <- L %*% t(L)
     Q <- chol2inv(chol(Sigma))
     b <- solve(Sigma, mu)

     dMVN(mu, mean=mu, Q=Q)
     dMVN(mu, mean=mu, Sigma=Sigma)
     dMVN(mu, mean=mu, Q=Q, log=TRUE)
     dMVN(mu, mean=mu, Sigma=Sigma, log=TRUE)

     xx <- matrix(c(0,6,8, 1,5,7, -0.5,5.5,8.5, 0.5,6.5,7.5), ncol=3, byrow=TRUE)
     dMVN(xx, mean=mu, Q=Q)
     dMVN(xx, mean=mu, Sigma=Sigma)
     dMVN(xx, mean=mu, Q=Q, log=TRUE)
     dMVN(xx, mean=mu, Sigma=Sigma, log=TRUE)

     zz <- rMVN(1000, mean=mu, Sigma=Sigma)
     rbind(apply(zz$x, 2, mean), mu)
     var(zz$x)
     Sigma
     cbind(dMVN(zz$x, mean=mu, Sigma=Sigma, log=TRUE), zz$log.dens)[1:10,]

     zz <- rcMVN(1000, b=b, Sigma=Sigma)
     rbind(apply(zz$x, 2, mean), mu)
     var(zz$x)
     Sigma
     cbind(dMVN(zz$x, mean=mu, Sigma=Sigma, log=TRUE), zz$log.dens)[1:10,]

     zz <- rMVN(1000, mean=rep(0, 3), Sigma=Sigma)
     rbind(apply(zz$x, 2, mean), rep(0, 3))
     var(zz$x)
     Sigma
     cbind(dMVN(zz$x, mean=rep(0, 3), Sigma=Sigma, log=TRUE), zz$log.dens)[1:10,]

     ### The same using the package mvtnorm
     ### ==================================
     # require(mvtnorm)
     # c(dMVN(mu, mean=mu, Sigma=Sigma), dmvnorm(mu, mean=mu, sigma=Sigma))
     # c(dMVN(mu, mean=mu, Sigma=Sigma, log=TRUE), dmvnorm(mu, mean=mu, sigma=Sigma, log=TRUE))
     #
     # rbind(dMVN(xx, mean=mu, Sigma=Sigma), dmvnorm(xx, mean=mu, sigma=Sigma))
     # rbind(dMVN(xx, mean=mu, Sigma=Sigma, log=TRUE), dmvnorm(xx, mean=mu, sigma=Sigma, log=TRUE))

