1

I have data available for the last days. Each day my table grows by one record. I want to calculate the probability that an event occurs once in the future 7 days.

I use the binomial distribution so far, because it is only about "Yes / No" or "Success / Failure". The probability for success is $p = 1/7$.

How do I use the formula correctly? I am not so sure if I can just add +7 to the total number of experiments ($n$).

$$ B(k \mid p, n) = \binom{n}{k}\cdot p^{k}\cdot (1-p)^{n-k} $$ where $n \; \hat=$ total number of trials / experiments; $k \; \hat=$ number of successes; $p \; \hat=$ probability

Class in C#

internal sealed class ProbabilityCalculator
    {
        internal double BinomialDistribution(ulong numberOfSuccesses, ulong experiments, double probability)
        {
            if (probability < 0.0 || probability > 1.0) { return 0.0; }
        return BinomialCoefficient(experiments, numberOfSuccesses) * Math.Pow(probability, numberOfSuccesses) * Math.Pow(1.0 - probability, experiments - numberOfSuccesses);
    }

    private static ulong BinomialCoefficient(ulong n, ulong k)
    {
        return Factorial(n) / (Factorial(k) * Factorial(n - k));
    }

    private static ulong Factorial(ulong x)
    {
        if (x &gt;= 2ul) return x * Factorial(x - 1ul);
        return 1ul;
    }

}

Calling the function

ulong totalNumberOfExperiments = (ulong)allData.Count;
ulong numberOfSuccesses = (ulong)allData.Where(d => d.aBool).ToList().Count();
double currentProbability = (double)numberOfSuccesses / (double)totalNumberOfExperiments;
ProbabilityCalculator pC = new ProbabilityCalculator();
double expectedProb = pC.BinomialDistribution(numberOfSuccesses, totalNumberOfExperiments, currentProbability);

where allData is a List with instances of a class.

Nerrit
  • 402
Daniel
  • 111
  • 1
    I am confused: You know that on each day a given event $X$ occurs with probability $p=\frac 17$, the days are independent, and you want to know the probability that in the next $7$ days, the event $X$ occurs on at least one day. Is that correct? – Maximilian Janisch Jan 08 '23 at 14:52
  • @MaximilianJanisch That's correct – Daniel Jan 08 '23 at 14:57
  • 1
    You are looking for $\mathbb P(Y\ge 1)$, where $Y\sim\text{Binomial}(n=7,p=1/7)$, and this is $$\mathbb P(Y\ge 1)= 1 - \mathbb P(Y=0) = 1 - (1-p)^n = 1 - \left(\frac 67\right)^7 = \frac{543607}{823543} \approx 66%.$$ – Maximilian Janisch Jan 09 '23 at 11:11
  • @MaximilianJanisch Thank you for the solution. I feel like I have to ask — What does Y ≥ 1 ? mean? Y is a random (discrete) variable. – Daniel Jan 09 '23 at 22:00
  • 1
    Yes, $Y$ is a discrete random variable modeling the number of days on which the event $X$ occurs within the next $7$ days. Then $Y\ge 1$ means that $X$ happens on at least one day. – Maximilian Janisch Jan 10 '23 at 00:25

0 Answers0