Commit 2636eb96 authored by Mauro Donega's avatar Mauro Donega
Browse files

MC methods

parent 66d2ef3d
%% Cell type:code id: tags:
``` python
import numpy as np
from matplotlib import pyplot as plt
```
%% Cell type:code id: tags:
``` python
f = lambda x, : np.exp(-x)
values = np.linspace(0,5,100)
fig, ax = plt.subplots()
ax.plot(values, f(values), 'r')
```
%% Output
[<matplotlib.lines.Line2D at 0x1119f3940>]
%% Cell type:code id: tags:
``` python
# Generate uniformly distributed random numbers
N = 10**3
x = np.random.uniform(0, 5, N)
y = np.random.uniform(0.0, 1.0, N)
```
%% Cell type:code id: tags:
``` python
# Hit or Miss Method
random_hit_miss = x[(y < f(x))]
```
%% Cell type:code id: tags:
``` python
# Plot
bins = np.linspace(0,5,50)
plt.hist(random_hit_miss, bins) #, density=True)
plt.show()
```
%% Output
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
import numpy as np
from matplotlib import pyplot as plt
```
%% Cell type:code id: tags:
``` python
r = np.random.uniform(0,1,10**3)
```
%% Cell type:code id: tags:
``` python
bins=np.linspace(0,1,50)
plt.hist(r,bins)
plt.show()
```
%% Output
%% Cell type:code id: tags:
``` python
f = -np.log(1-r)
```
%% Cell type:code id: tags:
``` python
bins2= np.linspace(0,5,50)
plt.hist(f,bins2) #, density=True)
plt.show()
```
%% Output
%% Cell type:code id: tags:
``` python
```
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment