Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'd like to plot the CDF of the following pdf in Mathematica 7:

$$f(x)=0.1 g(x)+0.85 m(x)+0.05 h(x)$$ where

$$ \begin{array}{ccl} g(x)&:&P(0)=1,\ P(x\neq 0)=0\\ h(x)&:&P(1)=1,\ P(x\neq 1)=0 \end{array} $$

and $m(x)$ is a truncated normal distribution between {0, 1} of NormalDistribution[0.85, 0.1]

My code for the moment is (I used the code from MixtureDistribution in Mathematica 7):

Clear@MixtureDistribution
MixtureDistribution /: CDF[MixtureDistribution[wts_List, dist_List]] :=
    With[{normWts = Normalize[wts, Total], cdfs = CDF[#, \[FormalX]] & /@ dist}, 
        Evaluate[Total[normWts cdfs] /. \[FormalX] -> #] &]
MixtureDistribution /: 
    CDF[m : MixtureDistribution[wts_List, dist_List], x_] := CDF[m]@x

truncf = PDF[NormalDistribution[0.85, .1], x] / 
    (CDF[NormalDistribution[0.85, .1], 1] - CDF[NormalDistribution[0.85, .1], 0]);

ℳ = MixtureDistribution[{0.1, 0.85, 0.05}, 
        {DiscreteUniformDistribution[{0, 0}], truncf, DiscreteUniformDistribution[{1, 1}]}];

Plot[CDF[ℳ]@x, {x, 0, 1}, Filling -> Axis]

I get a blank plot without any distribution plotted... Any ideas how to fix this?

share|improve this question
    
The error is in the truncf line. NormalDistribution[...] is a symbolic distribution and when you normalize it by some number, what do you mean? This is no longer a "distribution" and should give an error. It is surprising you just get a blank plot with no indication that this is incorrect. In contrast, RandomReal[0.5 NormalDistribution[], {10}] correctly gives you an error. –  rm -rf May 28 '13 at 1:22
    
@rm-rf: I'm trying to get the truncated normal distribution, so I divide the original pdf with the CDF at the limits of the interval. How can I solve it? Thanks –  jpcgandre May 28 '13 at 1:28
    
Yes, but for a truncated normal distribution, you divide the PDF of the distribution by the CDF at the end points, whereas here you're dividing the symbolic distribution by that number, not its PDF. I've just pointed out where and why the error arises, so perhaps you can try a different approach with this in mind. FWIW, version 8 has TruncatedDistribution and also ProbabilityDistribution which allows you to define symbolic distributions from arbitrary PDFs. You'll have to think about it a little more for version 7. –  rm -rf May 28 '13 at 1:33
    
@rm-rf: OK so I've edited my code. I still don't see the result... I believe the error is in DiscreteUniformDistribution[{0, 0}] because I can see the truncf plot. –  jpcgandre May 28 '13 at 1:39
    
But now you're passing a PDF to MixtureDistribution, when it expects a symbolic distribution. That's why I said "you'll have to think about it a little more"... As a hint, look at how I defined MixtureDistribution — it takes a list of symbolic distributions, calculates their individual CDFs and computes the weighted CDF. You could instead redefine it to take the individual CDFs directly and then compute the weighted CDF. That way, you can provide the CDF of your truncated distribution (you should for the others as well) instead of having to worry about creating a symbolic distribution. –  rm -rf May 28 '13 at 1:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.