8

I'm trying to write a function that will return true or false each time it's called, but with a known frequency, say 60% of the time it'll be true, the other 40% it'll be false. Using that function I'm to create another function that returns true 50% of the time.

My initial approach was to use the random function, and return true if its under 0.6, false if it's over. Not sure how to approach the second part using that.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
eric
  • 81
  • 3
  • Look at the four cases (first output, second output): (1)True True (2) True False (3)False True (4) False False. To get True on the second output, you need to sum the probability of the cases (True True) and (False True). To get False on the second output, you need to sum the probability of (True False) and (False False) – systemizer May 14 '11 at 18:25
  • 1
    That seems unnecessarily complicated. Why not use random() directly to implement both? –  May 14 '11 at 18:28
  • @Neil I thought that at the beginning, but it seems it's a homework task to compose a function using the first one. – sawa May 14 '11 at 18:31
  • @systemizer your method still yields .6 probability for True and .4 for False, it doesn't solve the problem.. – Mihai Oprea May 14 '11 at 18:32
  • 3
    Ooh, I hate artificial problems - the world is quite full enough of real ones, as far as I can see. –  May 14 '11 at 18:34
  • @MihaiOprea I wasn't giving a solution, I was giving a way to find the solution. It's a basic apriori problem. Draw out a probability tree with the situations I suggested and the find the probability of each case. Then sum the ones that yield True in the secound output to determine the final True probability. It would be easier to explain with a paper and pencil :( – systemizer May 14 '11 at 18:37
  • 3
    @eric: I would suggest you ignore Neil's comment about artificial problem. Just because one cannot think of any applications, does not mean there aren't any. Unfortunately, you will find many such (yes, I said it: narrow minded) comments around on this site. I hope you don't let such comments discourage you. And btw, welcome to this site. –  May 14 '11 at 19:22
  • 3
    This is not an artificial problem at all. This is a real problem. Usually I've seen it presented with P NOT being known; I imagine for some specific values of P one could cause faster convergence. I think the most obvious real-world application for this is to generate evenly distributed bits given a source of "truly random" data (such as background radiowaves or whatever). Basically, it's a way to filter out junk. – Brian May 14 '11 at 21:44
  • possible duplicate of [An interview question: About Probability.](http://stackoverflow.com/questions/5051970/an-interview-question-about-probability) – Sjoerd C. de Vries Jun 25 '11 at 21:47

4 Answers4

12

Let's take the general case: you built a function F1() that returns True with probability P (in your case, P=60%). now you build the second function this way:

F2():
   result1 = F1()
   result2 = F1()
   if result1 = True and result2 = False: return True
   elif result1 = False and result2 = True: return False
   else:  F2()

In this case, the probability of running F1 twice and obtaining (True,False) is the same as obtaining (False,True) and it's P * (1-P). Instead, if you get either (True,True) or (False,False) you call F2 recursively. This means, that after running F2 you always obtain True or False with probability 1/2 since the first two branches have equal probabilities, and the third will always give you the result of a function with 1/2 probability.

I am making this a community wiki in case someone wants to make my answer more clear. I realize it might be a little hard to explain the concept.

The average number of calls

The probability that the function F2() terminates right after n recursive calls is:

{(1-P)^2+P^2}^n*2P(1-P)

Therefore, the average number of recursive calls required is:

\Sum_{i=0}^\infty i*{(1-P)^2+P^2}^i*2P(1-P)

sawa
  • 165,429
  • 45
  • 277
  • 381
Mihai Oprea
  • 2,051
  • 3
  • 21
  • 39
  • cool, don't forget to accept if you feel it answers your question :) – Mihai Oprea May 14 '11 at 19:34
  • It's cool, but it might not converge. What is the average number of recursive calls? – sawa May 14 '11 at 19:37
  • 1
    @sawa: Probability of getting TF or FT is 6/25. So the expected number of calls is 25/6. –  May 14 '11 at 19:38
  • @sawa: it's a probability & math question, the case where it doesn't converge doesn't really matter. in reality you would never need such a construction of functions – Mihai Oprea May 14 '11 at 19:43
  • @Mihai: Not really. This has practical applications. How do you think you can generate a random number between [1,n] given a source of unbiased bits? The ideas are similar, just in the opposite direction. –  May 14 '11 at 19:57
  • @Aryaghatta I don't get your calculation leading to 25/6. – sawa May 14 '11 at 20:07
  • 1
    I added the formula for getting the average calls. Can someone continue the calculation? – sawa May 14 '11 at 20:14
  • 1
    @sawa: If probability of a heads occuring is p, then the expected number of tosses before we see heads is 1/p. This is a well known fact and can be proved using the infinite series for expectation. Another proof appears here: http://en.wikipedia.org/wiki/Expected_value#Discrete_distribution_taking_only_non-negative_integer_values –  May 14 '11 at 20:51
  • 1
    When this is applied to the real world, often F1 has some finite backing stream of bits. In that scenario, a common improvement to reduce the rate at which F1's underlying data is consumed is to append result1 to the back of F1's underlying bitstream in the case that result1 and result2 are equal. – Brian May 14 '11 at 21:49
0

Okay so we have some function that generates a True value with p = 0.60 and a False value with p = 0.40 . Suppose for a second we run this function twice? What is the probability that the results will be the same.

so F_1 == true and F_2 == true happens with p( 0.60 * 0.60 ) = 0.36

and F_1 == false and F_2 == false happens with p( 0.40 * 0.40 ) = 0.16

and F_1 != F_2 happens with p( 0.6 * 0.4 + 0.6 * 0.4 ) = 0.48

that will give you a function with returns true 0.52 percent of the time, which isn't quite what we are looking for but is interesting.

A much simple solution would be to say: If I have true 60 % of the time, then what percent of the time ( which what probability ) should I change that value to false in order to be true 50 % of the time.

That's all I'll say because this is homework.

zellio
  • 31,308
  • 1
  • 42
  • 61
0

Approach:

You run your 40/60 function until you hit result that returns 40% probability. Once this happens you calculate result.

0.5 is calculated this way.

0.5 = 0.6 - 0.6^2 + 0.6^3 + 0.6^4 - 0.6^5 - 0.6^6 + 0.6^7 + 0.6^8 - 0.6^9 + ....
(if current result is more than 0.5 you add else you substract)

Once you hit 0.4 you calculate result of this function. If it is > 0.5 you return true, else you return false.

Luka Rahne
  • 10,336
  • 3
  • 34
  • 56
0

hey how about this approach: We know that calling F1() 10 times: 6 times it'll return true and 4 times false

So in F2() we can have a counter that counts to 5. With every call to F1() we increment the counter. F2() returns what F1() returns. When the counter reaches 5 F2() returns "false" and re-initializes the counter. What say?