Estimate a probability from elements of a list in

2020-08-01 04:38发布

问题:

I have a list of 100,000 simulated numbers of T in R (min: 1.5, max 88.8) and I want to calculate the probability of T being between 10 and 50.

I sumulated 100,000 numbers of T, where T is t(y) %*% M %*% y where M is a 8x8 matrix of constant values and y is a 8x1 matrix. The element in the i-th row if y, is equal to: a_i + b_i where a is a vector of constants and b is a vector whose elements follow a normal (0,sd=2) distribution (each element is a different simulated number of N(0,2) )

回答1:

Is it in a vector or a list? If it's a vector, the following should work. If it's in a list, you may use unlist() to convert it to a vector.

mylist <- runif(100000,1.5,88.8) #this is just to generate a random number vector 
length(which(mylist>=10 & mylist<=50))/length(mylist)


回答2:

set.seed(42)
myrandoms <- rnorm(100000, mean=5, sd=2)
mydistr <- ecdf(myrandoms)

#probability of beeing between 1 and 3:
diff(mydistr(c(1, 3)))
#[1] 0.13781

#compare with normal distribution
diff(pnorm(c(1, 3), mean=5, sd=2))
#[1] 0.1359051

If you really have a list, use myrandoms <- do.call(c, mylist) to make it a vector.



标签: r probability