fork download
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from scipy import integrate, optimize
  4.  
  5. # функция распределения
  6. def distr(x):
  7. return np.exp(-x*10)*(np.cos(np.exp(x*10))+1)
  8. # ее интеграл
  9. s = integrate.quad(distr, 0, 1, limit=1000, full_output=1)[0]
  10. def distr_int(x):
  11. return integrate.quad(distr, 0, x, limit=100, full_output=1)[0] / s
  12. # обратная от интеграла
  13. def inv(x):
  14. return optimize.fsolve(lambda z: distr_int(z) - x, x0=0.5)[0]
  15.  
  16. x = np.random.rand(10)
  17. # применяем распределение
  18. y = np.vectorize(inv)(x)
Runtime error #stdin #stdout #stderr 0.03s 63620KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 1, in <module>
    import numpy as np
ImportError: No module named numpy