0% found this document useful (0 votes)
8 views

Tarea 8

The document discusses fitting a linear regression model to data. It loads data from a file, calculates the chi-squared value for different parameter combinations to find the best fit, then uses a matrix method to calculate the maximum likelihood solution and covariance matrix of the parameter estimates. It plots the original data, best chi-squared fit, and maximum likelihood fit on a scatter plot for comparison.

Uploaded by

Marcela Bernal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Tarea 8

The document discusses fitting a linear regression model to data. It loads data from a file, calculates the chi-squared value for different parameter combinations to find the best fit, then uses a matrix method to calculate the maximum likelihood solution and covariance matrix of the parameter estimates. It plots the original data, best chi-squared fit, and maximum likelihood fit on a scatter plot for comparison.

Uploaded by

Marcela Bernal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

In [24]:

from matplotlib import pyplot as plt


import numpy as np

datos = np.loadtxt("datos.txt")
x = datos[:, 0]
y = datos[:, 1]

yerr= datos[:, 2]

plt.errorbar(x, y, yerr, fmt="o", color='blue',


ecolor='black', elinewidth=1, capsize=3)

plt.title('Datos', fontsize=25)
plt.xlabel('Eje $x$')
plt.ylabel('Eje $y$')

plt.show()

In [4]:

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
import numpy as np

datos = np.loadtxt("datos.txt")
x = datos[:, 0]
y = datos[:, 1]
sigma = datos[:,2]
a = np.arange(7, 8, 0.02, dtype =float)
b = np.arange(0.5, 1.2 , 0.013, dtype =float)
agrid, bgrid = np.meshgrid(a, b)
χ_cuadrado = sum((y - (agrid + bgrid*(x) ))**2 / (sigma)**2)
χ_cuadrado

Out[4]: array([ 105.92717207, 754.07382313, 437.03527721, 29.26236354,


367.88519807, 29.86840098, 517.278748 , 413.06188466,
65.19969916, 314.47379361, 630.35632653, 200.44364913,
756.73221005, 462.58345885, 857.17527376, 1138.34573482,
172.97522813, 12.03240983, 51.65497928, 799.18725433,
97.17851638, 203.37935135, 467.82799096, 39.24277125,
32.65709033, 639.73317688, 158.52797617, 47.78727665,
64.32682993, 58.79991752, 239.03905592, 89.39347234,
63.07162113, 261.92086006, 102.44161244, 206.92345405,
32.76236814, 565.62933872, 38.01842601, 102.26470079,
168.16013641, 235.12294888, 504.453226 , 189.83779387,
15.85071471, 247.89806566, 296.47517674, 34.04639835,
50.63013471, 85.45312133])

In [5]:
minimo = np.argmin(χ_cuadrado)
a_val = a[minimo]
b_val = b[minimo]
a_val, b_val

Out[5]: (7.339999999999993, 0.7210000000000002)

In [7]:
np.min(χ_cuadrado)

Out[7]: 12.032409830788836

In [3]:
M=(np.column_stack((np.ones(np.size(x)),x)))

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
M

Out[3]: array([[1. , 5.469],


[1. , 7.979],
[1. , 8.204],
[1. , 1.22 ],
[1. , 6.02 ],
[1. , 5.255],
[1. , 4.639],
[1. , 4.714],
[1. , 6.327],
[1. , 9.257],
[1. , 8.155],
[1. , 9.444],
[1. , 9.196],
[1. , 4.148],
[1. , 8.258],
[1. , 9.463],
[1. , 6.28 ],
[1. , 2.523],
[1. , 5.924],
[1. , 8.604],
[1. , 0.485],
[1. , 8.548],
[1. , 7.202],
[1. , 2.96 ],
[1. , 4.854],
[1. , 8.443],
[1. , 6.025],
[1. , 1.571],
[1. , 5.261],
[1. , 1.876],
[1. , 6.704],
[1. , 5.487],
[1. , 1.564],
[1. , 9.325],
[1. , 5.43 ],
[1. , 6.526],
[1. , 6.713],
[1. , 8.189],
[1. , 0.136],
[1. , 8.478],
[1. , 4.351],
[1. , 4.775],

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
[1. , 7.153],
[1. , 8.402],
[1. , 1.133],
[1. , 6.124],
[1. , 2.214],
[1. , 0.397],
[1. , 2.807],
[1. , 2.937]])

In [226…
M.shape

Out[226… (50, 2)

In [4]:
Y = np.reshape(y, (50,1))
Y

Out[4]: array([[12.482],
[14.695],
[16.623],
[ 8.363],
[12.628],
[11.689],
[12.267],
[12.121],
[11.938],
[15.822],
[15.306],
[16.387],
[14.828],
[11.961],
[15.335],
[16.554],
[13.094],
[ 9.367],
[11.808],
[15.459],
[ 6.391],
[16.184],
[13.805],
[10.529],
[10.229],

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
[14.818],
[14.463],
[ 9.213],
[11.929],
[10.286],
[14.031],
[13.057],
[10.62 ],
[15.505],
[12.036],
[13.076],
[12.798],
[16.414],
[ 7.095],
[14.435],
[11.511],
[12.243],
[13.396],
[13.422],
[ 8.693],
[12.123],
[ 7.83 ],
[ 7.333],
[10.373],
[10.664]])

In [3]:
C = (sigma**2) * np.eye(50)
C

Out[3]: array([[1.006009, 0. , 0. , ..., 0. , 0. , 0. ],


[0. , 0.25 , 0. , ..., 0. , 0. , 0. ],
[0. , 0. , 1.212201, ..., 0. , 0. , 0. ],
...,
[0. , 0. , 0. , ..., 1.418481, 0. , 0. ],
[0. , 0. , 0. , ..., 0. , 0.346921, 0. ],
[0. , 0. , 0. , ..., 0. , 0. , 0.25 ]])

In [6]:
#Solución de máxima probabilidad
Θ = (np.linalg.inv(np.mat(np.transpose(M))*np.mat(np.linalg.inv(C))*np.mat(M)))*((
np.mat(np.transpose(M))*np.mat(np.linalg.inv(C))*np.mat(Y)))
Θ

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Out[6]: matrix([[7.59867088],
[0.89430915]])

In [7]:
#Matriz de covarianza
ΣΘ = (np.linalg.inv(np.mat(np.transpose(M))*np.mat(np.linalg.inv(C))*np.mat(M)))
ΣΘ

Out[7]: matrix([[ 0.05027995, -0.00725228],


[-0.00725228, 0.00128337]])

In [8]:
#incerteza para parámetros a y b:
a_inc=np.sqrt(ΣΘ[0,0])
b_inc=np.sqrt(ΣΘ[1,1])

a_inc, b_inc

Out[8]: (0.22423191280722105, 0.035824098828766776)

In [6]:
from matplotlib import pyplot as plt
import numpy as np

datos = np.loadtxt("datos.txt")
x = datos[:, 0]
y = datos[:, 1]
x1 = np.sort(x)

plt.scatter(x, y, color='royalblue')
plt.title('Datos', fontsize=25)
plt.xlabel('Eje $x$')
plt.ylabel('Eje $y$')

plt.plot(x1, 7.339999999999993 + 0.7210000000000002*x1, ':', lw = 3, color = 'black',


label='modelo_χcuadrado')
plt.plot(x1, 7.59867088 + 0.89430915*x1, lw = 2.5, color = 'red', label='modelo_matricial')
plt.legend()

plt.show()

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
In [ ]:

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD

You might also like