Skip to content

Instantly share code, notes, and snippets.

@tupui
Last active April 12, 2022 11:37
Show Gist options
  • Save tupui/54a57f474a069f685ba85c9036f6b121 to your computer and use it in GitHub Desktop.
Save tupui/54a57f474a069f685ba85c9036f6b121 to your computer and use it in GitHub Desktop.
Timing MC vs QMC
"""Timing MC vs QMC.
Comparison of `numpy.random.Generator` (MC) vs `scipy.stats.qmc.Sobol` (QMC)
speed to sample points.
----------------
MIT License
Copyright (c) 2022 Pamphile Tupui ROY
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from functools import partial
import timeit
import numpy as np
from scipy.stats import qmc
import tqdm
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
repeat = 10
number = 1000
d_gen = [1, 5, 10, 15, 20]
n_gen = [2**10, 2**11, 2**12, 2**13, 2**14, 2**15]
def rng_random(rng, n):
return rng.random((n, d))
def qrng_random(qrng, n):
return qrng.random(n)
result = []
for d in tqdm.tqdm(d_gen):
for n in tqdm.tqdm(n_gen):
timer_rng = timeit.Timer(partial(rng_random, np.random.default_rng(), n))
timing_rng = timer_rng.repeat(repeat=repeat, number=number)
result.append((
'np.random',
d, n,
timing_rng
))
timer_qrng = timeit.Timer(partial(qrng_random, qmc.Sobol(d=d), n))
timing_qrng = timer_qrng.repeat(repeat=repeat, number=number)
result.append((
'qmc.Sobol',
d, n,
timing_qrng
))
result.append((
'np.random/qmc.Sobol',
d, n,
np.array(timing_rng) / np.array(timing_qrng)
))
df = pd.DataFrame(result, columns=["func", "d", "n", "timing"])
df = df.explode("timing", ignore_index=True)
df.n = np.log2(df.n.values)
df_timing = df[df.func != 'np.random/qmc.Sobol'].copy()
df_timing.timing = np.log2(df_timing.timing.values.astype(float))
rel_plot = sns.relplot(x="n", y="timing", hue="d", col="func", kind="line", data=df_timing)
rel_plot.set_xlabels(rf"$2^n$")
rel_plot.set_ylabels(r"$\log( \mathrm{timing} )$")
plt.show()
df_ratio = df[df.func == 'np.random/qmc.Sobol']
line_plot = sns.lineplot(x="n", y="timing", hue="d", data=df_ratio)
line_plot.set(xlabel=rf"$2^n$", ylabel="timing(np.random/qmc.Sobol)")
plt.show()
@tupui
Copy link
Author

tupui commented Apr 12, 2022

numpy.Generator vs scipy.stats.qmc.Sobol varying sample size and dimensions: Sobol is always faster

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment