0

I am trying to create a 2d array that has a specific min, max, mean and stddev. Long story short I am trying to use both

np.random.randint(min,max, size=(row,col)
np.random.normal(mu,sigma, size=(row,col) 

at the same time. Does anyone know if a function has already been made for this? I've have a standard deviation matching function but it changes the min and max of the array as well unfortunately that could be another way to do it too.

martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

0

You can use uniform distribution with boundaries "translated" from normal to uniform space (using error function) and convert it to normal distribution using inverse error function.

import matplotlib.pyplot as plt
import numpy as np
from scipy import special

mean = 2
std = 3
min_value = 0
max_value = 6

min_in_standard_domain = (min_value - mean) / std  
max_in_standard_domain = (max_value - mean) / std

min_in_erf_domain = special.erf(min_in_standard_domain)
max_in_erf_domain = special.erf(max_in_standard_domain)

random_uniform_data = np.random.uniform(min_in_erf_domain, max_in_erf_domain, 10000)
random_gaussianized_data = (special.erfinv(random_uniform_data) * std) + mean
fig, axes = plt.subplots(1, 2, figsize=(12, 6))
axes[0].hist(random_uniform_data, 30)
axes[1].hist(random_gaussianized_data, 30)
axes[0].set_title('uniform distribution samples')
axes[1].set_title('erfinv(uniform distribution samples)')
plt.show()

enter image description here

dankal444
  • 3,172
  • 1
  • 23
  • 35