.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/monte_carlo/variance_reduction/plot_01_neyman_stratification.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via JupyterLite. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_monte_carlo_variance_reduction_plot_01_neyman_stratification.py: Neyman stratification ===================== Jerzy Neyman (1934) showed survey statisticians how to sample a stratified population: draw from each stratum separately and allocate the sample in proportion to the stratum's weight times its standard deviation, .. math:: n_h \propto p_h \sigma_h, \qquad \operatorname{Var}\hat\mu = \sum_h \frac{p_h^2 \sigma_h^2}{n_h}. In Monte Carlo the "population" is the unit interval that drives the terminal stock price through :math:`Z = \Phi^{-1}(U)`. Equal-probability strata of :math:`U` remove the between-strata variance, and Neyman's allocation puts the remaining samples where the payoff varies most: in the money. .. GENERATED FROM PYTHON SOURCE LINES 21-37 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy.stats import norm from financialkit.monte_carlo import european_option_mc, stratified_estimate, terminal_prices, vanilla_payoff from financialkit.monte_carlo.visualizers import plot_allocation, plot_estimator_comparison from financialkit.options import black_scholes_price S, K, T, r, sigma = 100.0, 110.0, 1.0, 0.05, 0.2 exact = black_scholes_price(S, K, T, r, sigma) def call(u): return np.exp(-r * T) * vanilla_payoff(terminal_prices(S, T, r, sigma, norm.ppf(u)), K) .. GENERATED FROM PYTHON SOURCE LINES 38-40 Crude, proportional and Neyman allocations ------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 40-51 .. code-block:: Python n = 10_000 crude = european_option_mc(S, K, T, r, sigma, n_paths=n, rng=0) prop = stratified_estimate(call, n, n_strata=20, allocation="proportional", rng=1) ney = stratified_estimate(call, n, n_strata=20, allocation="neyman", rng=2) for res in (crude, prop, ney): print(f"{res.method:24s} {res.estimate:.4f} +/- {res.std_error:.4f} variance reduction x{res.variance_ratio(crude):.1f}") fig, axes = plt.subplots(1, 2, figsize=(12, 4)) plot_estimator_comparison([crude, prop, ney], exact=exact, ax=axes[0]) .. image-sg:: /api/gallery/monte_carlo/variance_reduction/images/sphx_glr_plot_01_neyman_stratification_001.png :alt: plot 01 neyman stratification :srcset: /api/gallery/monte_carlo/variance_reduction/images/sphx_glr_plot_01_neyman_stratification_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none crude 6.0910 +/- 0.1148 variance reduction x1.0 stratified (proportional) 6.0174 +/- 0.0273 variance reduction x17.7 stratified (neyman) 6.0396 +/- 0.0107 variance reduction x113.0 .. GENERATED FROM PYTHON SOURCE LINES 52-56 Where Neyman puts the samples ----------------------------- Strata of low :math:`U` end out of the money with zero variance and get only the two-sample floor. .. GENERATED FROM PYTHON SOURCE LINES 56-60 .. code-block:: Python plot_allocation(ney, ax=axes[1]) fig.tight_layout() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.064 seconds) .. _sphx_glr_download_api_gallery_monte_carlo_variance_reduction_plot_01_neyman_stratification.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../../../lite/lab/index.html?path=api/gallery/monte_carlo/variance_reduction/plot_01_neyman_stratification.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_neyman_stratification.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_neyman_stratification.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_neyman_stratification.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_