I would like to extract coefficients of indexed variables from a large sum. The following is an example motivated by linear regression to illustrate the problem.
Consider the (unnormalised) log likelihood of a linear regression problem with design matrix X
, observations y
, and regression parameters w
given by
logLikelihood = -gamma / 2 * Sum[(y[i] - Sum[X[i, j] w[j], {j, 1, p}]) ^ 2, {i, 1, n}]
I would like to use Mathematica to extract coefficients of indexed variables from the log-likelihood. For example, I would like to apply
SomeFunction[logLikelihood, w[k]^2]
and get the following result
- gamma / 2 * Sum[X[i,k] ^ 2, {i, 1, n}]
I have tried Coefficient
and differentiating with respect to the variable of interest but both are not quite working as I would like.
n
andp
left as a variables, or do you plug in numbers for them? $\endgroup$n
andp
as variables. $\endgroup$