Probability on future days (forecasting)

49 Views Asked by At

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 >= 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.