3
$\begingroup$
Maximize[{PDF[NormalDistribution[0, sigma], x], sigma > 0}, x, Reals]

Just returns unevaluated (when, obviously, it should return 0 along with the function value there).

Any ideas?

Edit: This seems to be a general trend over many PDFs:

Maximize[{PDF[GammaDistribution[a, b], x], a > 0 && b > 0 && x > 0}, x]

returns unevaluated, as does:

Maximize[{PDF[LogNormalDistribution[mu, sigma], x], mu > 0 && sigma > 0 && x > 0}, x]

and:

Maximize[{PDF[StudentTDistribution[nu], x], nu > 0 && x > 0}, x]

and so on... I should have specified but I'm not interested in finding the modes of a specific case (like the normal), but more interested in why Mathematica can't handle these.

$\endgroup$
2
  • $\begingroup$ As the output of Maximize[{(-(x^2/(2* sigma^2))), sigma > 0}, x] shows, the assumption is not taken into account. $\endgroup$
    – user64494
    Jan 26 at 17:15
  • $\begingroup$ I'd like to add that Maximize has problems with transcendental functions. $\endgroup$
    – user64494
    Jan 26 at 17:23

2 Answers 2

4
$\begingroup$
A workaround
    
    $Version
    
    (* "13.2.0 for Mac OS X x86 (64-bit) (November 18, 2022)" *)
    
    Clear["Global`*"]
    
    f[x_] = PDF[NormalDistribution[0, sigma], x];
    
    {max, arg} = Assuming[sigma > 0,
      {f[x] /. #, #} &[
       Solve[
         {D[#, x] == 0, D[#, {x, 2}] < 0} &[f[x]], x][[1]]]]
    
    (* {1/(Sqrt[2 π] sigma), {x -> 0}} *)

EDIT: More generally,

f2[x_] = PDF[NormalDistribution[m, sigma], x];

{max, arg} =
 Assuming[sigma > 0 && m ∈ Reals,
  {f2[x] /. #, Reverse /@ #} &[{Simplify@Reduce[
        {D[#, x] == 0, D[#, {x, 2}] < 0} &[f2[x]], x] // 
  ToRules}[[1]]]]

(* {1/(Sqrt[2 π] sigma), {x -> m}} *)
$\endgroup$
1
  • $\begingroup$ Thanks @Bob. This is sort of beautiful in how hideously complex it is to accomplish a simple task! $\endgroup$
    – ben18785
    Jan 26 at 17:23
3
$\begingroup$

For some distributions taking the log of the density results in an answer:

FullSimplify[
 Maximize[{LogLikelihood[NormalDistribution[μ, sigma], {x}]}, x, Reals], 
 Assumptions -> sigma > 0]
(* {-(1/2) Log[2 π] - Log[sigma], {x -> μ}} *)

For others, more manipulations prior to using Maximize are necessary. Also, all 3 of your examples work if specific values are given for the parameters:

Maximize[{PDF[GammaDistribution[3, 6], x], x > 0}, x]
(* {1/(3 E^2), {x -> 12}} *)

Maximize[{PDF[LogNormalDistribution[1, 2], x], x > 0}, x]
(* {E/(2 Sqrt[2 π]), {x -> 1/E^3}} *)

Maximize[PDF[StudentTDistribution[13], x], x]
(* {1024/(231 Sqrt[13] π), {x -> 0}} *)
$\endgroup$
2
  • $\begingroup$ Thanks @JimB. Any idea just why Mathematica struggles with the PDFs as they stand? $\endgroup$
    – ben18785
    Jan 27 at 15:40
  • $\begingroup$ I wouldn't use the word "struggles". Mathematica certainly decides in those situations that it can't find an answer. (Certainly that's better than producing a wrong answer.) And things are more complicated than restricting solutions to reals or positive reals. For example, the mode (which is what you're after) is not always finite. For a gamma distribution, the mode occurs at 0 for $0<a<1$ and $(a-1)b$ for $a\geq 1$. Talking to Mathematica support might help. $\endgroup$
    – JimB
    Jan 27 at 15:54

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.