Tema 2
Tema 2
Tema 2
2. Estimar (contrastar) la (una) relación lineal concreta (estructural).- supone actuar sobre los
coeficientes de la relación lineal.
i =i i =i
𝑛 𝑛 𝑛
E (a, b )
= 0
a
E (a, b )
= 0
b
𝑛 𝑛
𝜕𝐸(𝑎, 𝑏)
⟹ = ∑ −2𝑦𝑖 𝑥𝑖 + 2𝑎𝑥𝑖 2 + 2𝑥𝑖 𝑏 = 0 → 2 ∗ ∑ −𝑦𝑖 𝑥𝑖 + 𝑎𝑥𝑖 2 + 𝑥𝑖 𝑏 = 0
𝜕𝑎
𝑖=1 𝑖=1
𝑛 𝑛 𝑛
2 2
→ ∑ −𝑦𝑖 𝑥𝑖 + 𝑎𝑥𝑖 + 𝑥𝑖 𝑏 = 0 → 𝑅𝑒𝑜𝑟𝑑𝑒𝑛𝑎𝑚𝑜𝑠 𝑦 ∑ 𝑎𝑥𝑖 + 𝑥𝑖 𝑏 = ∑ 𝑦𝑖 𝑥𝑖
𝑖=1 𝑖=1 𝑖=1
𝑛 𝑛 𝑛
𝜕𝐸(𝑎, 𝑏)
⟹ = ∑ −2𝑦𝑖 + 2𝑎𝑥𝑖 + 2𝑏 = 0 → 2 ∗ ∑ −𝑦𝑖 + 𝑎𝑥𝑖 + 𝑏 = 0 → ∑ −𝑦𝑖 + 𝑎𝑥𝑖 + 𝑏 = 0
𝜕𝑏
𝑖=1 𝑖=1 𝑖=1
𝑛 𝑛
Una 𝑣𝑒𝑧 𝑜𝑏𝑡𝑒𝑛𝑖𝑑𝑎𝑠 𝑙𝑎𝑠 𝑑𝑒𝑟𝑖𝑣𝑎𝑑𝑎𝑠, 𝑙𝑜 𝑝𝑎𝑠𝑎𝑚𝑜𝑠 𝑎 𝑓𝑜𝑟𝑚𝑎 𝑚𝑎𝑡𝑟𝑖𝑐𝑖𝑎𝑙 𝑝𝑎𝑟𝑎 𝑝𝑜𝑑𝑒𝑟 𝑡𝑟𝑎𝑏𝑎𝑗𝑎𝑟 𝑚𝑒𝑗𝑜𝑟
𝑐𝑜𝑛 𝑙𝑎𝑠 𝑒𝑐𝑢𝑎𝑐𝑖𝑜𝑛𝑒𝑠.
a b
𝑛 𝑛 𝑛
𝑨 𝒔𝒐𝒍 𝒕
Tomando logaritmos:
y = Ae Bx ln ( y ) = Bx + ln ( A)
Y hacemos el cambio de variables:
yˆ = ln ( y ) C = ln ( A)
yˆ = Bx + C
Tomando logaritmos:
y = C xA ln(𝑦) = ln(𝐶) + 𝐴 ∗ ln (𝑥)
𝐲̂ = 𝐀𝐱̂ + 𝐁
Tomando logaritmos:
y = C x e Ax ln(𝑦) = ln(𝐶) + ln(𝑥) + 𝐴𝑥 ∗ ln (𝑒)
1
𝑦
ln ( ) = 𝐴𝑥 + ln(𝐶)
𝑥
𝐲̂ = 𝐀𝐱 + 𝐁
Cambio de variables:
𝑦
𝑦 = ln (𝑥 ); 𝐵 = ln(𝐶) → 𝐶 = 𝑒 𝐵
Dado los siguientes datos (x = peso e y = pulso del corazón);
data = [90 62
86 45
67 40
89 55
81 64
75 53];
x = data(:,1);
y = data(:,2);
Sx2 = sum(x.*x);
Sx = sum(x);
Sxy = sum(x.*y);
Sy = sum(y);
A = [Sx2 Sx
Sx length(y)];
t = [Sxy
Sy];
coefs = inv(A)*t;
a = coefs(1); %a = 0.5947
b = coefs(2); %b = 4.7990
xi = [66 91];
yi = a*xi + b;
plot(x,y,'o');
hold on;
plot(xi, yi);
hold off;
Calculate the operation to obtain a new vector and compute a new fitting
yp = 1./(y.*y);
A = [x ones(length(x),1)];
y2 = log(yp);
sol = pinv(A)*y2;
a = exp(sol(2));
b = sol(1);
xi = 65:1:90;
estimacion = a*exp(b*xi);
plot(x,yp,'o');
hold on;
plot(xi,estimacion);
hold off;
A = [x2 ones(length(x2),1)];
y2 = log(y2p./x2);
sol = pinv(A)*y2;
a = exp(sol(2));
b = sol(1);
xi = linspace(min(x2),max(x2));
estimacion = a.*xi.*exp(b*xi);
hold on;
plot(xi,estimacion);
hold off;
MODELO LINEAL GENERALIZADO
Es una generalización del modelo de la regresión lineal ordinaria, salvo que tenemos más de una
entrada.
ei = yi − yˆ ( xi ) = yi − ( a1 xi1 + a2 xi 2 + + am xim + a0 )
2. Definir un criterio de error
e1
n
e
E = ei = (e1 e2 en ) 2 = r t r
2
i =i
e
n
E = r t r = (b − Ax ) (b − Ax ) =
t
b t b − b t ( Ax ) − ( Ax ) b + ( Ax ) ( Ax ) =
t t
b t b − b t Ax − x t At b + x t At Ax
bt Ax = (bt Ax ) = xt At b
t
Por lo que:
E = bt b − 2 x t At b + x t At Ax
3. Derivando el error respecto del vector x de parámetros:
E bt b − 2 x t At b + x t At Ax bt b 2 x t At b x t At Ax
= = − +
x x x x x
Para poder seguir derivando, es preciso saber cómo se deriva respecto de un vector:
z t Mz z t M t b
z t M
z
=M
z
= M + Mt z ( ) z
= M tb
Demostración:
z t M
=M
z
Sean Z y M,
𝑍1
𝑀11 … 𝑀1𝑛
𝑍2
𝑍= ( ) → 𝑍 𝑡 = (𝑍1 𝑍2 … 𝑍𝑛 )1𝑥𝑛 ; 𝑀= ( … … … )
⋮
𝑀𝑛1 … 𝑀𝑛𝑛 𝑛𝑥𝑛
𝑍𝑛 𝑛𝑥1
𝒁𝒕 𝑴 =
(𝒁𝟏 𝑴𝟏𝟏 + 𝒁𝟐 𝑴𝟐𝟏 + ⋯ + 𝒁𝒏 𝑴𝒏𝟏 𝒁𝟏 𝑴𝟏𝟐 + 𝒁𝟐 𝑴𝟐𝟐 + ⋯ + 𝒁𝒏 𝑴𝒏𝟐 … 𝒁𝟏 𝑴𝟏𝒏 + 𝒁𝟐 𝑴𝟐𝒏 + ⋯ + 𝒁𝒏 𝑴𝒏𝒏 )
Ahora derivaremos la matriz 𝑍𝑡 𝑀 con respecto del vector Z. Para poder derivar respeto de un vector,
tenemos que derivar con cada uno de los elementos que conforman el vector:
∂z t M
∂z1
𝑀11 𝑀12 𝑀1𝑛
∂z t M
𝑀21 𝑀22 𝑀2𝑛
∂z2 = ( ⋮ ⋮ ⋮
)
⋮ 𝑀𝑛1 𝑀𝑛2 𝑀𝑛𝑛
∂z t M
( ∂zn )
∂zt M
De aquí deducimos que ∂z𝑛
=𝑀
z t Mz
z
(
= M + Mt z )
Sean Z y M,
𝑍 𝑡 ∗ 𝑀 = ((𝑍1 𝑀11 + 𝑍2 𝑀21 + 𝑍3 𝑀31 + ⋯ + 𝑍𝑛 𝑀𝑛1 ) , (𝑍1 𝑀12 + 𝑍2 𝑀22 + 𝑍3 𝑀32 + ⋯ + 𝑍𝑛 𝑀𝑛2 ),
(𝑍1 𝑀13 + 𝑍2 𝑀23 + 𝑍3 𝑀33 + ⋯ + 𝑍𝑛 𝑀𝑛3 ), … , (𝑍1 𝑀1𝑛 + 𝑍2 𝑀2𝑛 + 𝑍3 𝑀3𝑛 + ⋯ + 𝑍𝑛 𝑀𝑛𝑛 )
𝑍 𝑡 ∗ 𝑀 ∗ 𝑍 = ((𝑍1 𝑀11 +𝑍2 𝑀21 + 𝑍3 𝑀31 + … + 𝑍𝑛 𝑀𝑛1 ) ∗ 𝑍1 + (𝑍1 𝑀12 +𝑍2 𝑀22 + 𝑍3 𝑀32 + … +
𝑍𝑛 𝑀𝑛2 ) ∗ 𝑍2 + (𝑍1 𝑀13 +𝑍2 𝑀23 + 𝑍3 𝑀33 + … + 𝑍𝑛 𝑀𝑛3 ) ∗ 𝑍3 + … + (𝑍1 𝑀1𝑛 +𝑍2 𝑀2𝑛 + 𝑍3 𝑀3𝑛 +
… + 𝑍𝑛 𝑀𝑛𝑛 ) ∗ 𝑍𝑛 )
𝜕𝑧 𝑡 𝑀𝑧
= 2𝑀11 𝑍1 + 𝑀21 𝑍2 + 𝑀31 𝑍3 + … + 𝑀𝑛1 𝑍𝑛 + 𝑀12 𝑍2 + 𝑀13 𝑍3 + ⋯ 𝑀1𝑛 𝑍𝑛
𝜕𝑧1
= 2𝑀11 𝑍1 + (𝑀21 + 𝑀12 )𝑍2 + (𝑀31 + 𝑀13 )𝑍3 + … + (𝑀𝑛1 + 𝑀1𝑛 )𝑍𝑛
𝜕𝑧 𝑡 𝑀𝑧
= 𝑀21 𝑍1 + 𝑀12 𝑍1 + 2𝑀22 𝑍2 + 𝑀32 𝑍3 + ⋯ + 𝑀𝑛2 𝑍𝑛 + 𝑀23 𝑍3 + ⋯ + 𝑀2𝑛 𝑍𝑛
𝜕𝑧2
= (𝑀12 + 𝑀21 )𝑍1 + 2𝑀22 𝑍2 + (𝑀23 + 𝑀32 )𝑍3 + ⋯ + (𝑀2𝑛 + 𝑀𝑛2 )𝑍𝑛
⋯
𝜕𝑧 𝑡 𝑀𝑧
= 𝑀𝑛1 𝑍1 + 𝑀𝑛2 𝑍2 + 𝑀𝑛3 𝑍3 + ⋯ + 𝑀1𝑛 𝑍1 + 𝑀2𝑛 𝑍2 + 𝑀3𝑛 𝑍3 + ⋯ + 𝑀𝑛𝑛 𝑍𝑛
𝜕𝑧𝑛
= (𝑀𝑛1 + 𝑀1𝑛 )𝑍1 + (𝑀𝑛2 + 𝑀2𝑛 )𝑍2 + (𝑀𝑛3 + 𝑀3𝑛 )𝑍3 + ⋯ + 2𝑀𝑛𝑛 𝑍𝑛
∂z t M
∂z1
2𝑀11 𝑍1 + (𝑀21 + 𝑀12 )𝑍2 + (𝑀31 + 𝑀13 )𝑍3 + … + (𝑀𝑛1 + 𝑀1𝑛 )𝑍𝑛
∂z t M
(𝑀12 + 𝑀21 )𝑍1 + 2𝑀22 𝑍2 + (𝑀23 + 𝑀32 )𝑍3 + ⋯ + (𝑀2𝑛 + 𝑀𝑛2 )𝑍𝑛 )
∂z2 = ( ⋯
⋮ (𝑀 + 𝑀 )𝑍 + (𝑀 + 𝑀 )𝑍 + (𝑀𝑛3 + 𝑀3𝑛 )𝑍3 + ⋯ + 2𝑀𝑛𝑛 𝑍𝑛
𝑛1 1𝑛 1 𝑛2 2𝑛 2
∂z t M
( ∂zn )
Sean Z y M,
𝑍1
𝑀11 … 𝑀𝑛1
𝑍
𝑍 = ( 2) 𝑡
→ 𝑍 = (𝑍1 𝑍2 … 𝑍𝑛 )1𝑥𝑛 ; 𝑡
𝑀 = ( … … … )
⋮
𝑀1𝑛 … 𝑀𝑛𝑛 𝑛𝑥𝑛
𝑍𝑛 𝑛𝑥1
𝒁𝒕 𝑴 =
(𝒁𝟏 𝑴𝟏𝟏 + 𝒁𝟐 𝑴𝟏𝟐 + ⋯ + 𝒁𝒏 𝑴𝟏𝒏 𝒁𝟏 𝑴𝟐𝟏 + 𝒁𝟐 𝑴𝟐𝟐 + ⋯ + 𝒁𝒏 𝑴𝟐𝒏 … 𝒁𝟏 𝑴𝒏𝟏 + 𝒁𝟐 𝑴𝒏𝟐 + ⋯ + 𝒁𝒏 𝑴𝒏𝒏 )
𝒁𝒕 𝑴𝒃 = 𝒚𝟏 (𝒁𝟏 𝑴𝟏𝟏 + 𝒁𝟐 𝑴𝟏𝟐 + ⋯ + 𝒁𝒏 𝑴𝟏𝒏 ) + 𝒚𝟐 (𝒁𝟏 𝑴𝟐𝟏 + 𝒁𝟐 𝑴𝟐𝟐 + ⋯ + 𝒁𝒏 𝑴𝟐𝒏 ) + ⋯ + 𝒚𝒏 (𝒁𝟏 𝑴𝒏𝟏 +
𝒁𝟐 𝑴𝒏𝟐 + ⋯ + 𝒁𝒏 𝑴𝒏𝒏 )
Ahora derivaremos la matriz 𝑍𝑡 𝑀𝑏 con respecto del vector Z. Para poder derivar respeto de un vector,
tenemos que derivar con cada uno de los elementos que conforman el vector:
∂z t Mb
= 𝑴𝟏𝟏 𝒚𝟏 + 𝑴𝟐𝟏 𝒚𝟐 + ⋯ + 𝑴𝒏𝟏 𝒚𝒏
∂z1
∂z t Mb
= 𝑴𝟏𝟐 𝒚𝟏 + 𝑴𝟐𝟐 𝒚𝟐 + ⋯ + 𝑴𝒏𝟐 𝒚𝒏
∂z2
⋯
∂z t Mb
= 𝑴𝟏𝒏 𝒚𝟏 + 𝑴𝟐𝒏 𝒚𝟐 + ⋯ + 𝑴𝒏𝒏 𝒚𝒏
∂z𝑛
∂z t Mb
∂z1
∂z t Mb 𝑴𝟏𝟏 𝒚𝟏 + 𝑴𝟐𝟏 𝒚𝟐 + ⋯ + 𝑴𝒏𝟏 𝒚𝒏
∂z2 = ( 𝑴𝟏𝟐 𝒚𝟏 + 𝑴𝟐𝟐 𝒚…𝟐 + ⋯ + 𝑴𝒏𝟐 𝒚𝒏 )
⋮ 𝑴𝟏𝒏 𝒚𝟏 + 𝑴𝟐𝒏 𝒚𝟐 + ⋯ + 𝑴𝒏𝒏 𝒚𝒏
t
∂z Mb
( ∂zn )
∂zt Mb
De aquí deducimos que ∂Z
= 𝑀𝑡 𝑏
Una vez obtenidos tenemos que,
E bt b − 2 x t At b + x t At Ax bt b 2 x t At b x t At Ax
= = − +
x x x x x
bt b
=0
x
2x A b
t t
= 2 At b
E
= 0 − 2 At b + 2 At Ax
x x
x A Ax
t t
( )
= At A + (At A) x = 2 At Ax
t
x
Igualamos a 0,
−2 At b + 2 At Ax = 0
At b = At Ax
Es la y
MODELO NO LINEAL:
Anteriormente, en el modelo lineal calculábamos el error para así obtener una pendiente que se
acercara a nuestros datos. Por ejemplo, aquí nuestro error sería algo mayor:
Y en este por ejemplo nuestro error sería menor, como podemos observar se acerca mucho a
nuestros datos:
Que el error se acerque a 0 es lo que intentamos conseguir, para obtener una mayor precisión. Bien
pues con el modelo no lineal tenemos que conseguir los mismo, obtener un error pequeño, y ¿Cómo
lo hacemos?
Este sería nuestro error, luego tendríamos que encontrar el mínimo error en ella.
Si lo dibujamos observamos lo siguiente:
Esto en la vida real nos puede servir para programar por ejemplo una red neuronal. Dado un ajuste
de ella, buscaremos la que menor error nos devuelva.
[x,y]=meshgrid(-3:0.1:3,-4:0.1:4);
z = (x.^4 - 3*x.^2 + x + y.*y + x.*y);
mesh(x,y,z)
while(1),
close all
pcolor(x,y,z),shading flat;hold on;
sol = ginput(1)';
for i=1:100, % Condición de parada
x=sol(1,i);
y=sol(2,i);
dx = 4 * x.^3 - 6*x + 1 + y;
dy = 2 * y + x;
sol(:,i+1) = sol(:,i) - 0.1 * [dx;dy]; % Siguiente interación
plot([sol(1,i) sol(1,i+1)],[sol(2,i) sol(2,i+1)],'r', 'LineWidth',3)
pause
end;
end;
Dada la función determina el mínimo.
while(1),
close all
pcolor(x,y,z),shading flat;hold on;
sol = ginput(1)’;
for i=1:100,
%subs asigna un valor a una variable
%diff devuelve la derivada parcial respecto de una variable
dx = subs(diff(Z,X),{X Y},{sol(1,i) sol(2,i)});
dy = subs(diff(Z,Y),{X Y},{sol(1,i) sol(2,i)});
sol(:,i+1) = sol(:,i) - 0.1 * [dx;dy];
plot([sol(1,i) sol(1,i+1)],[sol(2,i) sol(2,i+1)],'r', 'LineWidth',3)
pause
end;
end;
MÉTODO DE NEWTON:
Es parecido al anterior método salvo que esta vez tenemos que realizar dos derivadas parciales.
[x,y]=meshgrid(-3:0.1:3,-4:0.1:4);
z = (x.^4 - 3*x.^2 + x + y.*y + x.*y);
while(1),
close all
pcolor(x,y,z),shading flat;hold on;
sol = ginput(1)';
for i=1:100,
x=sol(1,i);
y=sol(2,i);
dx = 4 * x.^3 - 6*x + 1 + y;
dy = 2 * y + x;
dxx = 12 * x.^2 - 6; dxy = 1;
dyx = 1; dyy = 2;
H = [dxx dxy;dyx dyy];
sol(:,i+1) = sol(:,i) - 0.1 * inv(H) * ([dx;dy]);
plot([sol(1,i) sol(1,i+1)],[sol(2,i) sol(2,i+1)],'r')
pause
end;
end;
close all
syms X Y;
%x=-1:0.01:1;
%y=-1:0.01:1;
[x,y]=meshgrid(-10:0.01:10,-10:0.01:10);
a=100*(x.^2 - y.^2) + (1+x).^2;
A=100*(X.^2 - Y.^2) + (1+X).^2;
mesh(a); xticklabels([-1,-0.5,0,0.5,1]); yticklabels([-1,-0.5,0,0.5,1])
pcolor(x,y,a),shading flat;hold on;
sol = [10 ; 10];
finded = false;
i = 1;
while ~finded,