5. Probability: Frequentist (cont) and Bayesian

This session covers combining probabilities and the Bayesian view of probability as a degree of belief updated by evidence. The Python introduces conditional statements (if/elif/else) — how code makes decisions — along with controlling the display precision of numbers.


Calculating Probabilities

The probability of an outcome is the predicted relative frequency of that outcome, assuming the same situation could be repeated many times:

\[P(X = x) \approx \frac{\text{number of times } x \text{ occurs}}{\text{total number of trials}}\]

Probabilities can be theoretical (from reasoning about the structure of the experiment, e.g. a fair six-sided die gives P = 1/6 for each face) or experimental (from actual data). In science, experimental probabilities are more reliable but require sufficient data.


Combining Probabilities

When combining probabilities, the independence of events is often required. Two events A and B are statistically independent if and only if P(A and B) = P(A) · P(B).

AND (intersection): probability that both A and B occur. \[P(A \text{ and } B) = P(A) \cdot P(B) \quad \text{(for independent events)}\]

OR (union): probability that A, B, or both occur. \[P(A \text{ or } B) = P(A) + P(B) - P(A \text{ and } B)\]

For mutually exclusive events: \(P(A \text{ or } B) = P(A) + P(B)\)

Conditional probability: probability of A given that B has occurred. \[P(A \mid B) = \frac{P(A \text{ and } B)}{P(B)}\]

The independence assumption is frequently violated in experimental data. Some examples:

  • Two measurements of the same prepared sample on the same instrument on the same day share systematic sources of error and are not independent.
  • Replicates run by the same operator in the same session may be correlated.

Treating correlated measurements as independent inflates the apparent amount of information and can produce spuriously small p-values. This is one reason the distinction between technical replicates and independent replicates matters so much.


The Bayesian Approach

In the frequentist approach (Session 4), probability is the long-run frequency of an event. In the Bayesian approach, probability is a degree of belief — how strongly one believes a hypothesis to be true, given all available evidence.

Bayesian statistics provides a framework for updating beliefs in light of new data: starting from a prior estimate and producing a revised posterior estimate.

Brief history: Reverend Thomas Bayes formulated this approach in the mid-1700s. Pierre-Simon Laplace independently arrived at similar conclusions a few years later — a useful reminder of how slowly scientific ideas once spread.


Bayes’ Theorem

\[P(H \mid D) = \frac{P(D \mid H) \cdot P(H)}{P(D)}\]

Term Name Meaning
P(H | D) Posterior Probability of the hypothesis after seeing the data
P(H) Prior Probability of the hypothesis before seeing the data
P(D | H) Likelihood Probability of observing the data if the hypothesis is true
P(D) Marginal probability Total probability of the data across all hypotheses; normalizes the result

The posterior is proportional to the prior multiplied by the likelihood. When comparing two hypotheses, P(D) is the same for both and can often be set aside.


A Worked Example: Frequentist vs. Bayesian

Scenario: testing a water supply for a harmful contaminant.

  • Test sensitivity: 90% (correctly identifies contaminated samples)
  • Test specificity: 95% (correctly clears clean samples)
  • Prior information: the contaminant is present in ~1% of water samples from this region

The test returns positive. How should this be interpreted?

A frequentist reports the test statistics (sensitivity and specificity) and notes the result is positive, but does not naturally incorporate the 1% base rate.

A Bayesian calculates the actual probability that the positive test is truly a positive:

Out of 10000 samples from this region:

  • 100 are truly contaminated (1%)
  • 9900 are clean (99%)

Of the 100 contaminated: 90 test positive (true positives), 10 test negative (false negatives) Of the 9900 clean: 495 test positive (false positives, 5% of 9900), 9405 test negative

Total positive tests: 90 + 495 = 585

Probability that a positive result is truly a positive: 90 ÷ 585 ≈ 15%

Despite the test appearing reliable (90% sensitivity, 95% specificity), most positive results are false positives because the prior probability of contamination is very low. This is the base rate fallacy, and it has had serious real-world consequences in medical testing and legal proceedings.

The frequentist approach does not naturally surface this conclusion. The Bayesian approach requires you to state your prior and makes the result explicit.

Let’s consider how this might change in a region with much higher contamination prevalence: recalculate the posterior if

  • the base rate were 20% instead of 1%
  • the base rate were 50% instead of 1%

Comparing the Two Approaches

Frequentist Bayesian
Probability means Long-run frequency Degree of belief
Incorporates prior information No Yes
Output p-value, confidence interval Posterior probability distribution
Dominant in Most published literature Growing; especially in modeling and clinical trials

Bayesian statistics are increasingly used across all fields. However, the vast majority of published tests in chemistry and biology are still frequentist. This course focuses primarily on frequentist methods, while providing enough conceptual background to understand Bayesian results when you encounter them.


Other Approaches (for Awareness)

Likelihood approach: focuses on estimation rather than testing. Provides a middle ground between frequentist and Bayesian frameworks. Does not rely on p-values or arbitrary significance thresholds.

Information-theoretic approach: focuses on the information content of models. AIC and BIC — covered in Sessions 20 and 21 — are information-theoretic metrics. Uses relative model support rather than absolute significance.


A Caution on Misuse of Probability

Misunderstandings about how probabilities combine have had serious real-world consequences — courts have convicted individuals based on incorrectly combined probabilities where independence was assumed when events were correlated. The principle applies directly to science: always verify that the independence assumption is satisfied before multiplying probabilities together.


Making Decisions in Python: Conditional Statements

Bayesian reasoning updates a decision as evidence arrives; in code, decisions are made with conditional statements. This part covers if/elif/else, their use inside loops and comprehensions, and how to control the display precision of numerical output with f-strings.


Conditional Statements

Conditional statements execute code only when a condition is met.

if

if / else

if / elif / else

Warning

if/if/else vs if/elif/else: Using multiple if statements tests all conditions independently. Using elif stops at the first true condition. These give different results when conditions overlap.


Membership Testing

The in operator checks if an item is in a collection:


Conditionals in For Loops

Conditions can be placed inside loops to categorize data:

Counting with a loop:


Conditionals in List Comprehension


Python Operators

Comparison: ==, !=, >, >=, <, <=

Logic: and, or, not

Membership: in, not in

Identity: is, is not


Controlling Decimal Display with f-strings

To display a number to a specific number of decimal places, use :.Nf in an f-string:

This applies everywhere output is reported in this course. For example, reaction metrics:

Tip

Why :.Nf and not round()?

round(x, 2) changes the stored value to 2 decimal places — which can introduce floating-point rounding artefacts in subsequent calculations.

f"{x:.2f}" formats only the display. The full-precision value is retained internally, so downstream calculations are unaffected. Use :.Nf whenever you want to control what is shown, not what is stored.


Practical Example: Reaction Quality Assessment


TipKey takeaways
  • Combine probabilities: AND = × (independent events), OR = + − overlap — always check independence.
  • Bayesian probability = degree of belief; update prior → posterior with Bayes’ theorem.
  • Base rates matter: a ‘reliable’ test can still yield mostly false positives when prevalence is low.
  • This course is primarily frequentist; know enough Bayesian reasoning to read results.
  • In Python, if/elif/else make decisions; :.Nf controls display precision without changing the stored value.

In person session

TBD