Exponential distribution is a continuous distribution that models random time between events (until next event occurs) during a Poisson process, which is a process with constant occurrence rate and independence among each individual event. Exponential distribution has the following probability density function:
Where positive parameter β denotes the mean time between events, and its reciprocal is the mean rate for event occurring.
In R programming, there are generally four functions that are used for calculating Exponential distributions.
dexp() – probability density
pexp() – cumulative probability till specified quantile value
qexp() – quantile value, below which is the specified cumulative probability. This function is the opposite operation of pexp().
rexp() – for random number generation from a specified Exponential distribution.
Next we will show how to implement these functions in RStudio.
# dexp(x_vec, rate) for probability density of Exponential distribution
# rate here represents the mean number occurrence of poisson events
#during a unit time period. It is 1 / beta in the formula.
# Specify a vector
x_vec <- seq(1, 10, by = 0.2)
x_vec
#output
[1] 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4
[14] 3.6 3.8 4.0 4.2 4.4 4.6 4.8 5.0 5.2 5.4 5.6 5.8 6.0
[27] 6.2 6.4 6.6 6.8 7.0 7.2 7.4 7.6 7.8 8.0 8.2 8.4 8.6
[40] 8.8 9.0 9.2 9.4 9.6 9.8 10.0
#calculate probability densities for these values, from an Exponential distribution
#with rate 3
DEX <- dexp(x_vec, rate = 3)
DEX
#output
[1] 1.493612e-01 8.197117e-02 4.498673e-02 2.468924e-02 1.354974e-02
[6] 7.436257e-03 4.081104e-03 2.239757e-03 1.229205e-03 6.746020e-04
[11] 3.702294e-04 2.031862e-04 1.115110e-04 6.119851e-05 3.358645e-05
[16] 1.843264e-05 1.011605e-05 5.551804e-06 3.046894e-06 1.672171e-06
[21] 9.177070e-07 5.036483e-07 2.764080e-07 1.516959e-07 8.325250e-08
[26] 4.568994e-08 2.507517e-08 1.376155e-08 7.552496e-09 4.144898e-09
[31] 2.274768e-09 1.248419e-09 6.851470e-10 3.760166e-10 2.063623e-10
[36] 1.132540e-10 6.215513e-11 3.411146e-11 1.872077e-11 1.027417e-11
[41] 5.638586e-12 3.094522e-12 1.698310e-12 9.320521e-13 5.115210e-13
[46] 2.807287e-13
# Plot dexp values
plot(DEX) #calculate cumulative probabilities for these values
PEX <- pexp(x_vec, rate = 3)
PEX
#output
[1] 0.9502129 0.9726763 0.9850044 0.9917703 0.9954834 0.9975212
[7] 0.9986396 0.9992534 0.9995903 0.9997751 0.9998766 0.9999323
[13] 0.9999628 0.9999796 0.9999888 0.9999939 0.9999966 0.9999981
[19] 0.9999990 0.9999994 0.9999997 0.9999998 0.9999999 0.9999999
[25] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
[31] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
[37] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
[43] 1.0000000 1.0000000 1.0000000 1.0000000
# Plot cumulative probabilities
plot(PEX) # Specify vector for cumulative probabilities
prob_vec <- seq(0, 1, by = 0.1)
prob_vec
#output
[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
# Calculate quantile values for these cumulative probabilities
#from an Exponential distribution with rate 3
QEX <- qexp(prob_vec, rate = 3)
QEX
#output
[1] 0.00000000 0.03512017 0.07438118 0.11889165 0.17027521
[6] 0.23104906 0.30543024 0.40132427 0.53647930 0.76752836
[11] Inf
# Plot values
plot(QEX) #Set number 30
N <- 30
# Draw N random number from Exponential distribution with rate 3
REX <- rexp(N, rate = 3)
REX
#output
[1] 0.347262698 0.113683445 0.134654860 0.004289886 0.112857280
[6] 0.115457058 0.072351288 0.983339690 0.022066066 0.115432567
[11] 0.271507935 0.724925475 0.899791045 0.451568319 0.184542056
[16] 0.061661427 0.208170068 0.044074432 0.137766094 0.150764298
[21] 0.485294863 0.079782160 0.020996603 0.187394563 0.058556869
[26] 0.021327833 0.041098657 0.196455535 0.599667342 0.488005906
# Plot random numbers in a histogram with 6 bins
hist(REX, breaks = 6, main = "") To train a PyTorch Transformer for multi-track music generation, the standard paradigm is to convert…
In the field of AI and Natural Language Processing (NLP), The Transformer is the engine…
If your goal is purely symbolic melody generation (e.g., generating MIDI files or notes rather…
According to PyTorch document, In PyTorch, torch.nn.functional.affine_grid generates a 2D or 3D flow field (sampling…
Creating an Android mobile app with a deep learning AI model involves several key steps:…
Click here to go to source files for R Machine Learning