Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Outline Joint Probability Calculations in R Mean Value of Functions of Random Variables Extra Topic: Integration in R Joint Probability Calculations in R M. George Akritas M. George Akritas Joint Probability Calculations in R Outline Joint Probability Calculations in R Mean Value of Functions of Random Variables Extra Topic: Integration in R Joint Probability Calculations in R Mean Value of Functions of Random Variables Extra Topic: Integration in R M. George Akritas Joint Probability Calculations in R Outline Joint Probability Calculations in R Mean Value of Functions of Random Variables Extra Topic: Integration in R Marginal and Conditional PMFs through R pXY=c(.388 , .485, .090 , .009 , .010, .008 ,.003, .005, .002) # the vector of joint probabilities from the rat-drug example pXYm =matrix(pXY,3,3) # the matrix of joint probabilities px=rowSums(pXYm); py=colSums(pXYm) # the marginal PMFs diag(1/px) %*% pXYm # the matrix pY |X =x (y ) x,y pXYm[1,]/px[1] # the first row of pY |X =x (y ) x,y pXYm %*% diag(1/py) # the matrix pX |Y =y (x) x,y pXYm[,1]/py[1] # the first column of pX |Y =y (x) M. George Akritas x,y Joint Probability Calculations in R Outline Joint Probability Calculations in R Mean Value of Functions of Random Variables Extra Topic: Integration in R The joint PMF of independent RVs in R For X ∼ Bin(2, 0.5), Y ∼ Bin(3, 0.5) independent, do: pXYm=dbinom(0:2,2,0.5) %*% t(dbinom(0:3,3,0.5)) Alternatively: P=expand.grid(px=dbinom(0:2,2,0.5),py=dbinom(0:3,3,0.5)); P$pxy=P$px*P$py; pXYm =matrix(P$pxy,3,4) Check: P$pxy same as pXY=as.vector(pXYm) Also check: px=rowSums(pXYm) ; py=colSums(pXYm) # same as dbinom(0:2,2,0.5) and dbinom(0:3,3,0.5), respectively. M. George Akritas Joint Probability Calculations in R Outline Joint Probability Calculations in R Mean Value of Functions of Random Variables Extra Topic: Integration in R The PMF and CDF of X + Y with R I Let X ∼ Bin(3, 0.3) and Y ∼ Bin(4, 0.6), X , Y independent. P=expand.grid(px=dbinom(0:3,3,.3),py=dbinom(0:4,4,.6)); P$pxy=P$px*P$py G=expand.grid(X=0:3,Y=0:4); P$X=G$X; P$Y=G$Y; attach(P) sum(pxy[which(X+Y==4)]) # pmf of X + Y at x = 4 sum(pxy[which(X+Y<=4)]) # cdf of X + Y at x = 4 sum(pxy[which(3<X+Y & X+Y<=5)]) # P(3 < X + Y ≤ 5) I Try: v=seq(0,20,2) ; v[5] ; which(v == 8) ; which(2<= v & v<= 8); sum(v[2:5]) ; sum(v[which(2<= v & v<= 8)]) M. George Akritas Joint Probability Calculations in R Outline Joint Probability Calculations in R Mean Value of Functions of Random Variables Extra Topic: Integration in R Another Conditional PMF with R I With X and Y as in the previous slide, find the conditional PMF of X given that X + Y = 6. Solution: Given X + Y = 6, the possible values of X (i.e. the conditional sample space) are 3 and 2. X[which(X+Y==6)] # gives the conditional sample space px[which(X+Y==6)]*py[which(X+Y==6)]/sum(pxy[which(X+Y==6)]) # gives the conditional pmf, M. George Akritas Joint Probability Calculations in R Outline Joint Probability Calculations in R Mean Value of Functions of Random Variables Extra Topic: Integration in R Proposition Each of the following statements implies, and is implied by, the independence of X and Y . 1. pY |X (y |x) = pY (y ). 2. pY |X (y |x) does not depend on x, i.e. is the same for all possible values of X . 3. pX |Y (x|y ) = pX (x). 4. pX |Y (x|y ) does not depend on y , i.e. is the same for all possible values of Y . M. George Akritas Joint Probability Calculations in R Outline Joint Probability Calculations in R Mean Value of Functions of Random Variables Extra Topic: Integration in R Verification of the proposition with R Use the joint PMF of X ∼ Bin(2, 0.5) and Y ∼ Bin(3, 0.5) , X , Y independent. pXYm[1,]/rowSums(pXYm)[1] # gives P(Y = y |X = 0), y = 0 : 3 Check that this is the same as dbinom(0:3,3,.5) # why? Next verify that the following are the same: pXYm[2,]/rowSums(pXYm)[2] # gives P(Y = y |X = 1), y = 0 : 3 pXYm[3,]/rowSums(pXYm)[3] # gives P(Y = y |X = 2), y = 0 : 3 M. George Akritas Joint Probability Calculations in R Outline Joint Probability Calculations in R Mean Value of Functions of Random Variables Extra Topic: Integration in R Mean value of a function in R pxy=c(0.10, 0.08, 0.06, 0.04, 0.20, 0.14, 0.02, 0.06, 0.30) pxym=matrix(pxy,3,3) px=rowSums(pxym) ; py=colSums(pxym) P=expand.grid(px=px,py=py) ; P$pxy=pxy G=expand.grid(x=0:2,y=0:2) ; P$x=G$x; P$y=G$y attach(P) ; sum((x+y)*pxy) sum(x*pxy) ; sum(y*pxy) sum((x+y)**2*pxy) sum(pmin(x,y)*pxy) ; sum(pmin(x,y)**2*pxy) Also, E (e X +Y ) = sum(exp(x+y)*pxy) etc. M. George Akritas Joint Probability Calculations in R Outline Joint Probability Calculations in R Mean Value of Functions of Random Variables Extra Topic: Integration in R Proposition Let X1 , . . . , Xn be a simple random sample from a population having variance σ 2 . Then n E S 2 = σ 2 , where S 2 = 2 1 X Xi − X . n−1 i=1 The next slide gives a numerical verification of this proposition. M. George Akritas Joint Probability Calculations in R Outline Joint Probability Calculations in R Mean Value of Functions of Random Variables Extra Topic: Integration in R Proof by Simulation I Random number generation can be used to provide numerical evidence in support of, or in contradiction to, certain probabilistic/statistical claims. I Evidence for the above proposition can be obtained by generating a large number of samples (of any given size and from any distribution), compute the sample variance for each sample, and average the sample variances. I We will use 10,000 samples of size 5 from the standard normal distribution. The R commands are: > m=matrix(rnorm(50000),ncol=10000) > mean(apply(m,2,var)) M. George Akritas Joint Probability Calculations in R Outline Joint Probability Calculations in R Mean Value of Functions of Random Variables Extra Topic: Integration in R Try the following: integrate(dnorm, -Inf, Inf) ; integrate(dnorm, -Inf, Inf)$value integrate(dnorm, -Inf, 1.1) # same as pnorm(1.1) Define your own function and integrate it: f = function(x) {1/((x+1)*sqrt(x))} integrate(f, lower = 0, upper = 5) Can also do derivatives (symbolic and algorithmic): Type ?deriv M. George Akritas Joint Probability Calculations in R Outline Joint Probability Calculations in R Mean Value of Functions of Random Variables Extra Topic: Integration in R Double Integrals The following commands compute R 0.5 R y 0 −0.5 tan(x + y )dxdy : g1=function(y){integrate(function(x) tan(x+y), -0.5, y)$value} # the inside integral g2=function(y) {sapply(y,g1)} # note g2(y)=g1(y) for all y integrate(g2,0,.5) What is missing is the ability to define g=function(x,y){tan(x+y)*(x¡y)} # this you can do and then use a command like integrate(g, over=(-0.5, 0.5)x(0, 0.5)) M. George Akritas Joint Probability Calculations in R