MyNixOS website logo
Description

The Chi Distribution.

Light weight implementation of the standard distribution functions for the chi distribution, wrapping those for the chi-squared distribution in the stats package.

chi

chi implements the (d/p/q/r) statistics functions for the chi distribution in R. It is ideal for using in other packages since it is lightweight and leverages the (d/p/q/r)chisq() line of functions maintained by CRAN.

Getting chi

There are two ways to get chi. For the CRAN version, use

install.packages("chi")

For the development version, use

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

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

The defining property of the chi distribution is that it is the square root of a chi random variable.

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

library(chi)
library(ggplot2); theme_set(theme_bw())
x <- seq(0, 6, .01)
qplot(x, dchi(x, 7), geom = "line")

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

f <- function(x) dchi(x, 7)
q <- 2
integrate(f, 0, q)
#  0.2202226 with absolute error < 2.4e-15
(p <- pchi(q, 7))
#  [1] 0.2202226

The quantile function can be evaluated with qchi():

qchi(p, 7) # = q
#  [1] 2

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

set.seed(1)
rchi(5, 7)
#  [1] 2.006520 3.389800 3.349233 2.742686 3.907928

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

samples <- rchi(1e5, 7)
mean(samples <= q)
#  [1] 0.22172

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

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

Related packages

The Runuran package also implements a sampler from the chi distribution, using the function Runuran::urchi(). (It also provides udchi(), but not upchi() or uqchi().) Here's a comparison of how fast the samplers are, with the punch line being that Runuran is about 3x faster for larger numbers of samples, whereas chi is much faster for smaller samples (e.g. 100 or 1000).

library(Runuran)
library(microbenchmark)

# chi::rchi() is much faster for small datasets
microbenchmark(
  urchi(1e3, 5),
  rchi(1e3, 5)
)
#  Unit: microseconds
#             expr     min       lq     mean   median      uq      max neval
#   urchi(1000, 5) 784.582 808.7745 911.0466 838.4780 929.167 1859.345   100
#    rchi(1000, 5) 109.266 125.1120 143.1345 131.0915 145.558  333.781   100
#   cld
#     b
#    a

# Runuran::urchi is ~3x faster for larger datasets
microbenchmark(
  urchi(1e5, 5),
  rchi(1e5, 5)
)
#  Unit: milliseconds
#              expr       min        lq      mean    median        uq
#   urchi(1e+05, 5)  4.077995  4.317686  4.765376  4.512885  4.954472
#    rchi(1e+05, 5) 11.912304 12.291699 13.001405 12.669712 13.460673
#         max neval cld
#    6.763741   100  a 
#   15.766955   100   b
Metadata

Version

0.1

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows