Learn R Programming

RGeode (version 0.1.0)

rgammatr: Random generator for a Truncated Gamma distribution.

Description

Simulate random number from a truncated Gamma distribution.

Usage

rgammatr(n = 1, A = 0, B = 1, range = NULL)

Arguments

n

int, optional number of simulations.

A

double, optional shape parameter of the distribution.

B

double, optional rate parameter of the distribution.

range

array_like, optional domain of the distribution, where we truncate our Gamma. \(range(0)\) is the min of the range and \(range(1)\) is the max of the range.

Value

rgammatr returns the simulated value of the distribution:

u

double it is the simulated value of the truncated Gamma distribution. It will be a value in \((range(0),range(1))\).

Details

It provide a way to simulate from a truncated Gamma distribution with given pameters \(A,B\) and range \(range\). This will be used during the posterior sampling in th Gibbs sampler.

References

  • [1] Y. Wang, A. Canale, D. Dunson. "Scalable Geometric Density Estimation" (2016). The implementation of rgammatr is inspired to the Matlab implementation of gamrndtruncated by Ye Wang.

Examples

Run this code
# NOT RUN {
#Simulate a truncated Gamma with parameters 1,2 in the range
#1,5.
#Set the range:
range<- c(1,5)

#Simulate the truncated Gamma
set.seed(123)
vars1<-rgammatr(1000,1,2,range)

#Look at the histogram
hist(vars1,freq=FALSE,ylim=c(0,2),xlim = c(0,5))
lines(density(vars1))

#Compare with a non truncated Gamma
set.seed(123)
vars2<-rgamma(1000,1,2)


#Compare the two results
lines(density(vars2),col='red')

#Observation: simulate without range is equivalent to simulate from
#rgamma(1000,1,2)

# }

Run the code above in your browser using DataLab