MxLDevs
MxLDevs

Reputation: 19546

Calculating the expected probability that an expression resolves to True

Suppose I have a simple program that simulates a coin toss, with a given probability specified by an expression. It might look something like this:

# This is the probability that you will get heads.
$expr = "rand < 0.5"

def get_result(expr)
   eval(expr)
end

def toss_coin
  if get_result($expr)
     return "Head"
  else
     return "Tail"
  end
end

Now, I also want to tell the user what the probability of getting Head is.

For the given expression

"rand < 0.5"

We can eye-ball it and say the probability is 50%, because rand returns a number between 0 and 1, and therefore the expression evaluates to true 50% of the time on average.

However, if I decided to provide a rigged coin toss where the expression used to determine the outcome is

"rand < 0.3"

Now, I have a 30% chance of getting Head.

Is it possible to write a method that will take an arbitrary expression (that evaluates to a boolean!) and return the probability that it resolves to true?

def get_expected_probability(expr)
  # Returns the probability the `expr` returns true
  #    `rand < 0.5` would return 0.5
  #    `rand < 0.3` would return 0.3
  #    `true` would return 1
  #    `false` would return 0
end

Upvotes: 0

Views: 436

Answers (3)

sunnyrjuneja
sunnyrjuneja

Reputation: 6123

An incredibly slow way of approximating this would be to evaluate the expression a very large number of times and estimate the probability it converges to. The Law of Large Numbers guarantees that as n approaches infinity, it will be that probability.

$expr = "rand < 0.5"

def get_result(expr)
  eval(expr)
end

n = 1000000
a = Array.new(n)

n.times do |i|
  a[i] = eval($expr)
end

puts a.count(true)/n.to_f

Returned 0.499899 for me.

Upvotes: 1

pjs
pjs

Reputation: 19855

For simple comparisons to a uniform random number, yes, but in general, no. It depends on the distribution of the expression you're using to determine your boolean, and you could write arbitrarily complex expressions with bizarre distributions. However, it's pretty straightforward to estimate the probability empirically.

Create a Bernoulli (0/1) outcome based on the expression, yielding 1 when the expression is true and 0 when it is false. Generate a large number (n) of them. The long run average of the Bernoulli outcomes will converge to the probability of getting a true. If you call that p-hat and the true value is p, then p-hat should fall within the range p +/- (1.96 * sqrt(p*(1-p)/n)) 95% of the time. You can see from this that the larger the sample size n is, the more precise your estimate is.

Upvotes: 1

Peter Alfvin
Peter Alfvin

Reputation: 29419

My guess would be that it would be theoreticially possible to write such a method, assuming you restricted yourself to rand and deterministic mathematical functions and had complete knowledge of the systems floating point implementation, etc.

It would be much more straightforward, however, to approximate the probability by executing the expression a large number of times and keeping track of the percentage of times it succeeded.

Upvotes: 1

Related Questions