How to create a distribution function in R?

Given the following function:

f(x) = (1/2*pi)(1/(1+x^2/4))

How to determine its distribution and write this distribution function in R?

+1
source share
2 answers

So, this is your function right now (I hope you know how to write the R function, and if not, check the spelling of your own function )

f <- function (x) (pi / 2) * (1 / (1 + 0.25 * x ^ 2))

fis defined on (-Inf, Inf), therefore integration on this range gives an indefinite integral. Fortunately, it approaches Infwith speed x ^ (-2), so the integral is well defined and can be calculated:

C <- integrate(f, -Inf, Inf)
# 9.869604 with absolute error < 1e-09

C <- C$value  ## extract integral value
# [1] 9.869604

Then you want to normalize f, since we know that probability density should integrate into 1:

f <- function (x) (pi / 2) * (1 / (1 + 0.25 * x ^ 2)) / C

You can draw its density:

curve(f, from = -10, to = 10)

density

+5

, , , say n = 1000 ?

, , , . , .

enter image description here

set.seed(0); range(simf(1000, 1e-2))
#[1] -56.37246  63.21080
set.seed(0); range(simf(1000, 1e-3))
#[1] -275.3465  595.3771
set.seed(0); range(simf(1000, 1e-4))
#[1] -450.0979 3758.2528
set.seed(0); range(simf(1000, 1e-5))
#[1] -480.5991 8017.3802

, e = 1e-2 . , () :

set.seed(0); x <- simf(1000)
hist(x, prob = TRUE, breaks = 50, ylim = c(0, 0.16))
curve(f, add = TRUE, col = 2, lwd = 2, n = 201)

enter image description here

+1

Source: https://habr.com/ru/post/1695131/


All Articles