http://www.mathworks.com/matlabcentral/newsreader/view_thread/325764
MATLAB Central Newsreader  PDF to CDF in MATLAB
Feed for thread: PDF to CDF in MATLAB
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Sun, 13 Jan 2013 19:40:09 +0000
PDF to CDF in MATLAB
http://www.mathworks.com/matlabcentral/newsreader/view_thread/325764#895395
Hemming
Hi!<br>
Im trying to extract a scattering angle for a photon using the KleinNishina scattering angle distribution (KN in the code) and for this I need the CDF (of KN) to be able to use the Monte Carlo method when that is achieved. All i've managed so far is to plot the PDF between 0 degrees and Pi to see that it looks alright, and that it has that "peanut shape". Ive tried to use the built in CDF function but it seems very slow.<br>
<br>
E_gamma=0.140;<br>
alpha=0.511;<br>
P=zeros(3142,1);<br>
KN_matrix=zeros(3142, 1);<br>
<br>
for k=1:3142 <br>
j=(k1)/1000;<br>
PE=1/(1+((E_gamma/alpha)*(1cos(j))));<br>
KN=(PE^2*(PE+(1/PE)1+(cos(j))^2))/2;<br>
KN_matrix(k,1)=(KN);<br>
plot(j,KN_matrix(k,1)) <br>
hold on<br>
end<br>
<br>
Thanks!<br>
axis equal

Sun, 13 Jan 2013 20:55:11 +0000
Re: PDF to CDF in MATLAB
http://www.mathworks.com/matlabcentral/newsreader/view_thread/325764#895396
TideMan
On Monday, January 14, 2013 8:40:09 AM UTC+13, Hemming wrote:<br>
> Hi!<br>
> <br>
> Im trying to extract a scattering angle for a photon using the KleinNishina scattering angle distribution (KN in the code) and for this I need the CDF (of KN) to be able to use the Monte Carlo method when that is achieved. All i've managed so far is to plot the PDF between 0 degrees and Pi to see that it looks alright, and that it has that "peanut shape". Ive tried to use the built in CDF function but it seems very slow.<br>
> <br>
> <br>
> <br>
> E_gamma=0.140;<br>
> <br>
> alpha=0.511;<br>
> <br>
> P=zeros(3142,1);<br>
> <br>
> KN_matrix=zeros(3142, 1);<br>
> <br>
> <br>
> <br>
> for k=1:3142 <br>
> <br>
> j=(k1)/1000;<br>
> <br>
> PE=1/(1+((E_gamma/alpha)*(1cos(j))));<br>
> <br>
> KN=(PE^2*(PE+(1/PE)1+(cos(j))^2))/2;<br>
> <br>
> KN_matrix(k,1)=(KN);<br>
> <br>
> plot(j,KN_matrix(k,1)) <br>
> <br>
> hold on<br>
> <br>
> end<br>
> <br>
> <br>
> <br>
> Thanks!<br>
> <br>
> axis equal<br>
<br>
Instead of plotting in a loop, define vectors:<br>
j=[0:3141]'/1000;<br>
PE=1/(1+((E_gamma/alpha)*(1cos(j))));<br>
KN_matrix=(PE.^2.*(PE+(1./PE)1+(cos(j)).^2))/2;<br>
plot(j,KN_matrix)<br>
<br>
Notice the dots before the operators to get elementbyelement multiplication, exponentiation, etc.

Sun, 13 Jan 2013 22:02:11 +0000
Re: PDF to CDF in MATLAB
http://www.mathworks.com/matlabcentral/newsreader/view_thread/325764#895398
Roger Stafford
"Hemming" wrote in message <kcv2ip$mv1$1@newscl01ah.mathworks.com>...<br>
> Im trying to extract a scattering angle for a photon using the KleinNishina scattering angle distribution (KN in the code) and for this I need the CDF (of KN) to be able to use the Monte Carlo method when that is achieved. All i've managed so far is to plot the PDF between 0 degrees and Pi to see that it looks alright, and that it has that "peanut shape". Ive tried to use the built in CDF function but it seems very slow.<br>
         <br>
Do I understand correctly that this is a probability density function with respect to a solid angle of scattering? Wouldn't that need to be known in obtaining a cumulative distribution? Perhaps you need to explain your problem in much greater detail since you are communicating with people who are not necessarily conversant with quantum electrodynamics. Pretend we know nothing about the physics of the subject and explain your question in purely mathematical terms and in great detail.<br>
<br>
Roger Stafford

