Home » HOW TO: Implement Bayes’s Theorem in Power Apps

HOW TO: Implement Bayes’s Theorem in Power Apps

Bayes’s theorem is a mathematical concept that helps us update our beliefs about something when we are given new information. It allows us to calculate the probability of an event happening based on what we already know about it and any new information we receive.

In simple terms, Bayes’s theorem helps us adjust our initial assumptions or predictions in light of new evidence. For example, if you’re trying to determine whether it will rain tomorrow, you might start with an initial assumption that it has a 50/50 chance of raining. However, if you see a weather forecast that says there’s a high chance of rain, you can use Bayes’s theorem to update your initial assumption and calculate a new probability of it raining tomorrow based on this new information.

Overall, Bayes’s theorem is a powerful tool for making more accurate predictions and updating our beliefs based on new evidence. It has applications in a wide range of fields, including science, economics, and even artificial intelligence.

Most commonly, however, Bayes’s theorem is used in court for assessing the probability of guilt given the available evidence, in IT to assess the likelihood of an email being spam given its content and, in science to weigh up the results of scientific experiments against prior beliefs.

The scientific application is what we will be solving/using here.

Let’s assume…

A medical test is used to detect a disease. The test has a 98% true positive rate (P(PT|D) = 0.98), a 3% false positive rate, and the prevalence of the disease in the general population is 0.5% (P(D) = 0.005). Given that a person tests positive, what is the probability that they actually have the disease? Let’s find out…!

First, we need to calculate P(PT), the probability of a positive test. We can use the total probability rule for this:

P(PT) = P(PT|D) * P(D) + P(PT|~D) * P(~D)

Here, P(PT|~D) is the false positive rate, and P(~D) is the probability of not having the disease.

P(PT) = (0.98 * 0.005) + (0.03 * (1 – 0.005)) P(PT) ≈ 0.03085

Now, we can use Bayes’ Theorem to find P(D|PT), to find out the probability of having the disease given a positive test result.

Let’s do this in PowerApps!

  1. Open PowerApps and create a new canvas app.
  2. Add three Text Label’s and rename them “P(A)”, “P(B)” and “P(B|A) respectively.
  3. Add three Text Input fields and rename them “txtPA”, “txtPB” and “txtPBA” respectively.
  4. Change the Default property for each to “0.005”, “0.03475” and “0.98” respectively. These are just made up numbers from our original problem above, but you can change them based on any stats you have.
  5. Add a Button.
  6. Change the button’s OnSelect property to…
If(
    IsNumeric(txtPA.Text) && IsNumeric(txtPB.Text) && IsNumeric(txtPBA.Text),
    Set(
        P_A,
        Value(txtPA.Text)
    );
    Set(
        P_B,
        Value(txtPB.Text)
    );
    Set(
        P_B_A,
        Value(txtPBA.Text)
    );
    Set(
        P_A_B,
        (P_B_A * P_A) / P_B
    );
    Set(
        ResultText,
        Text(P_A_B, "[$-en-US]0.00%")
    )
)

6. Add another Text Label and change it’s Text property to the variable we created in the above, “ResultText”.

7. Give it a spin!

Here’s what we get…

Using our original figures, this shows that if a person tests positive, there is a 14% chance they actually have the disease, given the true positive rate, false positive rate and the prevalence of the disease in the general population. You will notice that prevalence is really important here. For example, if the prevalence of a disease is 0.05%, it means that out of every 10,000 individuals in the population, 5 of them have the disease.

Play around with the numbers and see what it shows.

See you soon!

PhilGlew-Deval

Back to top