I have a function in 5 variables, lkl, and I want to evaluate its second derivative wrt tau at a point. (I will also later want the mixed partials.) Here is my function and its second partial:

lkl[\[Mu]_, \[Tau]_, s_, crit_, 
  y_] := -Log[Sqrt[\[Tau]^2 + s^2]*Sqrt[2 \[Pi]]] - {0.5*
    Sqrt[\[Tau]^2 + s^2]^(-2)*(y - \[Mu])^2} - 
    NormalDistribution[0, 1], (crit*s - \[Mu])/Sqrt[\[Tau]^2 + s^2]]]

d22 := D[lkl[\[Mu], \[Tau], s, crit, y], {\[Tau], 2}]

Following the top answer here, I first tried to evaluate the derivative as follows:

d22 /. {\[Mu] -> 0.1, \[Tau] -> 1, s -> .5, crit -> 1.96, y -> 0.5}

but this yields

General::ivar: 1 is not a valid variable.

as well as the output: $$\partial_{\{1,2\}}\{-0.851658\}$$

The -0.851658 is actually the value of lkl itself evaluated at this point, which is obviously a problem, but I don't know how to fix this.

Then, following the top answer here, I tried:

d22[\[Mu]_, \[Tau]_, s_, crit_, y_] := 
 Derivative[2][lkl[\[Mu], #, s, crit, y] &][\[Tau]]

which throws the same error, along with new one saying

SetDelayed: Tag D in [...] is Protected.

1 Answer 1


Try with a fresh kernel. It works fine on V12.0.0


  • $\begingroup$ Well, that was easy! Just for my edification as a new user, this seems to imply there were stored variables that somehow messed up the evaluation? $\endgroup$
    – half-pass
    Commented Apr 20, 2022 at 14:45
  • 1
    $\begingroup$ @half-pass yes, some underlying definitions from previous commands that got carried around and caused issues. Don't worry. It happens daily all over the world :) $\endgroup$
    – bmf
    Commented Apr 20, 2022 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.