Back to projects
May 03, 2024
2 min read

Lumière

A didactical, modular path tracer

In this repository, you can find all the code associated to the Lumière path tracing project I have been working on.

Monte Carlo Estimator for the Normal Distribution
import numpy as np
import plotly.graph_objs as go  

def normal_dist(x, mean, variance):
    return 1 / np.sqrt(2 * np.pi * variance) * np.exp(-np.power(x - mean, 2) / (2 * variance))

# Define parameters for normal distribution
mean = 0
variance = 1
visual_width = 5
comp_width = 3
left_bound = mean - comp_width
right_bound = mean + comp_width
fidelity = 10000

# Generate (x, y) values for visualizing the normal distribution
x_values = np.linspace(left_bound, right_bound, fidelity)
y_values = np.zeros(len(x_values))
for i, x in enumerate(x_values):
    y_values[i] = normal_dist(x, mean, variance)

# Plot the normal distribution
fig = go.Figure()
fig.add_trace(go.Scatter(x=x_values, y=y_values, mode='lines', name=f'Normal Distribution(µ={mean}, var={variance})', line=dict(color='lightblue')))

# Monte Carlo Estimator
num_samples = 100
sample_x = []
sample_density = []

# Generate num_samples samples at random positions
samples = np.random.rand(num_samples) * (right_bound - left_bound) + left_bound
for sample in samples:
    sample_x.append(sample)
    sample_density.append(normal_dist(sample, mean, variance))

# Monte Carlo Estimate
monte_carlo_estimate = np.mean(sample_density) * (right_bound - left_bound)
print(f'Monte Carlo Estimate: {monte_carlo_estimate}')

# Visualize the Monte Carlo Estimator
fig.add_trace(go.Scatter(x=sample_x, y=sample_density, mode='markers', marker=dict(size=8, color='orange'), name='Monte Carlo Estimator'))
fig.update_yaxes(range=[0, 0.5])
fig.update_layout(title=f'Normal Distribution & Monte Carlo Estimator (N = {num_samples})',
                  xaxis_title='x',
                  yaxis_title='Probability Density',
                  showlegend=False,
                  width=1920,
                  height=1080)
fig.write_image(f"normal-{num_samples}.png")