Overview

StatComp18001 is a simple R package including three functions, namely, generateBetaRandomVariable (generate beta distribution samples using R), giniRatioComputing (Computing Gini ratio using R) and generateCauthyMetropolisHastings (Generate cauthy distribution with Metropolis Hastings method).

betaSampling

The source R code for generateBetaRandomVariable is as follows:

generateBetaRandomVariable <- function(a,b) {
    n <- 1e3
    j<-k<-0
    y <- numeric(n)
    while (k < n) {
        u <- runif(1)
        j <- j + 1
        x <- runif(1) #random variate from g
        if (x^(a-1) * (1-x)^(b-1) > u) {
            #we accept x
            k <- k + 1
            y[k] <- x
        }
    }
    return(y)
}

The above code is a function to generate a random sample of size n from the Beta(a,b) distribution by the acceptance-rejection method.

computingGiniRatio

The source R code for giniRatioComputing is as follows:

giniRatioComputing = function(n,x,mu){
    x=sort(x)
    gini_ratio=0
    a=0
    for (i in 1:n) {
        if(mu==FALSE){
            mu=mean(x)
        }
        a=a+(2*i-n-1)*x[i]
    }
    gini_ratio=a/(n^2*mu)
    return(gini_ratio)
}

The above code is a function to generate a random sample to calculate Gini Ratio statistics.

generateCauthyMetropolisHastings

The source R code for generateCauthyMetropolisHastings is as follows:

generateCauthyMetropolisHastings = function(n, sigma, x0, N) {
    x <- numeric(N)
    x[1] <- x0
    u <- runif(N)
    for (i in 2:N) {
        y <- rnorm(1, x[i-1], sigma)
        if (u[i] <= ( (dt(y, n)*dnorm(x[i-1],y,sigma))/(dt(x[i-1], n)*dnorm(y,x[i-1],n)))){
            x[i] <- y
        }else {
            x[i] <- x[i-1]
        }
    }
    return(x)

The above code is a function to generate a random sample from Cauthy distribution with metropolis Hastings method.



mrjinyx/StatComp18001 documentation built on May 19, 2019, 6:22 p.m.