My question is related to the This question. I want to plot different PDFs for a given random variable in the same plot using f[x_NumericQ].

For example, I have tried to evaluate the following code for different values of $\mu$ and $\sigma > 1$ to get differents PDFs in the same plot, but I can't. I only can only plot it for one fixed parameter.


μ = 0.5;
f[z_?NumericQ] :=
    Exp[-z^2]* Erf[(z-μ)/(Sqrt[2]*σ)]* Erfi[(z-μ)/(Sqrt[2]*σ)], {σ, 1, ∞}]
Dist = ProbabilityDistribution[f[z], {z, -∞, ∞}];
pdfH[z_] := PDF[Dist, z];
Plot[pdfH[z], {z, -5, 5}]

How can I plot different PDFs related to my distribution in the same plot using a different color to distinguish each PDF?

Note: $\mu \in [0, 1),\,\sigma >1$

  • 2
    $\begingroup$ The linked question has an accepted answer, so you will need to tell us more about your problem and specifically what you have tried and what issues you are running into in your own problem that are not solved by that answer. Note also that the correct syntax is F[x_?NumericQ] (with the ?). $\endgroup$ – MarcoB Dec 3 '19 at 17:08
  • $\begingroup$ For example I want to plot differents PDF in the same plot by varaiations of parameters like mean value ,..., For example in the linked questions The Given plot is for fixed parameters value , How I can use F[x_?NumericQ] for Mu=0.5, ,0.4,0.3,..... and the same with others plot to get differents plot in one plot $\endgroup$ – zeraoulia rafik Dec 3 '19 at 17:19
  • $\begingroup$ Can you please provide an example of the pdf you want to plot, with different parameter values. The example you refer to is non-standard: it is about a pdf that cannot be expressed in a closed-form. Your problem sounds much simpler. $\endgroup$ – wolfies Dec 3 '19 at 17:21
  • $\begingroup$ Ok, Wait I add it to the question now $\endgroup$ – zeraoulia rafik Dec 3 '19 at 17:31
  • 2
    $\begingroup$ And what makes you think that that function, integrating over $\sigma$, is a (valid) pdf? $\endgroup$ – wolfies Dec 3 '19 at 18:44

To evaluate the function that you derive as the PDF of f for multiple values of μ, it is a good idea to make it depend on μ as well as `z; like so:

f[z_?NumericQ, μ_?NumericQ] := NIntegrate[Exp[-z^2] Erf[(z - μ)/(σ Sqrt[2])]^2, {σ, 1, ∞}]

Now I alter the defintion you give for the PDF function to return a pure function parametrized by μ

pdfF[μ_?NumericQ] := PDF[ProbabilityDistribution[f[x, μ], {x, -∞, ∞}]]

Now a plot with μ ranging over a subdivision of the unit interval can be made like this:

With[{n = 3},
        Labeled[pdfF[#1][z], Row[{"μ = ", Rationalize[#1]}], Above], {z, -5, 5},
        PlotRange -> All,
        PlotStyle -> #2] &
      Rest[{#, Hue[#]} & /@ Subdivide[0., 1., n]],
  ImageSize -> Large]]


After seeing plot, I don't have any confidence that pdfF is a valid PDF.


The OP points out in a comment that I made an error when I thought I could simplify the definition of f. I use the correct definition here.

f[z_?NumericQ, μ_?NumericQ] := 
  NIntegrate[Exp[-z^2] Erf[(z - μ)/(σ Sqrt[2])] Erfi[(z - μ)/(σ Sqrt[2])], {σ, 1, ∞}]

The rest of the code given a above is still valid , but here is an improved plotting expression.

With[{n = 3},
    Plot[Labeled[pdfF[#1][z], Row[{"μ = ", #1}], Above], {z, -5, 5},
      PlotRange -> All,
      PlotStyle -> #2] &
     ({#, Hue[#]} & /@ (Range[n]/n)),
    ImageSize -> Large]]


It doesn't look much different and, more importantly, consider this;

With[{n = 3}, NIntegrate[pdfF[#][z], {z, -5, 5}] & /@ (Range[n]/n)]

{0.707847, 1.11662, 1.83429}

The integral of a PDF over its domain should be 1, so my conclusion is still the your PDF formulation is wrong.

  • $\begingroup$ Thanks for that , PLeas how you know that is not a valid PDF ? $\endgroup$ – zeraoulia rafik Dec 4 '19 at 10:11
  • $\begingroup$ Pleas The function you took is not the same as above in my question the first function is error function and in the second is imaginarry error function erfi(z) $\endgroup$ – zeraoulia rafik Dec 4 '19 at 10:48
  • $\begingroup$ I have adjusted it I have got {0.98..,0.98,0.98...,0.98..} ,for n=4, I s this a valid PDF , i have used your last command $\endgroup$ – zeraoulia rafik Dec 5 '19 at 1:07
  • $\begingroup$ I have got this exactly :{0.981171,0.980972,0.980627,0.980114,0.979407,0.978465,0.97724,0.975674,0.973696} $\endgroup$ – zeraoulia rafik Dec 5 '19 at 1:11

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.