In [1]:
%pylab
In [3]:
def function(x):
y = exp(-((x-30.0)**2)/100.0)
return y
In [63]:
X = linspace(0, 60, 100)
Y = function(X)
In [62]:
plot(x, y)
Out[62]:
In [11]:
#1. Make a random number between 0 and 60
In [55]:
walk = []
x = random.random()*60
walk.append(x)
In [56]:
for i in range(200000):
x = random.random()*2-1
alpha = function(x + walk[-1])/function(walk[-1])
if alpha>=1.0:
walk.append(walk[-1]+x)
else:
beta = random.random()
if(beta<=alpha):
walk.append(walk[-1]+x)
else:
walk.append(walk[-1])
In [69]:
histo = hist(walk, bins=40, normed=True)
f = function(X)
norm = sum(f*(x[1]-x[0]))
plot(X,f/norm, linewidth=1, color='r')
Out[69]:
In [50]:
size(walk)
Out[50]:
In [2]:
def function(x, y):
f = exp((-(100*(y-x**2)**2)+(1-x)**2)/20.)
return f
In [3]:
from mpl_toolkits.mplot3d import Axes3D
In [33]:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X = np.linspace(-20, 20, 300)
Y = np.linspace(-20, 200, 300)
X, Y = np.meshgrid(X,Y)
Z = function(X, Y)
ax.plot_surface(X, Y, log10(Z))
show()
In [115]:
print amin(Y)
In [31]:
hist = hist2d(X, Y, weights=log10(Z), bins=(60, 60), normed=True)
colorbar()
Out[31]:
In [6]:
%pylab inline
#load the list of points
obs_data = loadtxt("movimiento.dat")
t_obs = obs_data[:,0]
x_obs = obs_data[:,1]
In [7]:
scatter(t_obs,x_obs)
Out[7]:
In [4]:
def likelihood(y_obs, y_model):
chi_squared = (1.0/2.0)*sum((y_obs-y_model)**2)
return exp(-chi_squared)
In [5]:
def my_model(t_obs, x_obs, a, b):
return a*t_obs + b*t_obs**2
In [ ]:
a_walk = []
b_walk = []
l_walk = []
m_walk = append(random.random())
b_walk = append(random.random())
y_init = my_model(t_obs, m_walk[0], b_walk[0])
l_walk = append(l_walk, likelihood(y_obs, y_init))
print m_walk
print b_walk
print l_walk