Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Not sure on which SE site to ask this. It is essentially a math question however I am looking for a practical code solution.

I have a loop with an integer i which changes (according to a predefined set of rules) every time tails comes up in a biased coin toss. For the sake of simplicity, let's assume this number increments on every tails. The loop runs n times, or until heads comes up. If heads does not come up in n tosses, i = -n.

I am trying to find both the average value of i (not counting the case where i = -n), and the expected value.

Consider the following example C# code:

const int n = 10;      //loops n times
const double p = .25;  //probability of heads
int i = 1;             //variable number

Random rnd = new Random();

for (int j = 0; j < n; j++)
{
    if (rnd.NextDouble() < p) break;
    i++;
}
if (i > n) i = -n;

To get the average value I figured I'd have to calculate a weighted average, with weight w=p^(j+1), and simply run the loop n times.

const int n = 10;      //loops n times
const double p = .25;  //probability of heads
int i = 1;             //variable number
double i_avg = 0.0;    //average
double total_w = 0.0;  //weight

for (int j = 0; j < n; j++)
{
    double w = Math.Pow(p, j + 1.0);
    total_w += w;
    i_avg += i * w;
    i++;
}
i_avg /= total_w;

But this seems incorrect. On p = 0.25 and n = 10, the result is ~2.33. But in a simulation, I get an average value of ~3.40, and expected value of ~2.65.

What is the correct way to calculate these values? If possible, please include example code in a procedural programming language, as I find this easier to understand.

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

I think I figured it out.

The probability of heads coming up on the first toss is p. On the second toss, we've already hit tails, which has a probability of 1-p, so the prob. of heads would be (1-p)*p. On the third it would be (1-p)*(1-p)*p = (1-p)^2*p, etc. So for every toss, the probability of heads coming up is

w = (1-p)^j*p

To get the expected value, add -n to the sum with probability (1-p)^n.

ev = (i_avg + (1-p)^n * (-n)) / (total_w + (1-p)^n)

And since total_w + (1-p)^n = 1,

ev =  i_avg + (1-total_w) * (-n)

So the final program would be:

const int n = 10;      //loops n times
const double p = .25;  //probability of heads
int i = 1;             //variable number
double i_avg = 0.0;    //average
double ev = 0.0;       //expected value
double total_w = 0.0;  //weight

for (int j = 0; j < n; j++)
{
    double w = Math.Pow(1.0 - p, j) * p;
    total_w += w;
    i_avg += i * w;
    i++;
}
ev = i_avg + (1.0 - total_w) * (-n);
i_avg /= total_w;

I'd love to turn this into fancy Latex expressions but I have no idea how to do that.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.