Mon, 14 Jan 2013 01:24:12 +0000
Re: PDF to CDF in MATLAB
http://www.mathworks.com/matlabcentral/newsreader/view_thread/325764#895402
Roger Stafford
"Roger Stafford" wrote in message <kcvat3$k7t$1@newscl01ah.mathworks.com>...<br>
> "Hemming" wrote in message <kcv2ip$mv1$1@newscl01ah.mathworks.com>...<br>
> > Im trying to extract a scattering angle for a photon using the KleinNishina scattering angle distribution (KN in the code) and for this I need the CDF (of KN) to be able to use the Monte Carlo method when that is achieved. All i've managed so far is to plot the PDF between 0 degrees and Pi to see that it looks alright, and that it has that "peanut shape". Ive tried to use the built in CDF function but it seems very slow.<br>
>          <br>
> Do I understand correctly that this is a probability density function with respect to a solid angle of scattering? Wouldn't that need to be known in obtaining a cumulative distribution? .......<br>
         <br>
One more thought about your question. If it is solid angle that the probability density is taken with respect to, then its cumulative distribution in terms of scatter angle is easy to find. However if it is this scattering you wish to simulate in a Monte Carlo process using matlab's 'rand' generator, you would need the inverse of this CDF function and that may not be so easy to calculate. There are some functions in matlab that may able to help in this. There is also the alternative of a random procedure using rejection to achieve the proper simulation distribution. Anyway please give us the details of what it is you wish to do.<br>
<br>
Roger Stafford

Mon, 14 Jan 2013 07:54:08 +0000
Re: PDF to CDF in MATLAB
http://www.mathworks.com/matlabcentral/newsreader/view_thread/325764#895419
Hemming
Thanks for all the replies, i realize my formulation was very vague. Here's another go:<br>
<br>
My problem is that different scattering angles of Compton scattering have different propabilities. High energy photons is, for instance, more likely to scatter 2 degrees than 90. This is were KleinNishina (KN) comes into play. I would like to create a distribution (a CDF), using the KNfunction between 0180 degrees (which is my PDF).<br>
So what i should do is integrate KN with respect to the scattering angle (j) (create the inverse of this function as you put it Roger) and this has proved difficult by hand. <br>
If this could be achieved i would use this CDF to randomly select points in the distribution representing different angles.<br>
<br>
I hope this clears it up a bit, otherwise i'll give it another go. Thanks again!<br>
<br>
Hemming <br>
<br>
<br>
"Roger Stafford" wrote in message <kcvmns$rgv$1@newscl01ah.mathworks.com>...<br>
> "Roger Stafford" wrote in message <kcvat3$k7t$1@newscl01ah.mathworks.com>...<br>
> > "Hemming" wrote in message <kcv2ip$mv1$1@newscl01ah.mathworks.com>...<br>
> > > Im trying to extract a scattering angle for a photon using the KleinNishina scattering angle distribution (KN in the code) and for this I need the CDF (of KN) to be able to use the Monte Carlo method when that is achieved. All i've managed so far is to plot the PDF between 0 degrees and Pi to see that it looks alright, and that it has that "peanut shape". Ive tried to use the built in CDF function but it seems very slow.<br>
> >          <br>
> > Do I understand correctly that this is a probability density function with respect to a solid angle of scattering? Wouldn't that need to be known in obtaining a cumulative distribution? .......<br>
>          <br>
> One more thought about your question. If it is solid angle that the probability density is taken with respect to, then its cumulative distribution in terms of scatter angle is easy to find. However if it is this scattering you wish to simulate in a Monte Carlo process using matlab's 'rand' generator, you would need the inverse of this CDF function and that may not be so easy to calculate. There are some functions in matlab that may able to help in this. There is also the alternative of a random procedure using rejection to achieve the proper simulation distribution. Anyway please give us the details of what it is you wish to do.<br>
> <br>
> Roger Stafford

