1

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) )

Ricardo Oliveros-Ramos
  • 4,322
  • 2
  • 25
  • 42
user3143784
  • 59
  • 1
  • 5
  • From what probability distribution were they sampled? – Simon O'Hanlon Dec 30 '13 at 16:42
  • 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 a different simulated number of N(0,2) ) – user3143784 Dec 30 '13 at 16:56
  • Do you have a list or a vector? Providing a small reproducible data set always helps. – Dason Dec 30 '13 at 17:01
  • 1
    Here is a link on how to make reproducible data in R http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – MCP_infiltrator Dec 30 '13 at 17:04

2 Answers2

2

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)
Sean
  • 21
  • 1
  • 1
    My favorite succinct coding for the scenario in @Sean's answer is `mean(mylist>=10 & mylist<=50)` – aosmith Dec 30 '13 at 17:58
1
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.

Roland
  • 127,288
  • 10
  • 191
  • 288
  • If you plan on calculating a lot of empirical probabilities this would be a good way to go - if you only plan on calculating one then aosmith's comment in Sean's answer is probably better. – Dason Dec 30 '13 at 19:13
  • How can we comment on the comparison between the probability which was calculated from the simulated numbers and the probability of the original distribution? – user3143784 Jan 02 '14 at 20:34
  • @user3143784 I don't really understand your comment. The theoretical and simulated results match quite nicely and if you increase the sample size they match even better. – Roland Jan 02 '14 at 21:11