0

I am performing linear regression using the equation $x^{*}=(A^TA)^{-1}A^Tb$, from:

$Ax=b$$A^TAx^{*}=A^Tb$$(A^TA)^{-1}A^TAx^{*}=(A^TA)^{-1}A^Tb$$x^{*}=(A^TA)^{-1}A^Tb$

I am trying to model an exponential relationship (which estimates a target, y, with A and β) of the form $y=Ae^{βt}$, which is equal to $ln(y) = α+βt$, where $A = e^{α}$

I have a target set $b$ which I took the natural log of all elements to transform it to $ln(b)$, and a matrix $A$ which has coefficients of 1 and k, like so: $A=\begin{bmatrix} 1 & k_1\\ 1 & k_2\\ 1 & k_3 \\ \end{bmatrix}$ and so on. Thus, when I plug this matrix $A$ into the equation $x=(A^TA)^{-1}A^Tb$, I expect the matrix $x$ to have coefficients $α$ and $β$ such that $ln(b) = α+βt$, which I know is a smooth function.

However, when I plot the product $Ax^{*}$ (which I think is the predicted values of $ln(b)$ I do not get a smooth function, instead I get:

Predicted b updated

Unfortunately, I have no clue what equation this represents; it does not match the graph of $ln(\hat{b}) = -4.40680302+0.00149165363t$?

I have also attached my predicted $x$ and $b$ matrices for reference.

predicted $x=\begin{bmatrix} -4.40680302\\ .00149165363\\ \end{bmatrix}$

$b = \begin{bmatrix} 0.01081389\\ 0.01918884\\ 0.01299735\\ 0.01767956\\ 0.01230499\\ 0.02024096 \end{bmatrix}$

Below is the Python code I used to perform the described computations:

weekly_pos = np.array([39,71,125,279,149,198]).reshape(-1,1)
A = np.concatenate((np.ones((len(weekly_pos),1)),weekly_pos), axis=1)
b = np.log(np.array([19/1757,44/2293,49/3770,64/3620,56/4551,84/4150])).reshape(-1,1)
u = np.linalg.inv(np.matmul(A.T,A))
v = np.matmul(A.T,b)
pred_x = np.matmul(u,v)
y_hat = np.matmul(A,pred_x)
H. Khan
  • 35
  • 5
  • Can you post a minimal working example of the code you used to produce this? – Demetri Pananos Oct 18 '20 at 22:50
  • @DemetriPananos Sure, I've just added it – H. Khan Oct 18 '20 at 22:57
  • What is the relationship between, $x$, $b$ and $y$? It seems you use $x$ in two different ways, and perhaps that $y=b$ or $\log(y)=b$ or something. This would be more clear if you used each symbol in one way only. Also, your plot shows values plotted on the horizontal axis at 0, 1, 2, ... but these are not values in $b$. So is this just a problem with how you've plotted the data? – Sycorax Oct 18 '20 at 23:18
  • The equation $\ln(\hat{b}) = .0124891386+.0000212436105x$ does not make sense if $x$ is the estimated coefficients of the linear model. – Sycorax Oct 18 '20 at 23:27
  • @Sycorax That's sort of what I'm trying to figure out - what equation correctly relates those two values in my x* matrix? – H. Khan Oct 18 '20 at 23:29
  • It's hard to say. Those values $(0.0124, 0.0000212)$ don't correspond to the OLS solution with `np.log(b)` as a target. My suggestion is to write down what you want to do using a paper and pencil using consistent notation. Then write that program; the problems seem to arise from inconsistent notation, and perhaps a bug in how you made the plot. – Sycorax Oct 18 '20 at 23:30
  • @Sycorax I see - I've fixed the x matrix, and the plot looks slightly different, but still has the spike that I am unsure of the origin of. – H. Khan Oct 18 '20 at 23:47
  • The plot looks the same to me. We know it must be wrong because the intercept is -4 and all of the values $t$ are small. Also, do you mean to write $$\ln(\hat{b}) = -4.40680302+0.00149165363t?$$ or $$\ln(\hat{b}) = -4.40680302+0.00149165363x?$$ What's the relationship between $Ax=b$ and $b=A\exp(\beta t)$? It seems to use $b$ and $A$ in two different ways. – Sycorax Oct 18 '20 at 23:48

1 Answers1

2

After the edits, I think the only remaining source of confusion is that is the prediction on the log scale, so instead we can write $$ \begin{align} \ln(\hat{b}) &= -4.40680302+0.00149165363 t \\ \hat{b}&=\exp(-4.40680302+0.00149165363t) \\ &= \exp(-4.40680302)\exp(0.00149165363t) \\ &= \hat{A}\exp(\hat{\beta} t) \end{align} $$

As for your plot, the problem is that

  • on the horizontal axis, you're not plotting $t$, but the indices of $t$, i.e. $[0,1,2,\dots,5]$. Instead, use your values for $t$ (which are called weekly_pos in your code). This is important because weekly_pos is not sorted, so you get the zig-zag pattern as a result of using the indices.
  • on the vertical axis, you're plotting $\ln(\hat{b})$, not $b$. So you should exponentiate your predictor if you want predictions on the linear scale.

For some information about the bias of this procedure, see Predicting y from log y as the dependent variable

Sycorax
  • 76,417
  • 20
  • 189
  • 313
  • It might be worth pointing out that this approach yields predictions that are biased low. – whuber Oct 19 '20 at 13:17
  • @whuber This is a good suggestion. I've tried to pull a good Question about this into the post but if you have a different suggestion, I'll happily make the edit. – Sycorax Oct 19 '20 at 16:06
  • What I would be most concerned about is the contradiction between the two models proposed by the OP: it is implied that each has additive error, but that's not possible. – whuber Oct 19 '20 at 16:26