Simulating Deposit Account Openings
When desiging a set of simulations, you typically need to make 2 decisions.- The distribution that you will use to model the process being simulated
- The statisitics (usually moments that you use to paraterize or fit the distribution )
Simulating the Number of Account Openings
Account openings over a period (say a month) is a random process whose value is a non-negative integer. That is, the number of openings in a month can be any of 0,1,2,3,4...As such, this is classified as being a count process, which is typically fit using a Poisson Distribution.
The Poisson distribution has a single parameter, {% \lambda %} which determines the mean and variance of the distribution.
from scipy.stats import poisson
# Set the expected average rate (mu / lambda) and the number of samples (periods)
mu_value = 4.0
sample_size = 10
# Generate random numbers from the Poisson distribution
random_numbers = poisson.rvs(mu=mu_value, size=sample_size)
print(f"Nmber of accounts opened each period: {random_numbers}")
Simulating the Account Balance
The account balance for each newly opened account is also a random variable. Here, the variable can be any value greater than zero. (Although one could argue that there should be a reasonable bound on the upside).A simple distribution with this property is the lognormal distribution.
To specify the lognormal distribution, you need to specify its mean and variance.
import numpy as np
from scipy.stats import lognorm
# 1. Define the parameters of the underlying normal distribution
mu = 0.0 # Mean of the log-transformed data
sigma = 0.5 # Standard deviation of the log-transformed data
# 2. Map parameters to SciPy's notation
shape_parameter = sigma # 's' in scipy
scale_parameter = np.exp(mu) # 'scale' in scipy
loc_parameter = 0 # Keep at 0 unless shifting the distribution
# 3. Simulate random variables using .rvs()
sample_size = 10000
simulated_data = lognorm.rvs(
s=shape_parameter,
loc=loc_parameter,
scale=scale_parameter,
size=sample_size,
random_state=42 # Seed for reproducibility
)
# Verification: Print the empirical mean vs theoretical expectation
print(f"Empirical Mean: {np.mean(simulated_data):.4f}")
print(f"Theoretical Mean: {np.exp(mu + (sigma**2)/2):.4f}")