Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: piecewice pdf, problems with cdf

  • To: mathgroup at smc.vnet.net
  • Subject: [mg105383] Re: [mg105365] piecewice pdf, problems with cdf
  • From: Patrick Scheibe <pscheibe at trm.uni-leipzig.de>
  • Date: Mon, 30 Nov 2009 06:12:06 -0500 (EST)
  • References: <200911291012.FAA16385@smc.vnet.net>

Hi,

if you need to introduce piecewise functions, why don't you use
Piecewise[]-functions? I think we had this issue a few weeks ago.
Just look what f[x] gives you and try to understand that when Integrate
doesn't call your function with a numeric value it always gets 0. The
solution is

f[x_] := Piecewise[{{1/9 x^2, 0 < x <= 3}}, 0];
cdfSlow[x_] := Integrate[f[v], {v, -\[Infinity], x}];

cdfSlow[1]

1/27

The cdf is slow in this case since every call to cdf integrates the
expression again. Here it is better to integrate algebraically and use
the result.
Look what this gives:

Assuming[Element[x, Reals], Integrate[f[v], {v, -\[Infinity], x}]]

so you know the integral explicitely.

cdfFast[x_] := 
 Evaluate[Assuming[Element[x, Reals], 
   Integrate[f[v], {v, -\[Infinity], x}]]]

With this I have for the call 

Plot[cdfFast[x], {x, -1, 5}, PlotPoints -> 100] // AbsoluteTiming

0.007639 seconds and with your definition 3.741658 seconds.



Cheers
Patrick

On Sun, 2009-11-29 at 05:12 -0500, michael partensky wrote:
> Hi! Teaching the continuous distributions, I needed to introduce the
> piecewise functions.
> Here is the example that did not work well:
> 
> In[56]:= f1[x_] /; 0 < x <= 3 := 1/9  x ^2;
> f1[x_] := 0;
> 
> Plot[f1[x],{x,-1,4}] works fine. However, the results for cdf are ambiguous
> In[57]:= cdf[x_] := Integrate[f1[v], {v, -\[Infinity], x}]
> 
> In[59]:= cdf[1]
> Out[59]= 0
> 
> I thought that may be the second definition (for some reason) overwrote the
> first, but apparently this was not the case.
> 
> Then I tried using Which,
> 
> f1[x_] := Which[0 < x <= 3, x^2/9, x <= 0 || x > 3, 0];
> 
> Plot[f2[x], {x, -1, 4}] worked fine.
> 
> However, Plotting CDF is very slow.
> 
> What is the reason for the first error and how to accelerate (compile?)  the
> second?
> 
> Thanks
> Michael
> 
> PS: I was aware about the issues with the derivatives of Piecewise
> functions, but expected  integration to be safe. What did i do wrong?
> 
> On Tue, Nov 24, 2009 at 5:50 AM, Bill Rowe <readnews at sbcglobal.net> wrote:
> 
> > On 11/23/09 at 6:53 AM, lshifr at gmail.com (Leonid Shifrin) wrote:
> >
> > >Hi, Michael.
> >
> > >Your solution is indeed very memory -  hungry due to the mapping of
> > >Permutations, as you mentioned. The total number of permutations can
> > >be easily deduced from the list of multiplicities of elements in a
> > >given partition: n!/(n1!n2!...nk!), where n1, ..., nk are
> > >multiplicities of elements, and n is the length of the partition:
> > >n=n1+...+nk. The multiplicities can be obtained by Tally. The
> > >following modification can be realistically used in a much wider
> > >region of the problem's parameter space than your original one, and
> > >may possibly serve your needs.
> >
> > >In[1]:=
> > >Clear[outsNew];
> > >outsNew[sum_, thr_] :=
> > >Total[Factorial[Length[#]]/
> > >Times @@ Factorial[Tally[#][[All, 2]]] & /@
> > >Cases[IntegerPartitions[sum, thr, {1, 2, 3, 4, 5, 6}],
> > >Table[_, {thr}]]];
> >
> > The above can be made more efficient by using the form of
> > IntegerPartitions that only generates partitions of a given
> > length. That is
> >
> > outs[sum_, thr_] :=
> >    Total[Factorial[Length[#]]/
> >       Times @@ Factorial[Tally[#][[All, 2]]] & /@
> >      IntegerPartitions[sum, {thr}, {1, 2, 3, 4, 5, 6}]]
> >
> > does the same thing a bit more efficiently
> >
> >
> >
> 



  • Prev by Date: Re: piecewice pdf, problems with cdf
  • Next by Date: Re: Re: Bug ??????
  • Previous by thread: Re: piecewice pdf, problems with cdf
  • Next by thread: Re: piecewice pdf, problems with cdf