Python

Statistics

A comprehensive guide to performing meta-analysis in Python

Probability Basics

  • Probability of an Event:
P(event)=# of ways event can happentotal # of possible outcomesP(\text{event}) = \frac{\text{\# of ways event can happen}}{\text{total \# of possible outcomes}}
  • Example: Probability of heads in a coin flip = 12=0.5\frac{1}{2} = 0.5

  • Independent Events: Probability of the second event isn't affected by the first.

    • Sampling with replacement = events are independent.
  • Dependent Events: Probability of the second event is affected by the first.

    • Sampling without replacement = events are dependent.

Sampling in Python

  • Random Sampling: DataFrame.sample()
    sales_counts.sample()
  • Setting a Seed: Ensures reproducibility.
    np.random.seed(10)

Discrete Distributions

  • Probability Distribution: Describes probability of each outcome.
    • Expected value of a fair die roll:
Expected Value=(1×16)+(2×16)++(6×16)=3.5 \text{Expected Value} = (1 \times \frac{1}{6}) + (2 \times \frac{1}{6}) + \dots + (6 \times \frac{1}{6}) = 3.5
  • Discrete Uniform Distribution:
    • Fair Die: Equal probability for each outcome.
    • Uneven Die: Different probabilities for each outcome.

Continuous Distributions

  • Continuous Uniform Distribution: Equal probability across a continuous range.

    • Example: Waiting time for a bus between 0 and 12 minutes.
  • Area Under Curve = Probability:

    P(4wait time7)=3×112=0.25P(4 \leq \text{wait time} \leq 7) = 3 \times \frac{1}{12} = 0.25

Using Scipy for Distributions

  • Uniform Distribution:

    from scipy.stats import uniform
    uniform.cdf(value, min, max)
    • Probability of a value less than or equal to 7: uniform.cdf(7, 0, 12)
  • Binomial Distribution:

    • Binary Outcomes: Success (1) or Failure (0)
    • Single Flip:
      from scipy.stats import binom
      binom.rvs(1, 0.5, size=1)  # Result of one flip
    • Many Flips, One Time:
      binom.rvs(num_trials, prob_success, size=1)
    • Probability of k Successes:
      binom.pmf(k, n, p)

Key Terms

  • Expected Value (Binomial): E=n×pE = n \times p
  • Law of Large Numbers: As sample size grows, the sample mean approaches the expected value.

Example Code

  • Generating Random Numbers (Uniform Distribution):
    uniform.rvs(min, range, size=sample_size)
  • Rolling a Die with Probability:
    die.sample(10, replace=True)

Last updated on

On this page