Mon, 14 Jan 2013 09:52:06 +0000
Re: PDF to CDF in MATLAB
http://www.mathworks.com/matlabcentral/newsreader/view_thread/325764#895423
Hemming
Hello again!<br>
<br>
I just found a 400 page book on Kahn's method in our university library that should propably help me here. I think i should give it a read before troubling you all with my non existent MATLAB skills again. Thanks for time and effort!<br>
<br>
<br>
"Hemming" wrote in message <kd0dj0$bsu$1@newscl01ah.mathworks.com>...<br>
> Thanks for all the replies, i realize my formulation was very vague. Here's another go:<br>
> <br>
> My problem is that different scattering angles of Compton scattering have different propabilities. High energy photons is, for instance, more likely to scatter 2 degrees than 90. This is were KleinNishina (KN) comes into play. I would like to create a distribution (a CDF), using the KNfunction between 0180 degrees (which is my PDF).<br>
> So what i should do is integrate KN with respect to the scattering angle (j) (create the inverse of this function as you put it Roger) and this has proved difficult by hand. <br>
> If this could be achieved i would use this CDF to randomly select points in the distribution representing different angles.<br>
> <br>
> I hope this clears it up a bit, otherwise i'll give it another go. Thanks again!<br>
> <br>
> Hemming <br>
> <br>
> <br>
> "Roger Stafford" wrote in message <kcvmns$rgv$1@newscl01ah.mathworks.com>...<br>
> > "Roger Stafford" wrote in message <kcvat3$k7t$1@newscl01ah.mathworks.com>...<br>
> > > "Hemming" wrote in message <kcv2ip$mv1$1@newscl01ah.mathworks.com>...<br>
> > > > Im trying to extract a scattering angle for a photon using the KleinNishina scattering angle distribution (KN in the code) and for this I need the CDF (of KN) to be able to use the Monte Carlo method when that is achieved. All i've managed so far is to plot the PDF between 0 degrees and Pi to see that it looks alright, and that it has that "peanut shape". Ive tried to use the built in CDF function but it seems very slow.<br>
> > >          <br>
> > > Do I understand correctly that this is a probability density function with respect to a solid angle of scattering? Wouldn't that need to be known in obtaining a cumulative distribution? .......<br>
> >          <br>
> > One more thought about your question. If it is solid angle that the probability density is taken with respect to, then its cumulative distribution in terms of scatter angle is easy to find. However if it is this scattering you wish to simulate in a Monte Carlo process using matlab's 'rand' generator, you would need the inverse of this CDF function and that may not be so easy to calculate. There are some functions in matlab that may able to help in this. There is also the alternative of a random procedure using rejection to achieve the proper simulation distribution. Anyway please give us the details of what it is you wish to do.<br>
> > <br>
> > Roger Stafford

Mon, 14 Jan 2013 09:59:06 +0000
Re: PDF to CDF in MATLAB
http://www.mathworks.com/matlabcentral/newsreader/view_thread/325764#895424
Roger Stafford
"Hemming" wrote in message <kd0dj0$bsu$1@newscl01ah.mathworks.com>...<br>
> Thanks for all the replies, i realize my formulation was very vague. Here's another go:<br>
> <br>
> My problem is that different scattering angles of Compton scattering have different propabilities. High energy photons is, for instance, more likely to scatter 2 degrees than 90. This is were KleinNishina (KN) comes into play. I would like to create a distribution (a CDF), using the KNfunction between 0180 degrees (which is my PDF).<br>
> So what i should do is integrate KN with respect to the scattering angle (j) (create the inverse of this function as you put it Roger) and this has proved difficult by hand. <br>
> If this could be achieved i would use this CDF to randomly select points in the distribution representing different angles.<br>
> <br>
> I hope this clears it up a bit, otherwise i'll give it another go. Thanks again!<br>
> <br>
> Hemming <br>
         <br>
My understanding is that the KleinNishina formula gives a differential cross section for photons which amounts to a relative probability density with respect to solid angle. That is, roughly speaking it is the relative probability of a scattering within some tiny solid angle divided by the amount of that solid angle. This means that to get a cumulative probability distribution it is necessary to take the integral of this KleinNishina expression with respect to cos(theta) where theta is the angle of scattering because of the nature of solid angle measure. This is different from integrating with respect to just theta. It also means that it is fairly easy to obtain this integral since if we substitute x = cos(theta), it would then be the integral of a rational function of x with respect to x. This is just a problem in partial fractions which is solved using elementary calculus. <br>
Some of the resulting terms will give rise to other rational functions of x, but one of them yields a logarithm expression. With the proper scaling and substituting cos(theta) again in place of x you would then have a valid cumulative distribution function as a function of theta. As I say, this is fairly easy to do. It does not require any numerical computation. <br>
<br>
However, to do a Monte Carlo simulation of this process using a random number generator which has a uniform distribution like matlab's 'rand' function, it is necessary to find the inverse of the above CDF. By this I mean that the random number generator generates a value and you must then find a value of theta that would give the CDF function that random value. This is the reverse of directly evaluating the CDF given a value of theta. It is the combination of the rational terms and logarithm terms in the CDF expression that makes it unlikely that an explicit formula can be found to do this. Matlab does have functions like 'fzero' which can solve implicit equations but these are somewhat timeconsuming and depend on making good initial estimates.<br>
<br>
There are also what are known as 'rejection' methods in which a certain fraction of random numbers generated must be rejected in such a way as to give the appropriate distribution. I would say that as things stand at the moment it looks as though such a rejection method would be your best bet for doing a Monte Carlo procedure of this scattering, in spite of the need to generate considerably more random numbers.<br>
<br>
Roger Stafford