hello welcome to our channel for doing statistics using R in this video we will talk about Gamma distribution and Sh functions for gamma distributions in our programming so first we take a look at Gamma distribution so the GMA distribution is a general probability distribution of the time between events in por event process so the point and event process is a process in which we focus on events Ur continuously and independently in constant average rat so for example if you want to know the distribution of the telephone calls coming a call center during one day or specific time interval so this is quite like a poon process and a poon process has a constant uring rate so we say average average 1 hour there is 1. 5 telephone coming in in the call center so this is called Lambda usually called this parameter Lambda and GMA distribution is based on what we want to know the probability of the time until event coming so for example for the exponential distribution which is a particular case of the GMA distribution focus on the time until next event so next to one event so this is for exponential distribution and many other distributions like K square of beta distribution and exponential distribution of course they are all special cases of G GMA distribution with different value for the parameter so the gamma distribution has two parameter Alpha and beta Alpha is the shape for example in exponential distribution Alpha is one because because we want to know the probability of the time until next occurring so this is the one one event one point event so Alpha is One beta beta is what we have called lamb Lambda is a rate beta is the reciprocal of the Lambda so beta is for example in exponential distribution beta is for the average time until next event coming in so the beta is for the time time so if we look at this figure there are several curves for gamma distribution with different Alpha and beta for example this one alpha 1 but beta 1 is for exponential distribution and this one alpha 2 beta 1 is for K Square distribution and so on so now we look at the density function for gamma distribution first we have no gamma function so gamma function with one parameter Alpha is the integral for the positive value of DX with the function X Alpha - Y in power time e - x in power and Alpha is positive number and X is also positive number so this is gamma function and a gamma function has some interesting properties for example gamma n gamma n equals the factorial of n minus one and Gamma 1 here is equals one GMA 1 is one and Gamma n or gamma n is nus one factorial and Gamma 0. 5 is square root of Pi so gamma function is very interesting So based on the gamma function can build the density function for gamma distribution so this is gamma distribution for time x and Alpha Beta so X is the random variable this is usually for the time and Alpha Beta is two parameter we have here in the previous page this is f shape this is for scale and the density function is 1 / the beta Alpha in power time gamma function of alpha then * X Alpha - 1 in power and e - x x / beta in power so in some expression they also use the Lambda here so because beta is a reoc of Lambda so just few in the Lambda so for example oneid by Lambda in the not in the denominator in the numerator now and both Alpha and beta a positive number the mean for the beta function is Alpha Beta and variance is Alpha Beta Square so if we want to use beta functions in R so there are four functions called D gamma P gamma Q gamma and R gamma so for the D gamma is for the density and the probability P gamma for the cumulative distribution or cumulative probability and and Q gamma for Quant value and R gamma for random number generation and the parameter have shape and rate rate is Lambda usually is one if we use scale so do not use so not use rate so you can use rate or scale but not both and scale is one divid by rate so next we will show several examples in our studio using gamma distribution so first function is D gamma which is used to calculate the density value of random number X in a spe a specific commma distribution with shape and scale per so shape is Alpha and a scale is beta a scale default is one so we create a vector of x value from 0 to five with gap 0.
2 so next you can use D gamma function of this vecture with the shape Alpha is six and with scale one or five so which means the rate is five right so in the a specific unit time for example there five telephone coming in so this rate is five so average scale is one five so average time between next between two events is 105 right so here we use D gamma and for the X gamma this vecture for the random variable this representing for the random time and the shape for the six and the scale is 105 then we can run then can plot this density you can see this this density mimic the density curve of this gamma distribution for example for the random number five oh sorry here is the fifths because the total 25 value from zero to five so here the fifths value is about to one for the random number one so Random Number One the density is about 0. 8 so which means which means the probability density for the random number X which is the random time is 0. 8 next function is p GMA which is more interesting or for cumulative probability because we we usually do not focus on density value we we like to know the probability so this function is used to calculate the probability cumulative probability upon one random number value so say the formula is p gamma X this Vector the same as before and with a shape and scale scale default to one same as in a density function so we create again one vector between 0 and 10 with The Gap 0.
2 then sorry then we run this function for the P gamma and with the shape three and what by the default is one for the scale parameter then we plot so now I can see this cumulative probability function is increasing so for example this this value is zero this is 10 so this is about four four right this is four so for for the random number xals 4 the cumulative probability about 0. 7 so which means about 70% of the values in this gamma distribution has less than equal to value four right and next function is Q gamma which is the opposite operation of the P gamma so Q gamma is used to calculate the quantal value or the random number from the given cumulative probability so the formula is Q gamma with this x gamma here is cumulative probability and with shape or scale parameter so we create a vector between zero and one representing the cumulative probability then we can calculate the Quant or the random number Vector from this cumulative probability from a gamma distribution with the shape or Alpha equal 3 and with the betaal next we plot this vecture of random number also increasing because the cumulative probability increasing this this random number Vector also increasing so for example here 0. 5 cumulative probability the value is about about three little lower than three 2.
8 for example so which means the about 50% of the values in this gamma distribution has the value lower than or equal to 2. 8 right so the next function called a gamma which is quite meaningful is used to Generate random number from a gamma distribution so we set n equals 1,000 which means we want to generate 1,000 such random number then with our gamma function in the shape to Alpha = 2 and scale of beta = 105 then we show this Vector 1,000 such random number all the positive number then can print these numbers in his histogram this histogram also also mimic a gamma distribution for example here about 0. 2 has the highest cons and out the value about 1.
2 a very low frequency very low probability density if you look at Alpha and beta so Alpha is two beta is 105 so the mean value of this gamma distribution is 2 of five is 0. 4 sorry 0. 4 so the mean value is here for this gamma distribution so it's quite understandable that most values stay around this mean value in the end of this video we will show an example using gamma distribution so suppose that telephone calls arriving at a particular switchboard follow a por process so the POR process of GMA por process is for exponential for gamma distribution they all apply with a POS process so with an average of five Calles coming per minute so this is the Lambda average five calls coming per minute so this Lambda is five which means the beta is one five right so what is the probability that within one minutes at the most two calls have come in to the switchboard at the most so which means the random number less than equal to right so the solution is the PO process applies with a time until two poone events following a gamma distribution with Alpha = 2 Beta = 105 so if we denote by X One the time in minutes that transpires before two CES coming in the required probability is given by a gamma distribution with Alpha 2 Beta one or5 so we calculate this probability using p gamma because we want to know we want to know the probability of all the values less than equal to one in this gamma distribution where Alpha = 2 and Lambda equal 105 sorry scale equal 105 so betaal 105 so we just calculated this cumulative probability for x = 1 and the shape equal 2 the scale = 105 so the value is about 0.