Calculating a spatial distribution from a probability density

In summary: The inverse of a cumulative probability distribution is a function that takes in a given probability and outputs a given cumulative probability:f(x)=1-(1/x)*P(x)
  • #1
trmcclain
14
0
I'm hoping this will be the last time I call for help, but in any case, here it goes.

I thought I had a handle on this before, but in all of my attempts, my code diverges within a few iterations.
My problem is creating a spatial distribution of particles given a probability density. I've tried to start out with an easier function, the normal distribution [itex]f(x)=e^{-x^2}[/itex]

The method I've tried to implement, which has been failing, is the Newton-Raphson method:
[itex]x_{new}=x_{guess}-\frac{U-G(x)}{f(x)}[/itex] where U is a random number between 0 and 1 and [itex]G(x)=\int_{-∞}^x f(x)\,dx[/itex]

My code is in C++ and is as follows:
dr = rand() % 100;
U=dr/100.;
do{
Gfunc=(1./R)*(sqrt(PI)/2.)*(erf(rg)+1);
func=exp(-(rg*rg));
whole=(U-Gfunc)/func;

rn=rg-whole;
if(rn > 1E-5)
rg=rn;
}while(rn>1E-5);

(Sorry if there's a way to make code pretty on the forums)
**Note, I do use srand(time(NULL)) above this loop.**
 
Physics news on Phys.org
  • #2
I don't think your method works even in theory.

Ultimately what you want to do is produce a sample from the given distribution.

There are ways to take output from rand() and produce a sample from an arbitrary distribution; the relevant section of Numerical Recipes (3rd Edition) is a good place to start. (You can view a limited number of pages of the online edition per month for free; the relevant section starts at page 361.)

The usual way to get a U[0,1] random variable from rand() is:
Code:
double u01;

u01 = ((double)rand())/((double)[url=http://www.cplusplus.com/reference/cstdlib/RAND_MAX/]RAND_MAX[/url]);
 
  • #3
trmcclain said:
the normal distribution [itex]f(x)=e^{-x^2}[/itex]
That isn't a probability density unless you put the correct factors on it (which apparently you did try to do in your code).

Your statement of what you are trying to do isn't clear. I think you are trying to write a function to compute the inverse of a cumulative probability distribution (not "generate a distribution").

So your problem is:

Solve for [itex] x [/itex] in the equation [itex] v - \int_{-\infty}^x f(s) ds = 0[/itex]

where [itex] v [/itex] is a given probability and [itex] f(s) [/itex] is a given probabiity density.
 

What is a probability density?

A probability density is a mathematical function that describes the likelihood of a random variable taking on a certain value. It is often used in statistics and probability to represent the distribution of a set of data.

How is a probability density related to spatial distribution?

A probability density can be used to calculate the spatial distribution of a set of data by assigning a probability value to each point in space. This allows for a visual representation of the likelihood of a certain event occurring at different locations.

What factors affect the accuracy of calculating a spatial distribution from a probability density?

The accuracy of calculating a spatial distribution from a probability density can be affected by the size and resolution of the data set, the choice of probability distribution function, and the assumptions made about the underlying data.

How can a probability density be converted into a spatial distribution?

To convert a probability density into a spatial distribution, the probability values for each point in space are multiplied by a constant to ensure that the total sum of probabilities equals 1. This creates a normalized distribution that can be visually represented.

What are some applications of calculating spatial distributions from probability densities?

Calculating spatial distributions from probability densities can be useful in a variety of fields, such as ecology, epidemiology, and environmental science. It can help identify patterns and trends in data, and inform decision making processes in areas such as resource management and risk assessment.

Similar threads

  • Calculus
Replies
6
Views
1K
  • Poll
  • Set Theory, Logic, Probability, Statistics
Replies
4
Views
1K
Replies
3
Views
1K
  • Calculus and Beyond Homework Help
Replies
6
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
2
Views
1K
Replies
4
Views
1K
  • Calculus and Beyond Homework Help
Replies
1
Views
983
  • Set Theory, Logic, Probability, Statistics
Replies
3
Views
1K
  • Calculus and Beyond Homework Help
Replies
1
Views
547
  • Calculus and Beyond Homework Help
Replies
2
Views
1K
Back
Top