Keywords - Function groups - @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Library: distribs
See also: normal genar

Quantlet: gennorm
Description: Generates observations from a multivariate normal distribution with given mean vector and covariance matrix.

Usage: y=gennorm(n,mu,sig)
Input:
n scalar, sample size
mu p x 1 matrix, mean vector
sig p x p matrix, covariance matrix
Output:
y n x p matrix, generated observations

Example:
library("distribs")
n	= 1000     			; sample size
sig= #(1,0.7)~#(0.7,1)
mu	= #(0,0)			; mean vector
y	= gennorm(n, mu, sig)
d	= createdisplay(1,1)
show(d,1,1,y)

Result:
Shows 1000 observations from a multivariate normal distribution
with mean vector mu and covariance matrix sig.
Example:
library("distribs")          ; xplore library, contains "normgen"
library("graphic")           ; graphic library, contains "grcontour2"
mu=#(3,2)                    ; mean vector
a=#(1,-1.5)
b=#(-1.5,4)                  ; covariance matrix
sigma=a~b
x=gennorm(200, mu, sigma)    ; creates normal sample
di=createdisplay(1, 2)
show(di, 1, 1, x )           ; shows point cloud
setgopt(di, 1, 1, "title", "normal sample","xlabel","X1","ylabel","X2")
xmin=min(x)
xrange=max(x)-min(x)
ng=30                        ; number of grid points in each dimension
ng=ng*matrix(cols(x))
d=xrange'./(ng-1)            ; binwidth in each dimension
xg=grid(xmin',d,ng)
f=exp(-sum(((xg-mu')*inv(sigma)).*(xg-mu'),2)/2)/sqrt(det(2*pi*sigma)); normal density
c  =(1:9)/10*max(f)         ; computes contours at 10%..90% level
color=(0:6)|(0:1)
g = grcontour2(xg~f,c,color) ; macro for contours
show(di, 1, 2, g)           ; shows contour ellipses
setgopt(di, 1, 2, "title", "contour ellipses","xlabel","X1","ylabel","X2")

Result:
computes a scatterplot of a normal sample and in a second plot the
contour ellipses for mu =#(3,2) and sigma = #(1,-1.5)~#(-1.5,4)



Author: MD*Tech, 20030103 license MD*Tech
(C) MD*TECH Method and Data Technologies, 05.02.2006