π² Controlled Stochasticity: The Deterministic Backbone of Machine Learning
Machine learning and statistical inference rely heavily on randomness: parameter weight initializations in deep neural networks, random feature sub-sampling in Random Forests, train-validation splitting, data augmentation jitter, and stochastic gradient updates. However, unmanaged randomness introduces severe testing non-determinism, unreproducible bugs, and inconsistent research claims. True computational randomness is generated by deterministic Pseudo-Random Number Generators (PRNGs). Transitioning from legacy global-state engines (np.random.seed) to the modern object-oriented np.random.default_rng() API backed by PCG64 ensures statistical cryptographic robustness, eliminates thread contention across parallel workers via SeedSequence stream spawning, enables streaming reservoir sampling over unbounded data feeds, and powers numerical Monte Carlo simulations for complex non-analytic systems.
LEGACY PARADIGM: np.random.seed() (Stateful, Shared Global State):
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β GLOBAL RUNTIME STATE: Mersenne Twister MT19937 (2,496-byte massive state) β
β * Single global lock: Thread-unsafe in multi-worker parallelism β
β * Poor statistical properties in high dimensions (Fails BigCrush tests) β
β * Modifying seed in one library alters randomness across entire process! β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
MODERN PARADIGM: np.random.default_rng() (Decoupled, Thread-Safe Streams):
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 1. BITGENERATOR (Low-Level Raw Bit Producer) β
β * Engine: PCG64 (Permuted Congruential Generator - 128-bit state) β
β * Passes TestU01 BigCrush test suite; smaller 32-byte cache-local state β
β * Alternatives: PCG64DXSM, Philox (GPU/SIMD), SFC64 β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ (Provides 64-bit unsigned integers)
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 2. GENERATOR (High-Level Statistical Distribution Engine) β
β * Converts raw bits into probability distributions (Normal, Poisson, ...) β
β * Instantiated via: rng = np.random.default_rng(seed) β
β * Isolated instances: Zero cross-thread contention or global collisions β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ (Deterministic Worker Splitting)
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 3. SEEDSEQUENCE (Independent Child Stream Generation) β
β * Spawns non-overlapping independent RNG streams for parallel processes β
β * Mathematically avoids collision risk without manual seed guessing β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
default_rng() generator against the legacy np.random.seed() interface.
import time
import numpy as np
N = 10000000 # 10 Million Float64 Draws
# 1. Legacy Approach: Global Mersenne Twister Engine
np.random.seed(42)
t0 = time.perf_counter()
legacy_draws = np.random.standard_normal(N)
t_legacy = time.perf_counter() - t0
# 2. Modern Approach: Decoupled PCG64 Generator Instance
rng = np.random.default_rng(seed=42)
t0 = time.perf_counter()
modern_draws = rng.standard_normal(N)
t_modern = time.perf_counter() - t0
print("=" * 65)
print(f"PRNG GENERATOR BENCHMARK ({N:,} Standard Normal Draws)")
print("=" * 65)
print(f"Legacy MT19937 Engine Time : {t_legacy * 1000:6.2f} ms")
print(f"Modern PCG64 Generator Time : {t_modern * 1000:6.2f} ms")
print(f"Throughput Acceleration : {t_legacy / t_modern:6.2f}x Faster!")
print("-" * 65)
print(f"Modern Generator Type : {type(rng)}")
print(f"Underlying BitGenerator : {type(rng.bit_generator)}")
print(f"BitGenerator State (PCG64) : State keys = {list(rng.bit_generator.state.keys())}")
import numpy as np
def split_dataset_reproducible(X: np.ndarray, y: np.ndarray,
train_ratio: float = 0.70,
val_ratio: float = 0.15,
seed: int = 42) -> tuple:
"""
Splits features X and targets y deterministically into train, val, and test subsets.
"""
assert len(X) == len(y), "Features X and targets y must have identical length."
n_samples = len(X)
# Instantiate isolated local RNG (Does not mutate external global seeds)
rng = np.random.default_rng(seed)
# Generate shuffled index permutation vector: [0, n_samples - 1]
shuffled_indices = rng.permutation(n_samples)
# Compute partition boundary thresholds
train_end = int(n_samples * train_ratio)
val_end = train_end + int(n_samples * val_ratio)
train_idx = shuffled_indices[:train_end]
val_idx = shuffled_indices[train_end:val_end]
test_idx = shuffled_indices[val_end:]
return (X[train_idx], y[train_idx]), (X[val_idx], y[val_idx]), (X[test_idx], y[test_idx])
# Generate synthetic credit evaluation dataset: 1,000 samples, 4 features
X_raw = np.linspace(10, 100, 1000).reshape(1000, 1) * np.array([1.0, 2.0, 0.5, 3.2])
y_raw = np.where(X_raw[:, 0] > 50, 1, 0)
(X_tr1, y_tr1), (X_v1, y_v1), (X_te1, y_te1) = split_dataset_reproducible(X_raw, y_raw, seed=101)
(X_tr2, y_tr2), (X_v2, y_v2), (X_te2, y_te2) = split_dataset_reproducible(X_raw, y_raw, seed=101)
print("=" * 65)
print("REPRODUCIBLE DATASET PARTITIONING AUDIT")
print("=" * 65)
print(f"Total Dataset Rows : {len(X_raw)}")
print(f"Training Subset Shape : {X_tr1.shape} (70%)")
print(f"Validation Subset Shape : {X_v1.shape} (15%)")
print(f"Testing Subset Shape : {X_te1.shape} (15%)")
print("-" * 65)
print(f"Deterministic Consistency Across Runs: {np.array_equal(X_tr1, X_tr2)}")
print(f"Sample First 3 Training Targets Run 1 : {y_tr1[:3]}")
print(f"Sample First 3 Training Targets Run 2 : {y_tr2[:3]}")
seed=0, seed=1, seed=2) across parallel threads causes statistical stream overlap. SeedSequence.spawn() creates mathematically independent random streams for distributed processes.
import concurrent.futures
import numpy as np
def worker_simulation_task(worker_id: int, child_seed: np.random.SeedSequence) -> dict:
"""
Isolated worker process executing stochastic simulations with an independent RNG.
"""
# Initialize Generator from unique child SeedSequence
local_rng = np.random.default_rng(child_seed)
# Simulate 500,000 Monte Carlo trial draws
draws = local_rng.uniform(low=0.0, high=1.0, size=500000)
empirical_mean = float(np.mean(draws))
return {
"worker_id": worker_id,
"sample_size": len(draws),
"empirical_mean": round(empirical_mean, 5),
"deviation_from_half": round(abs(empirical_mean - 0.5), 5)
}
# 1. Master Entropy Source
MASTER_ENTROPY = 9921448102
root_seed_seq = np.random.SeedSequence(MASTER_ENTROPY)
# 2. Spawn 4 statistically guaranteed independent child streams
NUM_WORKERS = 4
child_seed_sequences = root_seed_seq.spawn(NUM_WORKERS)
# 3. Parallel Execution across ThreadPool / ProcessPool
print("=" * 65)
print("MULTI-WORKER INDEPENDENT STREAM SPAWNING")
print("=" * 65)
print(f"Root SeedSequence Entropy: {root_seed_seq.entropy}")
print(f"Spawned Child Streams : {len(child_seed_sequences)} non-overlapping pipelines\n")
with concurrent.futures.ThreadPoolExecutor(max_workers=NUM_WORKERS) as executor:
futures = [
executor.submit(worker_simulation_task, wid, c_seed)
for wid, c_seed in enumerate(child_seed_sequences)
]
for f in concurrent.futures.as_completed(futures):
res = f.result()
print(f"Worker {res['worker_id']} (N={res['sample_size']:,}) βββΊ Mean: {res['empirical_mean']} (Delta: {res['deviation_from_half']})")
import numpy as np
from typing import Iterator
def streaming_iot_sensor_feed(total_records: int) -> Iterator[dict]:
"""Generates an unbounded generator stream of sensor log dictionaries."""
for idx in range(total_records):
yield {"log_id": f"LOG-{idx:07d}", "temperature": round(20.0 + (idx % 15) * 0.4, 2)}
def reservoir_sample_stream(stream: Iterator[dict], k_capacity: int, seed: int = 42) -> list:
"""
Reservoir Sampling (Algorithm R): Maintains a representative sample of size k
from a stream of unknown length N in O(k) memory space.
"""
rng = np.random.default_rng(seed)
reservoir = []
for item_idx, item in enumerate(stream):
if item_idx < k_capacity:
# Fill the reservoir buffer initially
reservoir.append(item)
else:
# Generate random integer j in [0, item_idx]
j = rng.integers(0, item_idx + 1)
# If j falls within the reservoir capacity, replace element at j
if j < k_capacity:
reservoir[j] = item
return reservoir
STREAM_TOTAL = 500000 # 500,000 Streaming Events
RESERVOIR_SIZE = 5
stream_generator = streaming_iot_sensor_feed(STREAM_TOTAL)
sampled_records = reservoir_sample_stream(stream_generator, k_capacity=RESERVOIR_SIZE, seed=42)
print("=" * 65)
print(f"RESERVOIR STREAM SAMPLING (Sample k={RESERVOIR_SIZE} from N={STREAM_TOTAL:,})")
print("=" * 65)
print(f"Target Selection Probability per Item: {RESERVOIR_SIZE / STREAM_TOTAL:.2e}")
print("-" * 65)
print("Uniformly Sampled Items Retained in Memory:")
for sample in sampled_records:
print(f" Captured Log: {sample['log_id']} | Temperature: {sample['temperature']} Β°C")
import numpy as np
def stratified_train_test_split(labels: np.ndarray, test_ratio: float = 0.20, seed: int = 42) -> tuple:
"""
Performs stratified sampling preserving exact class ratios in splits.
"""
rng = np.random.default_rng(seed)
unique_classes, class_counts = np.unique(labels, return_counts=True)
train_indices = []
test_indices = []
for c in unique_classes:
# Find all index positions belonging to class c
c_indices = np.where(labels == c)[0]
# Shuffle indices for this class
shuffled_c = rng.permutation(c_indices)
n_test = int(np.round(len(c_indices) * test_ratio))
test_indices.extend(shuffled_c[:n_test])
train_indices.extend(shuffled_c[n_test:])
return np.array(train_indices), np.array(test_indices)
# Imbalanced fraud dataset: 9,800 Legitimate (0) vs. 200 Fraud (1) [2% Fraud Rate]
y_imbalanced = np.array([0] * 9800 + [1] * 200)
train_idx, test_idx = stratified_train_test_split(y_imbalanced, test_ratio=0.20, seed=42)
orig_fraud_rate = np.mean(y_imbalanced == 1) * 100
train_fraud_rate = np.mean(y_imbalanced[train_idx] == 1) * 100
test_fraud_rate = np.mean(y_imbalanced[test_idx] == 1) * 100
print("=" * 65)
print("STRATIFIED CLASS PROPORTION AUDIT (N=10,000)")
print("=" * 65)
print(f"Original Dataset Fraud Ratio : {orig_fraud_rate:.2f}% (200 of 10,000)")
print(f"Training Subset Fraud Ratio : {train_fraud_rate:.2f}% (160 of 8,000)")
print(f"Testing Subset Fraud Ratio : {test_fraud_rate:.2f}% (40 of 2,000)")
print("-" * 65)
print(f"Class Distribution Invariance : {np.isclose(train_fraud_rate, test_fraud_rate)} (Zero Partition Drift)")
import numpy as np
def monte_carlo_estimate_pi(num_samples: int, seed: int = 42) -> tuple:
"""
Estimates the value of Pi by computing the area of a quarter circle
inscribed in a unit square: Integral of sqrt(1 - x^2) from 0 to 1 = Pi / 4.
"""
rng = np.random.default_rng(seed)
# Sample uniform points in 2D unit square: [0, 1) x [0, 1)
x = rng.uniform(0.0, 1.0, num_samples)
y = rng.uniform(0.0, 1.0, num_samples)
# Condition: x^2 + y^2 <= 1 (Point falls inside circle)
inside_circle = (x**2 + y**2) <= 1.0
# Area ratio: Count(inside) / Total = Pi / 4
pi_estimate = (np.sum(inside_circle) / num_samples) * 4.0
abs_error = abs(pi_estimate - np.pi)
return pi_estimate, abs_error
sample_sizes = [1000, 10000, 100000, 1000000, 10000000]
print("=" * 65)
print("MONTE CARLO NUMERICAL INTEGRATION CONVERGENCE")
print("=" * 65)
print(f"Target Constant (True Ο) : {np.pi:.7f}\n")
for n in sample_sizes:
est_pi, err = monte_carlo_estimate_pi(n, seed=42)
print(f"Samples: {n:10,d} βββΊ Estimated Ο: {est_pi:.6f} | Absolute Error: {err:.6f}")
import numpy as np
# Portfolio Parameters
INITIAL_PORTFOLIO_INR = 10000000.0 # βΉ 1 Crore Portfolio
ANNUAL_RETURN = 0.12 # 12% Expected Return (drift ΞΌ)
ANNUAL_VOLATILITY = 0.22 # 22% Volatility (Ο)
TIME_HORIZON_DAYS = 21 # 1 Trading Month (21 days)
NUM_SIMULATIONS = 100000 # 100,000 Simulated Futures
# Daily calibration
dt = TIME_HORIZON_DAYS / 252.0 # Fraction of trading year
mu_daily = ANNUAL_RETURN * dt
sigma_daily = ANNUAL_VOLATILITY * np.sqrt(dt)
# Geometric Brownian Motion terminal price formulation:
# S_T = S_0 * exp( (ΞΌ - 0.5*Ο^2)*dt + Ο*sqrt(dt)*Z )
rng = np.random.default_rng(seed=42)
Z = rng.standard_normal(NUM_SIMULATIONS)
simulated_portfolio_values = INITIAL_PORTFOLIO_INR * np.exp(
(ANNUAL_RETURN - 0.5 * (ANNUAL_VOLATILITY**2)) * dt +
ANNUAL_VOLATILITY * np.sqrt(dt) * Z
)
# Compute P&L dollar distribution
pnl = simulated_portfolio_values - INITIAL_PORTFOLIO_INR
# Value at Risk (VaR) at 99% Confidence Level (1st percentile of PnL)
var_99_loss = -np.percentile(pnl, 1)
# Conditional Value at Risk (CVaR / Expected Shortfall): Mean loss beyond VaR cutoff
cvar_99_loss = -np.mean(pnl[pnl <= -var_99_loss])
print("=" * 65)
print("FINANCIAL PORTFOLIO RISK: 99% MONTE CARLO VAR (21-DAY HORIZON)")
print("=" * 65)
print(f"Initial Portfolio Capital : βΉ {INITIAL_PORTFOLIO_INR:,.2f}")
print(f"Simulated Trajectories : {NUM_SIMULATIONS:,} Market Scenarios")
print("-" * 65)
print(f"Median Projected Outcome : βΉ {np.median(simulated_portfolio_values):,.2f}")
print(f"Worst Scenario (Min PnL) : βΉ {np.min(pnl):,.2f}")
print(f"Best Scenario (Max PnL) : βΉ {np.max(pnl):,.2f}")
print("-" * 65)
print(f"99% Value-at-Risk (VaR) : βΉ {var_99_loss:,.2f} (Max loss expected in 99% of months)")
print(f"99% Conditional VaR (CVaR) : βΉ {cvar_99_loss:,.2f} (Expected average loss if tail breach occurs)")
import numpy as np
def target_unnormalized_density(x: float) -> float:
"""
Complex multimodal target distribution: Mixture of two Gaussians.
f(x) = 0.4 * exp(-0.5 * (x - 2)^2) + 0.6 * exp(-0.5 * (x + 3)^2)
"""
return float(0.4 * np.exp(-0.5 * (x - 2.0)**2) + 0.6 * np.exp(-0.5 * (x + 3.0)**2))
def metropolis_hastings_sampler(num_iterations: int, proposal_std: float = 1.0, seed: int = 42) -> np.ndarray:
"""
Metropolis-Hastings MCMC sampling from unnormalized target density.
"""
rng = np.random.default_rng(seed)
samples = np.empty(num_iterations)
current_x = 0.0 # Initial Markov Chain state
accepted_count = 0
for i in range(num_iterations):
# 1. Propose candidate from Gaussian proposal distribution: x' ~ N(x_curr, Ο^2)
proposed_x = current_x + rng.normal(0.0, proposal_std)
# 2. Acceptance Probability: Ξ± = min(1, p(x') / p(x_curr))
prob_current = target_unnormalized_density(current_x)
prob_proposed = target_unnormalized_density(proposed_x)
acceptance_ratio = min(1.0, prob_proposed / prob_current)
# 3. Accept or Reject with uniform draw u ~ Uniform(0, 1)
if rng.uniform(0.0, 1.0) < acceptance_ratio:
current_x = proposed_x
accepted_count += 1
samples[i] = current_x
acceptance_rate = accepted_count / num_iterations
return samples, acceptance_rate
N_MCMC = 50000
chain_samples, acc_rate = metropolis_hastings_sampler(N_MCMC, proposal_std=1.5, seed=42)
# Discard initial 10% burn-in samples to eliminate startup bias
burn_in = int(0.10 * N_MCMC)
stationary_samples = chain_samples[burn_in:]
print("=" * 65)
print("METROPOLIS-HASTINGS MCMC POSTERIOR SAMPLING")
print("=" * 65)
print(f"Total Iterations Simulated : {N_MCMC:,}")
print(f"Chain Acceptance Rate : {acc_rate * 100:.2f}% (Target: 20% - 50%)")
print(f"Burn-in Discarded : {burn_in:,} samples (Warm-up phase)")
print(f"Stationary Samples Retained : {len(stationary_samples):,}")
print("-" * 65)
print(f"Sampled Posterior Mean : {np.mean(stationary_samples):+.4f} (Matches multimodal center)")
print(f"Sampled Posterior Std Dev : {np.std(stationary_samples):.4f}")
print(f"Empirical 5th Percentile : {np.percentile(stationary_samples, 5):.4f}")
print(f"Empirical 95th Percentile : {np.percentile(stationary_samples, 95):.4f}")
RANDOM SAMPLING & SEEDING REFERENCE MATRIX:
ββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββ
β Sampling Paradigm / API β Python Syntax β Reproducibility Scopeβ Primary Machine Learning Role β
ββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββΌβββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββ€
β Modern Default Generator β rng = np.random.default_rng()β Object-Isolated β Primary standard: PCG64 BitGen β
β Legacy Seed (AVOID!) β np.random.seed(42) β Global Shared State β Deprecated: thread-unsafe MT19937β
β Parallel Stream Spawning β SeedSequence.spawn(N) β Thread/Process-Safe β Distributed multiprocessing runs β
β Array Permutation β rng.permutation(arr) β Deterministic Copy β Train-val-test dataset splitting β
β In-Place Array Shuffling β rng.shuffle(arr) β Mutates Memory View β Epoch shuffling in mini-batches β
β Categorical Choice Sampling β rng.choice(a, size, replace) β Uniform / Weighted β Bootstrap resampling, bagging β
β Reservoir Sampling β Algorithm R (Streaming) β O(k) Memory Bound β Big data streaming IoT logs β
β Stratified Sampling β Subgroup-wise sampling β Class Ratio Preservedβ Extreme class imbalance handling β
β Monte Carlo Integration β Integral via Uniform draws β O(1/βN) Convergence β High-dimensional volume estimate β
β MCMC Metropolis-Hastings β Transition kernel Ξ±-ratio β Stationary Posterior β Bayesian inference, latent state β
ββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββ€
β SAMPLING & SEEDING BEST PRACTICES: β
β 1. Never invoke `np.random.seed()` in libraries or production modules; use local `default_rng(seed)` instances. β
β 2. When distributing across multiple CPU processes, spawn independent seeds using `SeedSequence.spawn()`. β
β 3. For time-series data, never use random permutation splits; use chronological forward-chaining splits instead. β
β 4. In Monte Carlo simulations, verify that sample sizes achieve variance stability within desired precision bounds. β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