Giter Club home page Giter Club logo

betalu's Introduction

betalu

betalu implements the (d/p/q/r) statistics functions for the beta distribution with support [l,u] (also called the four parameter beta) in R. It is ideal for using in other packages since it is light weight and leverages the (d/p/q/r)beta() line of functions maintained by CRAN.

Getting betalu

Right now, you have to get betalu from GitHub, since it's not on CRAN yet. Here's how you do that:

# install.packages("devtools")
devtools::install_github("dkahle/betalu")

The (d/p/q/r)betalu() functions

The defining property of the betalu distribution is that it is the beta distribution drawn to a different support. In other words, it is the distribution of the random variable (Y = (u-l)*X + l), where (X) has a beta distribution.

The PDF (the f(x)) can be evaluated with the dbetalu() function:

library(betalu)
library(ggplot2); theme_set(theme_bw())
x <- seq(-1, 1, .01)
qplot(x, dbetalu(x, 5, 5, -1, 1), geom = "line")

The distribution is the shifted and scaled version of the standard beta distribution with support [0,1], shown in red below:

qplot(x, dbetalu(x, 5, 5, -1, 1), geom = "line") +
  stat_function(fun = dbeta, args = list(shape1 = 5, shape2 = 5), color = "red")

The CDF can be evaluated with the pbetalu() function:

f <- function(x) dbetalu(x, 5, 5, -1, 1)
q <- -0.5
integrate(f, -1, q)
#  0.04892731 with absolute error < 5.4e-16
(p <- pbetalu(q, 5, 5, -1, 1))
#  [1] 0.04892731

The quantile function can be evaluated with qbetalu():

qbetalu(p, 5, 5, -1, 1) # = q
#  [1] -0.5

And random number generation can be performed with rbetalu():

set.seed(1)
rbetalu(5, 5, 5, -1, 1)
#  [1] -0.22368019  0.06553507 -0.29829009  0.56115084  0.11761102
samples <- rbetalu(1e5, 5, 5, -1, 1)
qplot(samples, bins = 30)

rbetalu() can be used to obtain a Monte Carlo estimate of the probability given by pbetalu() above:

mean(samples <= q)
#  [1] 0.04853

Moreover, we can check the consistency and correctness of the implementation with

qplot(samples, geom = "density") + 
  stat_function(fun = f,  color = "red")

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.