Skip to content

Instantly share code, notes, and snippets.

View mjam03's full-sized avatar

Mark Jamison mjam03

  • Belfast / London
View GitHub Profile
@mjam03
mjam03 / 99635b5f-6fb0-4658-881b-daad73aeef4c.py
Created April 22, 2022 12:39
random_sampling_with_scipy_and_numpy_part_iii5
# define dist
my_dist = cust_dist(0.8)
# create samples
n = 1000000
cust_samps = my_dist.rvs(size=n)
# plot histogram
fig, ax = plt.subplots(ncols=1, figsize=(25,12))
# plot hist and pdf to see if they aline
sns.distplot(cust_samps, label='cust_samps', kde=False, norm_hist=True, ax=ax)
@mjam03
mjam03 / 54cc8aa1-9b9a-47dc-b90b-5296b0d07d7f.py
Created April 22, 2022 12:39
random_sampling_with_scipy_and_numpy_part_iii4
# define our gaussian look-a-like distribution
class cust_dist(stats.rv_continuous):
# define init with sigma deviation param e
def __init__(self, e, *args, **kwargs):
super().__init__(*args, **kwargs)
# init our variance divergence
self.e = e
# init our cdf and ppf functions
self.cdf_func, self.ppf_func = self.create_cdf_ppf()
@mjam03
mjam03 / f011c625-9409-4511-a61c-bbc1a8c762ef.py
Created April 22, 2022 12:39
random_sampling_with_scipy_and_numpy_part_iii3
def _ppf_single(self, q, *args):
factor = 10.
left, right = self._get_support(*args)
if np.isinf(left):
left = min(-factor, right)
while self._ppf_to_solve(left, q, *args) > 0.:
left, right = left * factor, left
# left is now such that cdf(left) <= q
# if right has changed, then cdf(right) > q
@mjam03
mjam03 / c9be344f-bbe8-4571-a5c7-8055e36007ff.py
Created April 22, 2022 12:39
random_sampling_with_scipy_and_numpy_part_iii2
# define instance of dist
naive_dist = naive_cust_dist(e=0.5)
# draw samples
n = 1000
naive_samps = naive_dist.rvs(size=n)
# plot them
fig, ax = plt.subplots(ncols=1, figsize=(25,12))
# plot hist and pdf to see if they aline
sns.distplot(naive_samps, label='naive_samps', kde=False, norm_hist=True, ax=ax)
@mjam03
mjam03 / 3e4f3984-5d9c-401b-b0c1-0a22abd88364.py
Created April 22, 2022 12:39
random_sampling_with_scipy_and_numpy_part_iii1
# define our custom distribution
class naive_cust_dist(stats.rv_continuous):
# define init with sigma deviation param e
def __init__(self, e, *args, **kwargs):
super().__init__(*args, **kwargs)
self.e = e
# function to return normal distribution pdf
def norm_p(self, x, loc=0, scale=1):
@mjam03
mjam03 / 2af2dc56-b60c-433d-9995-cddc04645eec.py
Created April 22, 2022 12:39
random_sampling_with_scipy_and_numpy_part_iii0
# usual suspects
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import scipy.stats as stats
import warnings
from scipy.integrate import simps
from scipy.interpolate import interp1d
@mjam03
mjam03 / 0aea52dc-b5a3-4403-bd08-9c6af5babefa.py
Created April 12, 2022 15:09
random_sampling_with_scipy_and_numpy_part_ii4
# sample size
n = 1000000
# define scipy implementation
snorm = stats.norm()
# define newer numpy implementation
nnorm = np.random.default_rng()
@mjam03
mjam03 / abbd81fd-178c-4ae1-b5bb-1598c964711b.py
Created April 12, 2022 15:09
random_sampling_with_scipy_and_numpy_part_ii3
double legacy_gauss(aug_bitgen_t *aug_state) {
if (aug_state->has_gauss) {
const double temp = aug_state->gauss;
aug_state->has_gauss = false;
aug_state->gauss = 0.0;
return temp;
} else {
double f, x1, x2, r2;
do {
@mjam03
mjam03 / 3141d0e2-fab3-4a05-b417-9bf00466a6ee.py
Created April 12, 2022 15:09
random_sampling_with_scipy_and_numpy_part_ii2
def __init__(self, seed=None):
if seed is None:
bit_generator = _MT19937()
elif not hasattr(seed, 'capsule'):
bit_generator = _MT19937()
bit_generator._legacy_seeding(seed)
else:
bit_generator = seed
@mjam03
mjam03 / e048a5c0-3717-407a-ae55-c9690c72184d.py
Created April 12, 2022 15:09
random_sampling_with_scipy_and_numpy_part_ii1
# stripped out the doc string for ease
def check_random_state(seed):
if seed is None or seed is np.random:
return np.random.mtrand._rand
### this is the key line ###
if isinstance(seed, (numbers.Integral, np.integer)):
return np.random.RandomState(seed)
if isinstance(seed, (np.random.RandomState, np.random.Generator)):