Why is this answer incorrect? - linear-regression

I calculated an answer in accordance with the provided formula. The first try, utilizing X2 yielded -0.37 and that was correct, however, on this next try, utilizing X1, which yielded 0.31 was marked as incorrect. Can someone please tell me why that is the case?
Posted below was my Octave code that generated my answer:
X1 = 7921
midterm_avr = mean([7921,5184,8836,4761])
midterm_range = range([7921,5184,8836,4761])
poly_regress = (X1 - midterm_avr) / midterm_range
sprintf('%.*f', 2, ((X1 - midterm_avr) / midterm_range))
This is the result that I received:
X1 = 7921
midterm_avr = 6675.5
midterm_range = 4075
poly_regress = 0.30564
ans = 0.31

Related

Function fzero in Matlab is not converging

I am solving a problem in my Macroeconomics class. Consider the following equation:
Here, k is fixed and c(k) was defined through the ```interp1''' function in Matlab. Here is my code:
beta = 0.98;
delta = 0.13;
A = 2;
alpha = 1/3;
n_grid = 1000; % Number of points for capital
k_grid = linspace(5, 15, n_grid)';
tol = 1e-5;
max_it = 1000;
c0 = ones(n_grid, 1);
new_k = zeros(n_grid, 1);
dist_c = tol + 1;
it_c = 0;
while dist_c > tol && it_c < max_it
c_handle = #(k_tomorrow) interp1(k_grid, c0, k_tomorrow, 'linear', 'extrap');
for i=1:n_grid
% Solve for k'
euler = #(k_tomorrow) (1/((1-delta)* k_grid(i) + A * k_grid(i)^alpha - k_tomorrow)) - beta*(1-delta + alpha*A*k_tomorrow^(alpha - 1))/c_handle(k_prime);
new_k(i) = fzero(euler, k_grid(i)); % What's a good guess for fzero?
end
% Compute new values for consumption
new_c = A*k_grid.^alpha + (1-delta)*k_grid - new_k;
% Check convergence
dist_c = norm(new_c - c0);
c0 = new_c;
it_c = it_c + 1;
end
When I run this code, for some indexes $i$, it runs fine and fzero can find the solution. But for indexes it just returns NaN and exits without finding the root. This is a somewhat well-behaved problem in Economics and the solution we are looking indeed exists and the algorithm I tried to implement is guaranteed to work. But I don't have much experience with solving this in MATLAB and I guess I have a silly mistake somewhere. Any ideas on how to procede?
This is the typical error message:
Exiting fzero: aborting search for an interval containing a sign change
because complex function value encountered during search.
(Function value at -2.61092 is 0.74278-0.30449i.)
Check function or try again with a different starting value.
Thanks a lot in advance!
The only term that can produce complex numbers is
k'^(alpha - 1) = k'^(-2/3)
You probably want the result according to the real variant of the cube root, which you could get as
sign(k') * abs(k')^(-2/3)
or more generally and avoiding divisions by zero
k' * (1e-16+abs(k'))^(alpha - 2)

Bayesian Lasso using PyMC3

I'm trying to reproduce the results of this tutorial (see LASSO regression) on PyMC3. As commented on this reddit thread, the mixing for the first two coefficients wasn't good because the variables are correlated.
I tried implementing it in PyMC3 but it didn't work as expected when using Hamiltonian samplers. I could only get it working with the Metropolis sampler, which achieves the same result as PyMC2.
I don't know if it's something related to the fact that the Laplacian is peaked (discontinuous derivative at 0), but it worked perfectly well with Gaussian priors. I tried with or without MAP initialization and the result is always the same.
Here is my code:
from pymc import *
from scipy.stats import norm
import pylab as plt
# Same model as the tutorial
n = 1000
x1 = norm.rvs(0, 1, size=n)
x2 = -x1 + norm.rvs(0, 10**-3, size=n)
x3 = norm.rvs(0, 1, size=n)
y = 10 * x1 + 10 * x2 + 0.1 * x3
with Model() as model:
# Laplacian prior only works with Metropolis sampler
coef1 = Laplace('x1', 0, b=1/sqrt(2))
coef2 = Laplace('x2', 0, b=1/sqrt(2))
coef3 = Laplace('x3', 0, b=1/sqrt(2))
# Gaussian prior works with NUTS sampler
#coef1 = Normal('x1', mu = 0, sd = 1)
#coef2 = Normal('x2', mu = 0, sd = 1)
#coef3 = Normal('x3', mu = 0, sd = 1)
likelihood = Normal('y', mu= coef1 * x1 + coef2 * x2 + coef3 * x3, tau = 1, observed=y)
#step = Metropolis() # Works just like PyMC2
start = find_MAP() # Doesn't help
step = NUTS(state = start) # Doesn't work
trace = sample(10000, step, start = start, progressbar=True)
plt.figure(figsize=(7, 7))
traceplot(trace)
plt.tight_layout()
autocorrplot(trace)
summary(trace)
Here is the error I get:
PositiveDefiniteError: Simple check failed. Diagonal contains negatives
Am I doing something wrong or is the NUTS sampler not supposed to work on cases like this?
Whyking from the reddit thread gave the suggestion to use the MAP as scaling instead of the state and it actually worked wonders.
Here is a notebook with the results and the updated code.

numerical computation problems in matlab

in this question i am addressing to numerical computation problems in matlab and want to get experience how to avoid this problems/errors in future
for example let consider following simple codes
t = 0.4 + 0.1 - 0.5
t =
0
it works fine,but
u = 0.4 - 0.5 + 0.1
u =
2.7756e-17
of course in mind it is also 0,but why not in first calculation got the same result?or what is difference?also please look
v = (sin(2*pi) = = sin(4*pi))
v = (sin(2*pi)==sin(4*pi))
v =
0
it shows that sine function is not periodic,so what is general advice in this case?introduce some epsilon?like
V=((sin(2*pi)-sin(4*pi))<eps)
V =
0
or
EPS=0.000000000000001
EPS =
1.0000e-15
>> V=((sin(2*pi)-sin(4*pi))<EPS)
V =
1
please help me
It's normal you get these results, because floating-point relative accuracy in Matlab is
eps('double')
ans =
2.2204e-16
For V=((sin(2*pi)-sin(4*pi))<eps), because
sin(2*pi)-sin(4*pi)
ans =
2.4493e-16
which is larger than eps('double'), so its result will be V=0.
And for V=((sin(2*pi)-sin(4*pi))<EPS), because EPS>2.4493e-16, so its result will be V=1.

CrossProduct and DotProduct - Expand these equations

I'm trying to use a formulae from http://www.blackpawn.com/texts/pointinpoly/default.html the upper of the two options. (I have already used the lower).
I just can not seem to wrap my head around the CrossProduct(b-a, p1-a) etc. Could someone Please expand these for me. They are below.
function SameSide(p1,p2, a,b)
cp1 = CrossProduct(b-a, p1-a)
cp2 = CrossProduct(b-a, p2-a)
if DotProduct(cp1, cp2) >= 0 then return true
else return false
as I understand it they should come out to this.
## Using the following as p=x,z, a=x,z, b=x,z, c=x,z
## p=4,1 a=2,0 b=4,3 c=0,4
function SameSide(px, pz, ax, az, bx, bz, cx, cz){
cp1x=(cx-bx*px-bx)
cp1z=(cz-bz*pz-bz)
cp2x=(cx-bx*ax-bx)
cp2z=(cz-bz*az-bz)
DotProd=(cp1x*cp2x+cp1z*cp2z)
}
But when trying this in Excel I get wrong answers again.
Please help! 8-|
Your DotProduct computation is correct. However, the cross product of 2 vectors v and w is :
cpx = vy*wz - vz*wy
cpy = vz*wx - vx*wz
cpz = vx*wy - vy*wx
where in your case, vx = bx-ax vy = by-ay and vz=bz-az, and wx=p1x-ax (or respectively p2x-ax) and similarly for y and z.

solve In Matlab a quadratic equation with very small coefficients

I'm implementing a code in matlab to solve quadratic equations, using the resolvent formula:
Here´s the code:
clear all
format short
a=1; b=30000000.001; c=1/4;
rdelta=sqrt(b^2-4*a*c);
x1=(-b+rdelta)/(2*a);
x2=(-b-rdelta)/(2*a);
fprintf(' Roots of the polynomial %5.3f x^2 + %5.3f x+%5.3f \n',a,b,c)
fprintf ('x1= %e\n',x1)
fprintf ('x2= %e\n\n',x2)
valor_real_x1= -8.3333e-009;
valor_real_x2= -2.6844e+007;
error_abs_x1 = abs (valor_real_x1-x1);
error_abs_x2 = abs (valor_real_x2-x2);
error_rel_x1 = abs (error_abs_x1/valor_real_x1);
error_rel_x2 = abs (error_abs_x2/valor_real_x2);
fprintf(' absolute_errorx1 = |real value - obtained value| = |%e - %e| = %e \n',valor_real_x1,x1,error_abs_x1)
fprintf(' absolute_errorx2 = |real value - obtained value| = |%e - %e| = %e \n\n',valor_real_x2,x2,error_abs_x2)
fprintf(' relative error_x1 = |absolut error / real value| = |%e / %e| = %e \n',error_abs_x1,valor_real_x1,error_rel_x1 )
fprintf(' relative_error_x2 = |absolut error / real value| = |%e / %e| = %e \n',error_abs_x2,valor_real_x2,error_rel_x2)
The problem I have is that it gives me an exact solution, ie for values ​​a = 1, b = 30000000,001 c = 1/4, the values ​​of the roots are:
Roots of the polynomial 1.000 x^2 + 30000000.001 x+0.250
x1= -9.313226e-009
x2= -3.000000e+007
Knowing that the exact value of the roots of the polynomial are:
x1= -8.3333e-009
x2= -2.6844e+007
Which gives me the following errors in the absolute and relative precision of the calculations:
absolute_errorx1 = |real value - obtained value| = |-8.333300e-009 - -9.313226e-009| = 9.799257e-010
absolute_errorx2 = |real value - obtained value| = |-2.684400e+007 - -3.000000e+007| = 3.156000e+006
relative error_x1 = |absolut error / real value| = |9.799257e-010 / -8.333300e-009| = 1.175916e-001
relative_error_x2 = |absolut error / real value| = |3.156000e+006 / -2.684400e+007| = 1.175682e-001
My question is: Is there an optimum method to obtain the roots of a quadratic equation?, ie I can make changes to my code to reduce the relative error between the expected solution and the resulting solution?
Using the quadratic formula directly in this cases results in a large loss of numerical precision from subtracting two values of very similar magnitude. This is because the expression
sqrt(b*b - 4*a*c)
is nearly the same as b. So you should use only one of these two roots, the one that does not involve subtracting two very close values, and for the other root you can use (for instance) the fact that the product of roots of a quadratic is c/a. I'll let you fill in the gaps.
Why does this sound like a homework problem from a first class in numerical analysis?
It has been a while since I was that young, but as I recall there is a trick. Anyway, you are wrong. The true roots of that polynomial are
solve('x^2 + 30000000.001*x + 0.25')
ans =
-30000000.000999991666666666944442
-0.0000000083333333330555578703796293981491
How well does roots do here?
p = [1 30000000.001 1/4];
format long g
roots(p)
ans =
-30000000.001
-8.33333333305556e-09
That actually seems pretty good. How does HPF do?
DefaultNumberOfDigits 64
a = hpf(1);
b = hpf('30000000.001');
c = hpf('0.25');
r1 = (-b + sqrt(b*b - 4*a*c))/(2*a)
r1 =
-0.000000008333333333055557870379629398149125529835186899898569329967
r2 = (-b - sqrt(b*b - 4*a*c))/(2*a)
r2 =
-30000000.000999991666666666944442129620370601850874470164813100101
Yep, HPF works nicely enough too.
So what happens when you use double precision numbers and the standard formula? Yeah, crapola arrives.
a = 1;
b = 30000000.001;
c = 0.25;
>> r1 = (-b + sqrt(b*b - 4*a*c))/(2*a)
r1 =
-7.45058059692383e-09
>> r2 = (-b - sqrt(b*b - 4*a*c))/(2*a)
r2 =
-30000000.001
Again, massive subtractive cancellation eats away at the result. (I seem to recall that was the problem you had in your last question.)
There is a trick you can use. See that the large solution was well estimated, just not the one near zero. So, what happens if you solved for the roots of fliplr(p) using the quadratic formula? How does this solve your problem? What transformation is implicitly done when you do that? (Sorry, but I won't do your homework. I think the above was enough of a hint anyway.)
i think your "real" values might be wrong (or maybe it's a precision thing... I dunno)
a*(valor_real_x1^2)+b*(valor_real_x1)+c
ans =
9.9999e-07
a*(valor_real_x2^2)+b*(valor_real_x2)+c
ans =
-8.4720e+13
A nice formula for this problem:
var q = sqrt(c*a)/b;
var f = .5 + .5 *sqrt(1-4*q*q);
var x1=-b*f/a;
var x2=-c/(f*b);