let f(x) = [2x^2, 3y^5]
I know how to calculate the derivative of f(x), which will be [d/dx 2x^2, d/dx 3y^5].
Is there a similar process being done when calculating the gradient of f(x)? If not, then how do you calculate the gradient of f(x)?
The gradient of a vector is a matrix
grad([f(x,y), g(x,y)]) = | df/dx dg/dx |
| |
| df/dy dg/dy |
Related
I am using the svyglm function to run a regression model. I have an interaction term which is a categorical variable of yes and no. The regression model is showing the estimates for no but I would like to analyse the yes. I was wondering how I can do that? Thanks for your help!
This is an image of the dataset covid.surveydesgin = enter image description here
The Data for this column is just Yes/No for 3000 row, I am unable to share the whole dataset.
YES = 692
NO = 2996
I tried to re-level using fct_relevel() and lapply() but that didn't work, any suggestions would be really appreciated.
Regression1<-svyglm(formula = ca_scghq1_dv~(None+Average+Above_Average+Astronomical)*ca_furlough, design = covid.surveydesgin)
summary(Regression1)
Call: svyglm(formula = ca_scghq1_dv ~ (None + Average + Above_Average + Astronomical) * ca_furlough, design = covid.surveydesgin)
Coefficients:
Estimate |Std. Error |t value |Pr(>|t|) |
(Intercept) 12.2457 0.5321 23.012 < 2e-16 ***|
|ca_furloughNo |0.5957 | 0.5731 | 1.039 |0.29906 |
|NoneTRUE:ca_furloughNo |-1.4100 |0.9212 |-1.531 |0.12644 |
|AverageTRUE:ca_furloughNo |1.7233 |1.3013 |1.324 |0.18596 |
|Above_AverageTRUE:ca_furloughNo |0.2059 |1.6990 |0.121 |0.90357 |
|AstronomicalTRUE:ca_furloughNo |1.8439 |2.2777 |0.810 |0.41855 |
Can I provide a starting point y to one of the eigen's solver for Ax = b?
I want to get an exact solution x that is close to the starting point y.
When would solving Ax = b with Newton's method (without line search) not converge?
Thanks.
Solve Ax = b for a solution that minimize L2 norm of x - y.
Given a y, that means minimizing L2 norm of x - y, subjected to the constraint Ax = b.
By Lagrange multiplier I get the following block matrix equation.
The vector u are one half of the lagrange multipliers.
/ A 0 \ / x \ / b \
| | | | = | |
\ I A^T / \ u / \ y /
Probably not the fastest way.
bestappr(sqrt(13),30) gives 18/5 for any precision I tried, but a closer approximation is 101/28. Is there anything I missed?
For PARI/GP,
By definition, a/b is the best rational approximation to x if |b x - a| < |vx - u| for all integers (u,v) with 0 < v <= B.
(see ??bestappr).
So in this case
|5x - 18| < |28x - 101|
and so gp considers 18/5 the better approximation. The next approximation it considers better is 119/33 followed by 137/38.
I've got a bit of a weird set of conditions I need to fit a curve to. I've tried looking it up elsewhere but I'm not even sure I'm using the right lingo. Any help is much appreciated.
I'm trying to fit a polynomial curve to a set of four points. Three of the points are known, but the fourth one is a little tricky. I have the x value for the maximum y value, but I don't know what the maximum y value is. For example, let's say there are known points at (0,0), (1,1), and (4,0). The maximum y value is at x=3 so the fourth point is (3, ymax). How would I fit a 4th order polynomial curve to those conditions? Thanks in advance.
Actually it is possible since you require the y value at x=3 should be maximum. So, a degree 4 polynomial has 5 coefficients to be determined and you have the following equations:
y(0) = 0
y(1) = 1
y(4) = 0
dy/dx(3) = 0 (first derivative at x=3 should be 0)
d2y/dx2(3) < 0 (2nd derivative at x=3 should be negative)
So, pick any negative value for d2y/dx2 at x=3 and solve the 5 linear equations and you will get one degree 4 polynomial. Note that the y value at x=3 obtained this way is only a local maximum, not a global maximum.
Filling in the algebra from #fang's answer (a little elementary calculus, some algebra, and some linear algebra):
y = a+b*x+c*x^2+d*x^3+e*x^4
y(0) = 0 -> a=0
Set a=0 for the rest of the computations.
y(1) = 1 -> b+c+d+e = 1
y(4) = 0 -> 4*b+16*c+64*d+256*e=0
dy/dx(3)=0 ->
b+2*x*c+3*x^2*d+4*x^3*e=0 ->
b+6*c+27*d+108*e=0
d2y/dx2(3)<0 = 2*c+6*d*x+12*e*x^2 < 0
= 2*c+18*d+108*e < 0
Pick a negative value V for the last expression, say -1:
V <- -1
A <- matrix(c(1, 1, 1, 1,
4,16,64,256,
1, 6,27,108,
0, 2,18,108),
ncol=4,byrow=TRUE)
b <- c(1,0,0,V)
(p <- solve(A,b))
## [1] 2.6400000 -2.4200000 0.8933333 -0.1133333
x <- seq(-0.5,5,length=101)
m <- model.matrix(~poly(x,degree=4,raw=TRUE))
y <- m %*% c(0,p)
Plot results:
par(las=1,bty="l")
plot(x,y,type="l")
points(c(0,1,4),c(0,1,0))
abline(v=3,lty=2)
Picking a larger magnitude (more negative) value for V will make the solution more sharply peaked at y=3.
I'm new to R and I need to plot the quadratic matrix equation:
x^T A x + b^T x + c = 0
in R^2, with A being a 2x2, b a 2x1, and c a constant. The equation is for a boundary that defines classes of points. I need to plot that boundary for x0 = -6...6, x1 = -4...6. My first thought was generate a bunch of points and see where they are zero, but it depends on the increment between the numbers (most likely I'm not going guess what points are zero).
Is there a better way than just generating a bunch of points and seeing where it is zero or multiplying it out? Any help would be much appreciated,
Thank you.
Assuming you have a symmetric matrix A,
eg
# A = | a b/2 |
# | b/2 c |
and your equation represents a conic section, you can use the conics package
What you need is a vector of coefficients c(a,b,c,d,e,f) representing
a.x^2 + b*x*y + c*y^2 + d*x + e*y + f
In your case, say you have
A <- matrix(c(2,1,1,2))
B <- c(-20,-28)
C <- 10
# create the vector
v <- append(c(diag(A),B,C),A[lower.tri(A)]*2), 1)
conicPlot(v)
You could easily wrap the multiplication out into a simple function
# note this does no checking for symmetry or validity of arguments
expand.conic <- function(A, B, C){
append(c(diag(A),B,C),A[lower.tri(A)]*2), 1)
}