I am new to linear algebra and I am trying to solve a system of three equations with five unknowns. The system I have is the following:
x1 + x2 + x3 + x4 + x5 = 1
-x1 + x2 + x3 - 2x4 - 2x5 = 1
2x1 + 2x2 - x3 - x4 + x5 = 1
So what I did was set up the augmented matrix like this:
1 1 1 1 1 1
-1 1 1 -2 -2 1
2 2 -1 -1 1 1
Then I try to obtain an identity matrix on the left side and end up with the following:
1 0 0 3/2 3/2 0
0 1 0 -3/2 -5/6 2/3
0 0 1 1 1/3 1/3
So I think the answer is x1 = 0, x2 = 2/3 and x3 = 1/3
But when I look in my answer sheet it reads:
(x1, x2, x3, x4, x5) = (0, 2/3, 1/3, 0, 0) + s(−3/2, 3/2, −1, 1, 0) + t(−3/2, 5/6, −1/3, 0, 1)
I have no idea how to interpret this. My x1,x2,x3 seems to match the first three in the first five-tuple but what are the other two five-tuples? Can someone explain what I am missing here? I would highly appreciate it.
A system of equations can be represented in matrix form as
Ax = b
where A is the matrix of coefficients, x is the column vector (x1, ..., xn) and b is the column vector with as many entries as equations are.
When b is not 0 we say that the system is not homogeneous. The associated homogeneous system is
Ax = 0
where the 0 on the right is again a column vector.
When you have a non-homogeneous system, like in this case, the general solution has the form
P + G
where P is any particular solution and G is the generic solution of the homogeneous system.
In your case the vector
P = (0, 2/3, 1/3, 0, 0)
satisfies all the equations and is therefore a valid particular solution.
The other two vectors (−3/2, 3/2, −1, 1, 0) and (−3/2, 5/6, −1/3, 0, 1) satisfy the homogeneous equations (take a moment to check this). And since there are 3 (independent) equations with 5 unknowns (x1..x5), the space of homogenous solutions can be generated by these two vectors (again because they are independent).
So, to describe the space of all homogeneous solutions you need two scalar variables s and t. In other words
G = s(−3/2, 3/2, −1, 1, 0) + t(−3/2, 5/6, −1/3, 0, 1)
will generate all homogeneous solutions as s and t take all posible real values.
Related
x <- abs(rnorm(8))
C <- (x[1]*x[2]*x[3])^(1/3)
y <- log(x/C)
Is it mathematically possible to determine x[1:3] given you only have y? Here, x and y are always vectors of length 8. I should note that x is known for some of my dataset, which could be useful to find a solution for the other portion of the data where x is unknown. All of my code is implemented in R, so R code would be appreciated if this is solvable!
Defining f as
f <- function(x) {
C <- (x[1]*x[2]*x[3])^(1/3)
log(x/C)
}
we first note that if k is any scalar constant then f(x) and f(k*x) give the same result so if we have y = f(x) we can't tell whether y came from x or from k*x. That is, y could have come from any scalar multiple of x; therefore, we cannot recover x from y.
Linear formulation
Although we cannot recover x we can determine x up to a scalar multiple. Define the matrix A:
ones <- rep(1, 8)
a <- c(1, 1, 1, 0, 0, 0, 0, 0)
A <- diag(8) - outer(ones, a) / 3
in which case f(x) equals:
A %*% log(x)
Inverting formula
From this formula, given y and solving for x, the value of x would equal
exp(solve(A) %*% y) ## would equal x if A were invertible
if A were invertible but unfortunately it is not. For example, rowSums(A) equals zero which shows that the columns of A are linearly dependent which implies non-invertibility.
all.equal(rowSums(A), rep(0, 8))
## [1] TRUE
Rank and nullspace
Note that A is a projection matrix. This follows from the fact that it is idempotent, i.e. A %*% A equals A.
all.equal(A %*% A, A)
## [1] TRUE
It also follows from the fact that its eigenvalues are all 0 and 1:
zapsmall(eigen(A)$values)
## [1] 1 1 1 1 1 1 1 0
From the eigenvalues we see that A has rank 7 (the number of nonzero eigenvalues) and the dimension of the nullspace is 1 (the number of zero eigenvalues).
Another way to see this is that knowing that A is a projection matrix its rank equals its trace, which is 7, so its nullspace must have dimension 8-7=1.
sum(diag(A)) # rank of A
## [1] 7
Taking scalar multiples spans a one dimensional space so from the fact that the nullspace has dimension 1 it must be the entirely of the values that map into the same y.
Key formula
Now replacing solve in ## above with the generalized inverse, ginv, we have this key formula for our approximation to x given that y = f(x) for some x:
library(MASS)
exp(ginv(A) %*% y) # approximation to x accurate up to scalar multiple
or equivalently if y = f(x)
exp(y - mean(y))
While these do not give x they do determine x up to a scalar multiple. That is if x' is the value produced by the above expressions then x equals k * x' for some scalar constant k.
For example, using x and y from the question:
exp(ginv(A) %*% y)
## [,1]
## [1,] 1.2321318
## [2,] 0.5060149
## [3,] 3.4266146
## [4,] 0.1550034
## [5,] 0.2842220
## [6,] 3.7703442
## [7,] 1.0132635
## [8,] 2.7810703
exp(y - mean(y)) # same
## [1] 1.2321318 0.5060149 3.4266146 0.1550034 0.2842220 3.7703442 1.0132635
## [8] 2.7810703
exp(y - mean(y))/x
## [1] 2.198368 2.198368 2.198368 2.198368 2.198368 2.198368 2.198368 2.198368
Note
Note that y - mean(y) can be written as
B <- diag(8) - outer(ones, ones) / 8
B %*% y
and if y = f(x) then y must be in the range of A so we can verify that:
all.equal(ginv(A) %*% A, B %*% A)
## [1] TRUE
It is not true that the matrix ginv(A) equals B. It is only true that they act the same on the range of A which is all that we need.
No, it's not possible. You have three unknowns. That means you need three independent pieces of information (equations) to solve for all three. y gives you only one piece of information. Knowing that the x's are positive imposes a constraint, but doesn't necessarily allow you to solve. For example:
x1 + x2 + x3 = 6
Doesn't allow you to solve. x1 = 1, x2 = 2, x3 = 3 is one solution, but so is x1 = 1, x2 = 1, x3 = 4. There are many other solutions. [Imposing your "all positive" constraint would rule out solutions such as x1 = 100, x2 = 200, x3 = -294, but in general would leave more than one remaining solution.]
x1 + x2 + x3 = 6,
x1 + x2 - x3 = 0
Constrains x3 to be 3, but allows arbitrary solutions for x1 and x2, subject to x1 + x2 = 3.
x1 + x2 + x3 = 6,
x1 + x2 - x3 = 0,
x1 - x2 + x3 = 2
Gives the unique solution x1 = 1, x2 = 2, x3 = 3.
I'm new to simulating data in R and would like to know how to generate a polynomial separator of degree 2 in R. This is the question:
Generate a Data Set by Simulations:
We seek to generate 5000 cases x^1 ... x^5000 in R^4
each case x = [ x1 x2 x3 x4 ] has 4 numerical features.
using random sampling of a uniform distribution over the interval [-2, +2]:
select 16 random numbers Aij with i= 1 2 3 4 and j = 1 2 3 4; select 4 random numbers Bi with i= 1 2 3 4; select 1 random number c
Define the polynomial of degree 2 in the 4 variables x1 x2 x3 x4 as follows:
Pol(x) = ∑i ∑j Aij xi xj + ∑i Bi xi + c/20
so far I've generated A, B, and C:
A <- matrix(runif(16, -2, 2), nrow=4, ncol=4)
B <- runif(4, -2, 2)
C <- runif(1, -2, 2)
But I'm having trouble finding out how to define a polynomial using the values I generated.
I am trying to do a piecewise linear OLS regression analysis in R, with one breakpoint. I have the following regression formula and restrictions:
Where D is a dummy. I would like to impose a restriction, so that the regression lines are continous (with a break point). The restriction below would work fine.
My question is, how do I formulate that in the lm()-function in R? I have previously tried the "segmented" package, but I need to interpret the intercepts for both lines in a regression summary.
I have provided some data below. The breakpoint here is 0, so d is 1 for x >= 0 .
x y d
1 4.3047451 11.2660463 1
2 7.0062423 -3.2897982 1
3 2.7862009 -2.8232636 1
4 -0.8662964 0.4051925 0
5 -0.9553261 -0.9228929 0
6 -1.6626654 3.5044546 0
7 3.4906905 1.4961349 1
8 -0.7072658 -0.2758436 0
9 -7.0054069 -1.3041742 0
10 -2.2510701 -0.1848814 0
11 -13.3617905 -0.2113756 0
12 4.1001251 0.2845748 1
13 -4.6575944 -1.1603290 0
14 5.2243857 3.8324236 1
15 3.5003320 -2.3672985 1
16 -13.2623113 -7.1593177 0
17 -1.7944354 -2.1725478 0
18 0.5885924 -0.2411048 1
19 -19.3744936 -0.1982088 0
20 -17.9876978 -1.5995063 0
Edit:
I have added a graphic representation of what I am trying to perform. It is important that the 2 fitted lines meet at the threshold, and that I can get 4 coefficents. 2 alphas, and 2 betas.
Since the breakpoint is x = 0 we have a = a2 and so:
nls( y ~ (x < 0) * (a + b * x) + (x > 0) * (a + b2 * x), dat,
start = list(a = 1, b = 1, b2 = 1))
or using lm
lm(y ~ I(x * (x < 0)) + I(x * (x > 0)), dat)
In general if B is the breakpoint:
B <- 0
nls( y ~ (x < B) * (a + b * (x - B)) + (x > B) * (a + b2 * (x - B)), dat,
start = list(a = 1, b = 1, b2 = 1))
B <- 0
lm(y ~ I((x - B) * (x < B)) + I((x - B) * (x > B)), dat)
This is not an answer but a comment which cannot be edited in the comments section because it requires an image to be understandable.
In fact, I cannot understand your data : When represented on Cartesian graph (below) the points appear very scattered. It doesn't look like a piecewise function. What am I missing ?
By the way, if the points were not too far from a piecewise function made of two inclined segments, there is a very simple method for the fitting. See pages 12-13 in this paper : https://fr.scribd.com/document/380941024/Regression-par-morceaux-Piecewise-Regression-pdf
I'm about to learn all about the simplex method in R project, unfortunately I crashed in this case:
We're running a 24h shop and we need to know how many employees do we need if there are six shifts (8-12,12-16 etc.) during the day, and one employee can work a maximum of 8 hours. Limits of the employees at one shift are:
0:00-4:00 < 5 4:00-8:00 < 7 8:00-12:00< 15 12:00-16:00 <10
16:00-20:00 <15 20:00-24:00 <9
I tried this:
library(boot)
a=c(1,1,1,1,1,1)
w1=c(1,1)
w2=c(1,1)
w3=c(1,1)
w4=c(1,1)
w5=c(1,1)
w6=c(1,1)
A1=rbind(w1,w2,w3,w4,w5,w6)
b1=c(5,7,15,10,15,9)
simplex(a=a,A1=A1,b1=b1,maxi=TRUE)
Error in`[<-`(`*tmp*`, , basic, value = iden(M)) :
index is out of borders
But it doesn't work.
The error occurs because the dimensions of the input matrices and vectors are not correct.
Since the coefficients vector a in your example has dimension 6, also the vector x in the objective function must have dimension 6. And since the b1 that is supplied to simplex() also has dimension 6, it follows that A1 in the constraint
A1 * x <= b1
must be a 6 x 6 matrix. However, A in your example is only a 6 x 2 matrix. This triggers the error message. (It would have been nicer if simplex() checked its inputs first and issued a more user friendly message...)
Here is an example with the right dimensions, which does work:
library(boot)
a = rep(1, 6) # vector with 6 ones
A1 = matrix(1, nrow=6, ncol=6) # 6x6 matrix with all ones
b1 = c(5, 7, 15, 10, 15, 9)
simplex(a=a, A1=A1, b1=b1, maxi=TRUE)
Note that this corrected example does not try to actually solve your specific simplex problem, it only illustrates correct usage of simplex().
In general it is worth carefully checking the input dimensions of the inputs to simplex(). They are explained nicely in the help pages:
?simplex
OK, I got it after 4 days :)) I post results here if anybody else'd have same problem. The main difficulty here is to calculate "people" as "number of hours at one shift"
a=c(1,1,1,1,1,1)
> w1=c(4,0,0,0,0,4)
> w2=c(4,4,0,0,0,0)
> w3=c(0,4,4,0,0,0)
> w4=c(0,0,4,4,0,0)
> w5=c(0,0,0,4,4,0)
> w6=c(0,0,0,0,4,4)
> b1=c(20,28,60,40,60,36)
> library(boot)
> simplex(a=a,A1=rbind(w1,w2,w3,w4,w5,w6)
+ ,b1=b1,maxi=T)
Linear Programming Results
Call : simplex(a = a, A1 = rbind(w1, w2, w3, w4, w5, w6), b1 = b1, maxi = T)
Maximization Problem with Objective Function Coefficients
x1 x2 x3 x4 x5 x6
1 1 1 1 1 1
Optimal solution has the following values
x1 x2 x3 x4 x5 x6
5 2 10 0 9 0
The optimal value of the objective function is 26.
CLOSED, Deletle subject or leave for others. Thank you admins for edit and #WhiteViking !
currently i work on calibration of probability. i use the calibration approach, called rescaling algorithm - the source http://lem.cnrs.fr/Portals/2/actus/DP_201106.pdf (page 7).
the algorithm i wrote is:
rescaling_fun = function(x, y, z) {
P_korg = z # yhat_test_prob$BAD
P_k_C1 = sum(as.numeric(y) - 1)/length(y) # testset$BAD
P_kt_C1 = sum(as.numeric(x) - 1)/length(x) # trainset$BAD
P_k_C0 = sum(abs(as.numeric(y) - 2))/length(y)
P_kt_C0 = sum(abs(as.numeric(x) - 2))/length(x)
P_new <- ((P_k_C1/P_kt_C1) * P_korg)/((P_k_C0/P_k_C0) * (1 - P_korg) + (P_k_C0/P_k_C1) * (P_korg))
return(P_new)
}
the input values are:
1. x - train_set$BAD (actuals of `train set`)
2. y - test_set$BAD (actuals of `test set`)
3. z - yhat_test_prob$BAD (prediction on `test set`)
the problem - the result values are not within range of 0 and 1. Could you please help to solve the problem?
Your formulas to obtain probs (P_k_C1 ...) need to be modified. For example, according to the paper, y is a binary variable (0, 1) and the formula is sum(y - 1)/length(y) which is most likely to be negative - it converts y values to be -1 or 0, followed by adding them. I consider it should be (sum(y)-1)/length(y). Below is an example.
set.seed(1237)
y <- sample(0:1, 10, replace = T)
y
[1] 0 1 0 0 0 1 1 0 1 1
# it must be negative as it is sum(y - 1) - y is 0 or 1
sum(as.numeric(y) - 1)/length(y)
[1] -0.5
# modification
(sum(as.numeric(y)) - 1)/length(y)
[1] 0.4