%pylab inline --no-import-all
Populating the interactive namespace from numpy and matplotlib
dir(np.random)
['RandomState', 'Tester', '__RandomState_ctor', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__warningregistry__', 'absolute_import', 'bench', 'beta', 'binomial', 'bytes', 'chisquare', 'choice', 'dirichlet', 'division', 'exponential', 'f', 'gamma', 'geometric', 'get_state', 'gumbel', 'hypergeometric', 'info', 'laplace', 'logistic', 'lognormal', 'logseries', 'mtrand', 'multinomial', 'multivariate_normal', 'negative_binomial', 'noncentral_chisquare', 'noncentral_f', 'normal', 'np', 'operator', 'pareto', 'permutation', 'poisson', 'power', 'print_function', 'rand', 'randint', 'randn', 'random', 'random_integers', 'random_sample', 'ranf', 'rayleigh', 'sample', 'seed', 'set_state', 'shuffle', 'standard_cauchy', 'standard_exponential', 'standard_gamma', 'standard_normal', 'standard_t', 'test', 'triangular', 'uniform', 'vonmises', 'wald', 'warnings', 'weibull', 'zipf']
np.random.rand?
np.random.rand()
0.5237061903414681
np.random.rand(10)
array([ 0.78606654, 0.09874798, 0.86201892, 0.19176246, 0.43471729, 0.08909562, 0.53593493, 0.82124908, 0.74166897, 0.08470391])
plt.hist(np.random.rand(1000))
(array([ 98., 97., 97., 83., 98., 110., 112., 97., 94., 114.]), array([ 5.37033778e-04, 1.00298789e-01, 2.00060545e-01, 2.99822301e-01, 3.99584057e-01, 4.99345812e-01, 5.99107568e-01, 6.98869324e-01, 7.98631080e-01, 8.98392835e-01, 9.98154591e-01]), <a list of 10 Patch objects>)
plt.hist(np.random.normal(0,1,100),bins=30)
(array([ 1., 0., 0., 0., 1., 2., 0., 2., 3., 3., 3., 2., 2., 6., 9., 8., 6., 6., 6., 9., 7., 2., 6., 2., 2., 5., 2., 3., 1., 1.]), array([-3.0672859 , -2.89647161, -2.72565732, -2.55484303, -2.38402874, -2.21321445, -2.04240016, -1.87158587, -1.70077158, -1.52995729, -1.359143 , -1.18832871, -1.01751442, -0.84670013, -0.67588584, -0.50507155, -0.33425726, -0.16344297, 0.00737132, 0.17818561, 0.3489999 , 0.51981418, 0.69062847, 0.86144276, 1.03225705, 1.20307134, 1.37388563, 1.54469992, 1.71551421, 1.8863285 , 2.05714279]), <a list of 30 Patch objects>)
np.random.choice?
np.random.choice(['a','b','c'])
'b'
plt.hist(np.random.gamma(shape=2,scale=2,size=10000),bins=30)
(array([ 7.02000000e+02, 1.40900000e+03, 1.53900000e+03, 1.37600000e+03, 1.20100000e+03, 9.75000000e+02, 7.35000000e+02, 5.44000000e+02, 4.54000000e+02, 2.95000000e+02, 2.23000000e+02, 1.40000000e+02, 1.22000000e+02, 9.30000000e+01, 5.70000000e+01, 4.10000000e+01, 2.30000000e+01, 2.10000000e+01, 1.60000000e+01, 6.00000000e+00, 9.00000000e+00, 6.00000000e+00, 0.00000000e+00, 3.00000000e+00, 4.00000000e+00, 2.00000000e+00, 0.00000000e+00, 1.00000000e+00, 1.00000000e+00, 2.00000000e+00]), array([ 2.46278137e-02, 8.64465415e-01, 1.70430302e+00, 2.54414062e+00, 3.38397822e+00, 4.22381582e+00, 5.06365342e+00, 5.90349102e+00, 6.74332862e+00, 7.58316622e+00, 8.42300383e+00, 9.26284143e+00, 1.01026790e+01, 1.09425166e+01, 1.17823542e+01, 1.26221918e+01, 1.34620294e+01, 1.43018670e+01, 1.51417046e+01, 1.59815422e+01, 1.68213798e+01, 1.76612174e+01, 1.85010550e+01, 1.93408926e+01, 2.01807302e+01, 2.10205678e+01, 2.18604054e+01, 2.27002430e+01, 2.35400806e+01, 2.43799182e+01, 2.52197559e+01]), <a list of 30 Patch objects>)
def myFunction(x):
return np.sin(x*2*np.pi)*np.sin(x*2*np.pi)
plt.hist( myFunction(np.random.rand(10000) ) )
(array([ 1972., 896., 760., 647., 638., 630., 684., 755., 931., 2087.]), array([ 2.44511855e-09, 1.00000001e-01, 1.99999999e-01, 2.99999998e-01, 3.99999996e-01, 4.99999995e-01, 5.99999993e-01, 6.99999992e-01, 7.99999990e-01, 8.99999989e-01, 9.99999987e-01]), <a list of 10 Patch objects>)
xarray = np.random.rand(10000)
yarray = myFunction(xarray)
ymax = np.max(yarray)
ytestarray = ymax*np.random.rand( xarray.size )
accept = map( lambda data: data[1]*ymax < myFunction(data[0]), zip(xarray,ytestarray) )
plt.scatter(xarray,ytestarray, c=accept, alpha=0.2)
plt.plot(np.linspace(0,1,100),myFunction(np.linspace(0,1,100)),c='r')
plt.xlabel('x')
plt.ylabel('rand*f_max')
<matplotlib.text.Text at 0x1140e7890>
import datetime
t0 = datetime.datetime.now()
xaccepted = xarray[ytestarray<yarray]
print datetime.datetime.now()-t0
0:00:00.000274
plt.hist(xaccepted, bins=50,normed=True)
plt.plot(np.linspace(0,1,100),myFunction(np.linspace(0,1,100))*2,c='r')
plt.xlabel('x')
plt.ylabel('p(x)')
<matplotlib.text.Text at 0x116067450>
mean = 10.
contents,edges,patches = plt.hist(np.random.noncentral_chisquare(2,mean**2,10000),bins=30,color='g',alpha=0.3)
plt.hist(np.random.normal(0,1,10000)**2+np.random.normal(mean,1,10000)**2,bins=edges,color='b',alpha=0.3)
(array([ 1.00000000e+00, 1.00000000e+00, 1.00000000e+01, 2.20000000e+01, 5.00000000e+01, 1.26000000e+02, 2.12000000e+02, 3.31000000e+02, 5.08000000e+02, 6.72000000e+02, 8.37000000e+02, 9.56000000e+02, 9.85000000e+02, 1.00400000e+03, 9.02000000e+02, 8.18000000e+02, 7.05000000e+02, 5.47000000e+02, 4.26000000e+02, 3.02000000e+02, 2.23000000e+02, 1.34000000e+02, 9.80000000e+01, 5.10000000e+01, 3.00000000e+01, 2.60000000e+01, 1.20000000e+01, 7.00000000e+00, 0.00000000e+00, 2.00000000e+00]), array([ 33.8415019 , 38.88445075, 43.92739959, 48.97034844, 54.01329729, 59.05624614, 64.09919498, 69.14214383, 74.18509268, 79.22804153, 84.27099037, 89.31393922, 94.35688807, 99.39983692, 104.44278576, 109.48573461, 114.52868346, 119.5716323 , 124.61458115, 129.65753 , 134.70047885, 139.74342769, 144.78637654, 149.82932539, 154.87227424, 159.91522308, 164.95817193, 170.00112078, 175.04406963, 180.08701847, 185.12996732]), <a list of 30 Patch objects>)
(array([ 9.42000000e+02, 1.17600000e+03, 1.20500000e+03, 1.17700000e+03, 1.05900000e+03, 9.37000000e+02, 7.98000000e+02, 6.50000000e+02, 4.82000000e+02, 4.07000000e+02, 3.22000000e+02, 2.23000000e+02, 1.50000000e+02, 1.28000000e+02, 9.30000000e+01, 6.50000000e+01, 4.70000000e+01, 3.70000000e+01, 2.70000000e+01, 1.70000000e+01, 2.10000000e+01, 1.60000000e+01, 5.00000000e+00, 5.00000000e+00, 6.00000000e+00, 2.00000000e+00, 1.00000000e+00, 0.00000000e+00, 1.00000000e+00, 1.00000000e+00]), array([ 4.46625022e-04, 1.14294686e+00, 2.28544709e+00, 3.42794732e+00, 4.57044755e+00, 5.71294778e+00, 6.85544802e+00, 7.99794825e+00, 9.14044848e+00, 1.02829487e+01, 1.14254489e+01, 1.25679492e+01, 1.37104494e+01, 1.48529496e+01, 1.59954499e+01, 1.71379501e+01, 1.82804503e+01, 1.94229506e+01, 2.05654508e+01, 2.17079510e+01, 2.28504513e+01, 2.39929515e+01, 2.51354517e+01, 2.62779520e+01, 2.74204522e+01, 2.85629524e+01, 2.97054527e+01, 3.08479529e+01, 3.19904531e+01, 3.31329534e+01, 3.42754536e+01]), <a list of 30 Patch objects>)
np.random.noncentral_chisquare?
nExp = 1000
nTries=200
maxVals1 = []
maxVals2 = []
for i in range(nExp):
maxVals1.append(np.max(np.random.normal(0,1,nTries)) )
maxVals2.append(np.max(np.random.noncentral_chisquare(3,2,nTries) ) )
contents, edges, panels = plt.hist(maxVals1,bins=30,alpha=0.3)
contents, edges, panels = plt.hist(maxVals2,bins=30,alpha=0.3)
np.mean(maxVals2)
20.750329265840548
np.random.weibull?
x1 = np.random.normal(0,1,1000)
x2 = np.random.normal(0,1,1000)
x3 = np.random.normal(0,1,1000)
chisq = x1**2+x2**2+x3**2
a,b,c = plt.hist(chisq,bins=30)
a,b,c = plt.hist(np.random.chisquare(3,1000),bins=b,alpha=0.3)
theta = np.random.rand(10000)*2*np.pi
contents, edges, patches = plt.hist(np.cos(theta))
data = np.random.normal(10,2,1000)
plt.hist(data)
(array([ 11., 44., 98., 205., 222., 224., 105., 66., 19., 6.]), array([ 4.55349115, 5.72462966, 6.89576817, 8.06690668, 9.23804519, 10.4091837 , 11.58032221, 12.75146072, 13.92259923, 15.09373774, 16.26487625]), <a list of 10 Patch objects>)
dir(np)
['ALLOW_THREADS', 'BUFSIZE', 'CLIP', 'ComplexWarning', 'DataSource', 'ERR_CALL', 'ERR_DEFAULT', 'ERR_DEFAULT2', 'ERR_IGNORE', 'ERR_LOG', 'ERR_PRINT', 'ERR_RAISE', 'ERR_WARN', 'FLOATING_POINT_SUPPORT', 'FPE_DIVIDEBYZERO', 'FPE_INVALID', 'FPE_OVERFLOW', 'FPE_UNDERFLOW', 'False_', 'Inf', 'Infinity', 'MAXDIMS', 'MachAr', 'ModuleDeprecationWarning', 'NAN', 'NINF', 'NZERO', 'NaN', 'NanWarning', 'PINF', 'PZERO', 'PackageLoader', 'RAISE', 'RankWarning', 'SHIFT_DIVIDEBYZERO', 'SHIFT_INVALID', 'SHIFT_OVERFLOW', 'SHIFT_UNDERFLOW', 'ScalarType', 'Tester', 'True_', 'UFUNC_BUFSIZE_DEFAULT', 'UFUNC_PYVALS_NAME', 'WRAP', '__NUMPY_SETUP__', '__all__', '__builtins__', '__config__', '__doc__', '__file__', '__git_revision__', '__name__', '__package__', '__path__', '__version__', '_import_tools', '_mat', 'abs', 'absolute', 'absolute_import', 'add', 'add_docstring', 'add_newdoc', 'add_newdoc_ufunc', 'add_newdocs', 'alen', 'all', 'allclose', 'alltrue', 'alterdot', 'amax', 'amin', 'angle', 'any', 'append', 'apply_along_axis', 'apply_over_axes', 'arange', 'arccos', 'arccosh', 'arcsin', 'arcsinh', 'arctan', 'arctan2', 'arctanh', 'argmax', 'argmin', 'argpartition', 'argsort', 'argwhere', 'around', 'array', 'array2string', 'array_equal', 'array_equiv', 'array_repr', 'array_split', 'array_str', 'asanyarray', 'asarray', 'asarray_chkfinite', 'ascontiguousarray', 'asfarray', 'asfortranarray', 'asmatrix', 'asscalar', 'atleast_1d', 'atleast_2d', 'atleast_3d', 'average', 'bartlett', 'base_repr', 'bench', 'binary_repr', 'bincount', 'bitwise_and', 'bitwise_not', 'bitwise_or', 'bitwise_xor', 'blackman', 'bmat', 'bool', 'bool8', 'bool_', 'broadcast', 'broadcast_arrays', 'busday_count', 'busday_offset', 'busdaycalendar', 'byte', 'byte_bounds', 'bytes_', 'c_', 'can_cast', 'cast', 'cdouble', 'ceil', 'cfloat', 'char', 'character', 'chararray', 'choose', 'clip', 'clongdouble', 'clongfloat', 'column_stack', 'common_type', 'compare_chararrays', 'compat', 'complex', 'complex128', 'complex256', 'complex64', 'complex_', 'complexfloating', 'compress', 'concatenate', 'conj', 'conjugate', 'convolve', 'copy', 'copysign', 'copyto', 'core', 'corrcoef', 'correlate', 'cos', 'cosh', 'count_nonzero', 'cov', 'cross', 'csingle', 'ctypeslib', 'cumprod', 'cumproduct', 'cumsum', 'datetime64', 'datetime_as_string', 'datetime_data', 'deg2rad', 'degrees', 'delete', 'deprecate', 'deprecate_with_doc', 'diag', 'diag_indices', 'diag_indices_from', 'diagflat', 'diagonal', 'diff', 'digitize', 'disp', 'divide', 'division', 'dot', 'double', 'dsplit', 'dstack', 'dtype', 'e', 'ediff1d', 'einsum', 'emath', 'empty', 'empty_like', 'equal', 'errstate', 'euler_gamma', 'exp', 'exp2', 'expand_dims', 'expm1', 'extract', 'eye', 'fabs', 'fastCopyAndTranspose', 'fft', 'fill_diagonal', 'find_common_type', 'finfo', 'fix', 'flatiter', 'flatnonzero', 'flexible', 'fliplr', 'flipud', 'float', 'float128', 'float16', 'float32', 'float64', 'float_', 'floating', 'floor', 'floor_divide', 'fmax', 'fmin', 'fmod', 'format_parser', 'frexp', 'frombuffer', 'fromfile', 'fromfunction', 'fromiter', 'frompyfunc', 'fromregex', 'fromstring', 'full', 'full_like', 'fv', 'generic', 'genfromtxt', 'get_array_wrap', 'get_include', 'get_numarray_include', 'get_printoptions', 'getbuffer', 'getbufsize', 'geterr', 'geterrcall', 'geterrobj', 'gradient', 'greater', 'greater_equal', 'half', 'hamming', 'hanning', 'histogram', 'histogram2d', 'histogramdd', 'hsplit', 'hstack', 'hypot', 'i0', 'identity', 'iinfo', 'imag', 'in1d', 'index_exp', 'indices', 'inexact', 'inf', 'info', 'infty', 'inner', 'insert', 'int', 'int0', 'int16', 'int32', 'int64', 'int8', 'int_', 'int_asbuffer', 'intc', 'integer', 'interp', 'intersect1d', 'intp', 'invert', 'ipmt', 'irr', 'is_busday', 'isclose', 'iscomplex', 'iscomplexobj', 'isfinite', 'isfortran', 'isinf', 'isnan', 'isneginf', 'isposinf', 'isreal', 'isrealobj', 'isscalar', 'issctype', 'issubclass_', 'issubdtype', 'issubsctype', 'iterable', 'ix_', 'kaiser', 'kron', 'ldexp', 'left_shift', 'less', 'less_equal', 'lexsort', 'lib', 'linalg', 'linspace', 'little_endian', 'load', 'loads', 'loadtxt', 'log', 'log10', 'log1p', 'log2', 'logaddexp', 'logaddexp2', 'logical_and', 'logical_not', 'logical_or', 'logical_xor', 'logspace', 'long', 'longcomplex', 'longdouble', 'longfloat', 'longlong', 'lookfor', 'ma', 'mafromtxt', 'mask_indices', 'mat', 'math', 'matrix', 'matrixlib', 'max', 'maximum', 'maximum_sctype', 'may_share_memory', 'mean', 'median', 'memmap', 'meshgrid', 'mgrid', 'min', 'min_scalar_type', 'minimum', 'mintypecode', 'mirr', 'mod', 'modf', 'msort', 'multiply', 'nan', 'nan_to_num', 'nanargmax', 'nanargmin', 'nanmax', 'nanmean', 'nanmin', 'nanstd', 'nansum', 'nanvar', 'nbytes', 'ndarray', 'ndenumerate', 'ndfromtxt', 'ndim', 'ndindex', 'nditer', 'negative', 'nested_iters', 'newaxis', 'newbuffer', 'nextafter', 'nonzero', 'not_equal', 'nper', 'npv', 'number', 'obj2sctype', 'object', 'object0', 'object_', 'ogrid', 'ones', 'ones_like', 'outer', 'packbits', 'pad', 'partition', 'percentile', 'pi', 'piecewise', 'pkgload', 'place', 'pmt', 'poly', 'poly1d', 'polyadd', 'polyder', 'polydiv', 'polyfit', 'polyint', 'polymul', 'polynomial', 'polysub', 'polyval', 'power', 'ppmt', 'print_function', 'prod', 'product', 'promote_types', 'ptp', 'put', 'putmask', 'pv', 'r_', 'rad2deg', 'radians', 'random', 'rank', 'rate', 'ravel', 'ravel_multi_index', 'real', 'real_if_close', 'rec', 'recarray', 'recfromcsv', 'recfromtxt', 'reciprocal', 'record', 'remainder', 'repeat', 'require', 'reshape', 'resize', 'restoredot', 'result_type', 'right_shift', 'rint', 'roll', 'rollaxis', 'roots', 'rot90', 'round', 'round_', 'row_stack', 's_', 'safe_eval', 'save', 'savetxt', 'savez', 'savez_compressed', 'sctype2char', 'sctypeDict', 'sctypeNA', 'sctypes', 'searchsorted', 'select', 'set_numeric_ops', 'set_printoptions', 'set_string_function', 'setbufsize', 'setdiff1d', 'seterr', 'seterrcall', 'seterrobj', 'setxor1d', 'shape', 'short', 'show_config', 'sign', 'signbit', 'signedinteger', 'sin', 'sinc', 'single', 'singlecomplex', 'sinh', 'size', 'sometrue', 'sort', 'sort_complex', 'source', 'spacing', 'split', 'sqrt', 'square', 'squeeze', 'std', 'str', 'str_', 'string0', 'string_', 'subtract', 'sum', 'swapaxes', 'sys', 'take', 'tan', 'tanh', 'tensordot', 'test', 'testing', 'tile', 'timedelta64', 'trace', 'transpose', 'trapz', 'tri', 'tril', 'tril_indices', 'tril_indices_from', 'trim_zeros', 'triu', 'triu_indices', 'triu_indices_from', 'true_divide', 'trunc', 'typeDict', 'typeNA', 'typecodes', 'typename', 'ubyte', 'ufunc', 'uint', 'uint0', 'uint16', 'uint32', 'uint64', 'uint8', 'uintc', 'uintp', 'ulonglong', 'unicode', 'unicode0', 'unicode_', 'union1d', 'unique', 'unpackbits', 'unravel_index', 'unsignedinteger', 'unwrap', 'ushort', 'vander', 'var', 'vdot', 'vectorize', 'version', 'void', 'void0', 'vsplit', 'vstack', 'warnings', 'where', 'who', 'zeros', 'zeros_like']
from scipy.stats import norm
y = np.linspace(0,1,100)
x = norm.ppf(y)
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x11435ac50>]
rand_y = np.random.rand(1000)
rand_x = norm.ppf(rand_y)
plt.hist(rand_x)
(array([ 2., 11., 44., 100., 224., 254., 232., 87., 38., 8.]), array([-3.66116107, -2.9865437 , -2.31192633, -1.63730896, -0.96269159, -0.28807422, 0.38654315, 1.06116053, 1.7357779 , 2.41039527, 3.08501264]), <a list of 10 Patch objects>)
x = np.linspace(-1,1,100)
y = myFunction(x)
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x120191bd0>]
fmax=1
x_data, y_data, x_accept, y_accept = [], [], [], []
for i in range(10000):
x=np.random.rand(1)*2-1
y=np.random.rand(1)*fmax
x_data.append(x[0])
y_data.append(y[0])
if y < myFunction(x):
x_accept.append(x[0])
y_accept.append(y[0])
plt.scatter(x_data,y_data, alpha=0.3, color='blue')
plt.scatter(x_accept,y_accept, alpha=0.3, color='red')
plt.plot(np.linspace(0,1,100),myFunction(np.linspace(0,1,100)))
[<matplotlib.lines.Line2D at 0x1201f5f90>]
plt.hist(x_accept,bins=100)
(array([ 78., 125., 96., 93., 108., 88., 94., 73., 77., 88., 90., 90., 79., 73., 64., 63., 54., 67., 63., 56., 57., 52., 41., 42., 55., 63., 48., 40., 43., 45., 29., 33., 30., 32., 38., 35., 34., 27., 22., 25., 18., 20., 16., 11., 6., 11., 4., 5., 3., 0., 2., 5., 11., 11., 8., 7., 12., 21., 19., 26., 13., 19., 22., 26., 22., 26., 34., 37., 27., 43., 42., 56., 55., 44., 55., 53., 50., 50., 56., 62., 51., 72., 65., 76., 61., 79., 74., 70., 71., 82., 89., 78., 110., 93., 92., 75., 101., 102., 87., 93.]), array([ -9.99505466e-01, -9.79515081e-01, -9.59524696e-01, -9.39534310e-01, -9.19543925e-01, -8.99553540e-01, -8.79563154e-01, -8.59572769e-01, -8.39582384e-01, -8.19591999e-01, -7.99601613e-01, -7.79611228e-01, -7.59620843e-01, -7.39630457e-01, -7.19640072e-01, -6.99649687e-01, -6.79659301e-01, -6.59668916e-01, -6.39678531e-01, -6.19688145e-01, -5.99697760e-01, -5.79707375e-01, -5.59716990e-01, -5.39726604e-01, -5.19736219e-01, -4.99745834e-01, -4.79755448e-01, -4.59765063e-01, -4.39774678e-01, -4.19784292e-01, -3.99793907e-01, -3.79803522e-01, -3.59813136e-01, -3.39822751e-01, -3.19832366e-01, -2.99841980e-01, -2.79851595e-01, -2.59861210e-01, -2.39870825e-01, -2.19880439e-01, -1.99890054e-01, -1.79899669e-01, -1.59909283e-01, -1.39918898e-01, -1.19928513e-01, -9.99381274e-02, -7.99477421e-02, -5.99573568e-02, -3.99669714e-02, -1.99765861e-02, 1.37991748e-05, 2.00041845e-02, 3.99945698e-02, 5.99849551e-02, 7.99753404e-02, 9.99657257e-02, 1.19956111e-01, 1.39946496e-01, 1.59936882e-01, 1.79927267e-01, 1.99917652e-01, 2.19908038e-01, 2.39898423e-01, 2.59888808e-01, 2.79879194e-01, 2.99869579e-01, 3.19859964e-01, 3.39850349e-01, 3.59840735e-01, 3.79831120e-01, 3.99821505e-01, 4.19811891e-01, 4.39802276e-01, 4.59792661e-01, 4.79783047e-01, 4.99773432e-01, 5.19763817e-01, 5.39754203e-01, 5.59744588e-01, 5.79734973e-01, 5.99725358e-01, 6.19715744e-01, 6.39706129e-01, 6.59696514e-01, 6.79686900e-01, 6.99677285e-01, 7.19667670e-01, 7.39658056e-01, 7.59648441e-01, 7.79638826e-01, 7.99629212e-01, 8.19619597e-01, 8.39609982e-01, 8.59600368e-01, 8.79590753e-01, 8.99581138e-01, 9.19571523e-01, 9.39561909e-01, 9.59552294e-01, 9.79542679e-01, 9.99533065e-01]), <a list of 100 Patch objects>)
len(x_accept)
3474
def myFunction(x):
return np.abs(x)
fmax=1
nExperiments= 10000
nDistributions = 8
xtot_data = []
for i in range(nExperiments):
x_data, y_data, x_accept, y_accept = [], [], [], []
while len(x_accept) < nDistributions:
x=np.random.rand(1)*2-1.
y=np.random.rand(1)*fmax
x_data.append(x[0])
y_data.append(y[0])
if y < myFunction(x):
x_accept.append(x[0])
y_accept.append(y[0])
xtot = np.sum(x_accept)
xtot_data.append(xtot)
c,e, p = plt.hist(xtot_data,bins=30)
xtot_data = []
nDistributions=50
for i in range(nExperiments):
xtot_data.append(np.sum(np.random.choice([-1,1],nDistributions)))
plt.hist(xtot_data, bins=100)
(array([ 1.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 8.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.70000000e+01, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 4.00000000e+01, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 8.90000000e+01, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.58000000e+02, 0.00000000e+00, 0.00000000e+00, 2.81000000e+02, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 4.39000000e+02, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 6.34000000e+02, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 7.56000000e+02, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 9.90000000e+02, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.07900000e+03, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.12500000e+03, 0.00000000e+00, 0.00000000e+00, 1.04600000e+03, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 9.73000000e+02, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 7.61000000e+02, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 6.07000000e+02, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 4.10000000e+02, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 2.68000000e+02, 0.00000000e+00, 0.00000000e+00, 1.71000000e+02, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 7.40000000e+01, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 3.80000000e+01, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 2.10000000e+01, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 9.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 4.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+00]), array([-26. , -25.48, -24.96, -24.44, -23.92, -23.4 , -22.88, -22.36, -21.84, -21.32, -20.8 , -20.28, -19.76, -19.24, -18.72, -18.2 , -17.68, -17.16, -16.64, -16.12, -15.6 , -15.08, -14.56, -14.04, -13.52, -13. , -12.48, -11.96, -11.44, -10.92, -10.4 , -9.88, -9.36, -8.84, -8.32, -7.8 , -7.28, -6.76, -6.24, -5.72, -5.2 , -4.68, -4.16, -3.64, -3.12, -2.6 , -2.08, -1.56, -1.04, -0.52, 0. , 0.52, 1.04, 1.56, 2.08, 2.6 , 3.12, 3.64, 4.16, 4.68, 5.2 , 5.72, 6.24, 6.76, 7.28, 7.8 , 8.32, 8.84, 9.36, 9.88, 10.4 , 10.92, 11.44, 11.96, 12.48, 13. , 13.52, 14.04, 14.56, 15.08, 15.6 , 16.12, 16.64, 17.16, 17.68, 18.2 , 18.72, 19.24, 19.76, 20.28, 20.8 , 21.32, 21.84, 22.36, 22.88, 23.4 , 23.92, 24.44, 24.96, 25.48, 26. ]), <a list of 100 Patch objects>)