Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate probability from density function

I've build density function and now I want to calculate the probability of a new data point to "fall" into selected interval (say, a=3, b=7). So, I'm looking for:

P(a<x<=b)

Some sample data:

df<- data.frame(x=c(sample(6:9, 50, replace=TRUE), sample(18:23, 25, replace=TRUE)))

dens<- density(df$x)

I'll be happy to hear of any solution, but preferably in base r

Thank you in advance

like image 937
staove7 Avatar asked Apr 23 '17 11:04

staove7


People also ask

How do you find the probability density of a distribution?

A distribution has a density function if and only if its cumulative distribution function F ( x) is absolutely continuous. In this case: F is almost everywhere differentiable, and its derivative can be used as probability density: d d x F ( x ) = f ( x ) . {\displaystyle {\frac {d} {dx}}F (x)=f (x).}

How to find the probability density function with Cuemath?

With Cuemath, find solutions in simple and easy steps. Find the probability density function for f (x) = x 2 + 2x for the interval [1, 3] and verify it using the probability density function calculator Find the probability density function for f (x) = 3x 2 - 4x for the interval [2, 4] and verify it using the probability density function calculator

What are the applications of the probability density function?

The following are the applications of the probability density function: 1 The probability density function is used in modelling the annual data of atmospheric NOx temporal concentration 2 It is used to model the diesel engine combustion 3 In Statistics, it is used to calculate the probabilities associated with the random variables.

Is the probability density function negative for all possible values?

The probability density function is non-negative for all the possible values, i.e. f (x)≥ 0, for all x. Due to the property of continuous random variables, the density function curve is continued for all over the given range.


1 Answers

You need to get the density as a function (using approxfun)and then integrate the function over the desired limits.

integrate(approxfun(dens), lower=3, upper=7)
0.258064 with absolute error < 3.7e-05

## Consistency check
integrate(approxfun(dens), lower=0, upper=30)
0.9996092 with absolute error < 1.8e-05
like image 161
G5W Avatar answered Oct 31 '22 03:10

G5W