liveBook · Manning

Uniform distribution

As explained in Machine Learning Algorithms in Depth

A uniform random variable has a constant probability density over a specified interval and zero density outside it.

A uniform random variable has constant probability density across a specified interval and zero density outside it. In Monte Carlo estimation, X and Y are sampled from Unif(−R, R). section 2.2

The distribution serves two distinct computational roles:

  • It supplies random points for Monte Carlo integration, where an indicator identifies samples inside a circle. section 2.2
  • It specifies error and scale hyperparameters in hierarchical Bayesian regression. section 6.3

Estimating π with uniform samples

Draw X and Y independently from Unif(−R, R), test whether X² + Y² < R², average the rectangle-area contributions, and divide by R². Increasing N reduces the standard error. section 2.2

Listing 2.1 Pi estimator
import numpy as np
import matplotlib.pyplot as plt

np.random.seed(42)     #1

def pi_est(radius=1, num_iter=int(1e4)):     #2


    X = np.random.uniform(-radius,+radius,num_iter)
    Y = np.random.uniform(-radius,+radius,num_iter)

    R2 = X**2 + Y**2
    inside = R2 < radius**2
    outside = ~inside

    samples = (2*radius)*(2*radius)*inside

    I_hat = np.mean(samples)
    pi_hat = I_hat/radius ** 2     #3
    pi_hat_se = np.std(samples)/np.sqrt(num_iter)    #4
    print("pi est: {} +/- {:f}".format(pi_hat, pi_hat_se))

    plt.figure()
    plt.scatter(X[inside],Y[inside], c='b', alpha=0.5)
    plt.scatter(X[outside],Y[outside], c='r', alpha=0.5)
    plt.show()

if __name__ == "__main__":

    pi_est()

The implementation shows NumPy uniform sampling, circle-membership tests, empirical integration, standard-error estimation, and accepted-versus-rejected sample plots. section 2.2

How Machine Learning Algorithms in Depth covers it

How deep it goes
Demonstrates sampling and model placement with implementations, but does not derive the distribution’s density or broader theory.
Examples
Uses a circle-area π estimator and uniform error and scale variables in a county-level radon regression.
What you can do after
Apply it via NumPy to generate bounded samples, estimate integrals, and visualize accepted and rejected points.
How it connects
Connects uniform distribution to Monte Carlo integration, indicator functions, hierarchical Bayesian regression, and posterior inference.

Also explained in

Compare how 9 books explain uniform distribution →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning Algorithms in Depth. The section links point to the parts of the book it draws on.