######################################################################## #### PRACTICAL 3 - probability distributions : GAUSSIAN ######################################################################## Learning objectives: Statistics: Computer science: - Distributions - functions - Gaussian ######################################################################## ### Exercise 0 : plot of a distribution ################################ ######################################################################## # STRUCTURE # # 1. parameters definition # 2. function definition, standard form: functname <- function(x){ code } # 3. plot of a single gaussian (with Mu=0 and Sigme=1) # 4. plot of multiple gaussian distributions # parameters definition (1) ########################################### # NB: GLOBAL variables that are used INSIDE the function f1, so that you # can update these variables and get different results from different # function calls Mu <- 0 Sigma <- 1 # function definition (2) ############################################# f1 <- function(x){ (1 / (Sigma*(sqrt(2*pi))) ) * exp( (-1/2)*( (x-Mu)/Sigma )^2) } # plot of a single gaussian (with Mu=0 and Sigme=1) (3) ######## plot(f1, xlim=c(-3,3), ylim=c(0,1)) abline(v=0) # this is to plot a single vertical line # NB: don't close this plot ... we will need it in 4 # plot of multiple gaussian distributions (4) ##################### sigmas <- c(0.5, 2, 4) mus <- c(-0.5, 0, 0.5) for (Sigma in sigmas){ for (Mu in mus){ plot(f1, -3, 3, add=TRUE) } } ######################################################################## ### Exercise 1 : Integral computation with x in fixed range and Sigma in ### a set of values ######################################################################## # STRUCTURE # # 1. Range definition (in the form [Mu-q, Mu+q] # 2. Gaussian parameters setting # 3. Computation of P(x) in [Mu-q, Mu+q] # Range definition (in the form [Mu-q, Mu+q] (1) ######### q <- 1 # Gaussian parameters setting (fixed Mu and the sigmas set (2) ######## Mu <- 0 sigmas <- c(0.5, 2, 4) # Computation of P(x) in [Mu-q, Mu+q] for different Sigma values (3) ## for (Sigma in sigmas){ Ttmpstr <- paste( "P(x) from ", Mu-q , " to ", Mu+q , " with Sigma=", Sigma, " is: ", ( integrate(f1, lower=(Mu-q), upper=(Mu+q)) )$value , sep='') print( tmpstr ) } ######################################################################## ### Exercise 2 : P(x) computation in ranges # [Mu - Sigma , Mu + Sigma] ######################################################################## # STRUCTURE # # 0. Set the value of Mu (constant), then set the value of Sigma ( 1 ) # 1. Range definition (in the form [Mu-q, Mu+q] : Set the value of q to Sigma, 2*Sigma, 3*Sigma # 3. Computation of P(x) in [Mu-q, Mu+q] # 1. ################################################################## Mu <- 0 Sigma <- 1 # 2 ################################################################## q <- Sigma # 3 ################################################################## integrate(f1, lower=(Mu-q), upper=(Mu+q)) ##### NOW REPEAT AFTER THE UPDATE OF THE q VARIABLE : q <- 2 * Sigma integrate(f1, lower=(Mu-q), upper=(Mu+q)) q <- 3 * Sigma integrate(f1, lower=(Mu-q), upper=(Mu+q)) ####################################################################### # You can now change the value of Sigma and then repeat points 2 and 3 # (in point 3 you should check for each value of q in Sigma, 2*Sigma and # 3*Sigma ) ###### SPECIAL VALUES ################################################ q <- 1.96 integrate(f1, lower=(Mu-q), upper=(Mu+q))