I defined a function with n terms where n is a variable:

LogLH[μ_, σ_] = n Log[a[σ]] - Sum[Log[y[[i]]] +  
(Log[y[[i]]] - μ)^2 / (2 σ^2), {i, 1, n}]

In this function, the y[[i]] are (assumed to represent) observations of a quantity - picked from a list of n elements.

This statement multiply produces the error "The expression i cannot be used as a part specification". It seems however, that M. correctly evaluates the function. M. also computes the partial derivatives for μ and σ. But the above errors repeat and lateron, I can not apply the Solve command to the resulting system of equations. Hence my question: How can I define an open ended list of n elements in M.?

  • 1
    $\begingroup$ Use SetDelayed (:=) instead of Set (=). Afterwards, once you provide a proper list y and a value for n, the sum will evaluate. E.g., n = 10; y = Range@10; LogLH[m, s]. The rest of your question is uncear: what is "M."? What inequalities? What system of equations? What about the derivatives of $\mu$ and $\sigma$? $\endgroup$ – corey979 Nov 14 '16 at 11:11

First, you need to use SetDelayed (:=) in your function definition. Next, if you use a list in the step definition then the Sum will step through its elements (rather than going over an integer range).

LogLH[μ_, σ_] := n Log[a[σ]] - Sum[
    Log[y] +  (Log[y] - μ)^2 / (2 σ^2), 
    {i, y}

So now when you use LogLH the y will stay as a symbol like this:

LogLH[1, 2]
(* -y (1/8 (-1 + Log[y])^2 + Log[y]) + n Log[a[2]] *)

until you set y to a list, then i will step through the values of y:

y = {1, 2, 3};
LogLH[1, 2]
(* {-(3/8) + n Log[a[2]], -(3/8) (-1 + Log[2])^2 - 3 Log[2] + 
      n Log[a[2]], -(3/8) (-1 + Log[3])^2 - 3 Log[3] + n Log[a[2]]} *)

If Solve does not work for your system of equations, you could also try NSolve or FindRoot

| improve this answer | |
  • $\begingroup$ Your approach eliminates the sum - which is however essential for later analytical treadment of the problem. Think e. g. of the basic problem of calculating a mean. You have n observations with values y[[i]], but for the formula you do not have these y[[i]] nor the n. $\endgroup$ – Robert Brusa Nov 14 '16 at 15:56
  • $\begingroup$ Now consider s2 to be a function of yq and you want to find the derivative of it. $\endgroup$ – Robert Brusa Nov 14 '16 at 16:04

First, use SetDelayed (:=) instead of Set (=). Next, I'd propose to incorporate the list y as an argument of the function, as "y[[i]] are (...) observations of a quantity - picked from a list of n elements", hence n = Length[y]:

LogLH[μ_, σ_, y_List] := 
 Length[y] Log[a[σ]] - Sum[Log[y[[i]]] + (Log[y[[i]]] - μ)^2/(2 σ^2), 
    {i, 1, Length[y]}]


LogLH[2, 0.5, Range[5]]

-18.8866 + 5 Log[a[0.5]]

The OP didn't specify neither a[σ], nor "the resulting system of equations". About the derivatives:

D[LogLH[μ, σ, Range[5]], μ]

enter image description here

D[LogLH[μ, σ, Range[5]], σ]

enter image description here

Mathematica won't handle a Sum with an unspecified n.

| improve this answer | |
  • $\begingroup$ You wrote: Mathematica won't handle a Sum with an unspecified n. Yes, that's exactly what bothers me. Why not? A function which is the sum of n terms can be differenciated - even if one does not know n. $\endgroup$ – Robert Brusa Nov 16 '16 at 9:50
  • $\begingroup$ See this discussion. $\endgroup$ – corey979 Nov 16 '16 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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