1

I'm not good in terms of probability theory. Here's what I'm trying to do:

I want to place PERCEPTORS_NUMBER points inside a circle

I have unnormalized_distribution(r) function. If we consider two circle radiuses r1 and r2 then:

enter image description here

equals to

enter image description here

Let's call the point's distance from the circle center its radius

All points will have radiuses in range [0;FOCUS_RADIUS]. Each point's radius is equals to i/DISTRIBUTION_DISCRETIZATION_FREQUENCY where i is integer

I can count unnormalized_distribution for each possible radius, then divide it by sums of unnormalized_distribution's for all radiuses.

ld distribution_integral=0; ld probabilities[FOCUS_RADIUS*DISTRIBUTION_DISCRETIZATION_FREQUENCY+1]; for(int i=0;i<=FOCUS_RADIUS*DISTRIBUTION_DISCRETIZATION_FREQUENCY;++i) { distribution_integral+=probabilities[i]=unnormalized_distribution(((ld)i)/DISTRIBUTION_DISCRETIZATION_FREQUENCY); } for(int i=0;i<=FOCUS_RADIUS*DISTRIBUTION_DISCRETIZATION_FREQUENCY;++i) { probabilities[i]/=distribution_integral; } 

Then for each radius I'll have the probability in range [0;1] that a certain point will have it. The sum of all probabilities will be 1

For a given point, how can I choose a radius according to this probability?


Update: Solution code:

ld distribution_integral=0; ld probabilities_sums[FOCUS_RADIUS*DISTRIBUTION_DISCRETIZATION_FREQUENCY+1]; for(int i=0;i<=FOCUS_RADIUS*DISTRIBUTION_DISCRETIZATION_FREQUENCY;++i) { distribution_integral+=probabilities_sums[i]=unnormalized_distribution(((ld)i)/DISTRIBUTION_DISCRETIZATION_FREQUENCY); } for(int i=0;i<=FOCUS_RADIUS*DISTRIBUTION_DISCRETIZATION_FREQUENCY;++i) { probabilities_sums[i]/=distribution_integral; if(i!=0) probabilities_sums[i]+=probabilities_sums[i-1]; } srand(time(0)); ld chosen_x_list[PERCEPTORS_NUMBER]; ld chosen_y_list[PERCEPTORS_NUMBER]; for(int i=0;i<PERCEPTORS_NUMBER;++i) { ld random_value=((double)rand())/RAND_MAX; ld best_diff=2; int best_ind=0; int r=FOCUS_RADIUS*DISTRIBUTION_DISCRETIZATION_FREQUENCY,l=0,m; while(r>=l) { m=l+(r-l)/2; if(probabilities_sums[m]>random_value) { if(m==0) best_ind=0; else if(probabilities_sums[m-1]<=random_value) best_ind=m; r=m-1; } else l=m+1; } ld chosen_radius=((ld)best_ind)/DISTRIBUTION_DISCRETIZATION_FREQUENCY; ld chosen_angle=(ld)(rand()%360); fprintf(stderr,"%f\n",(float)chosen_angle); //chosen_angle=0; ld chosen_x,chosen_y; chosen_x_list[i]=chosen_radius*cos(dtr(chosen_angle)); chosen_y_list[i]=chosen_radius*sin(dtr(chosen_angle)); } printf("%d\n",PERCEPTORS_NUMBER); for(int i=0;i<PERCEPTORS_NUMBER;++i) { printf("%f %f\n",(float)chosen_x_list[i],(float)chosen_y_list[i]); } 

ld is defined with

typedef long double ld; 

1 Answer 1

2

Have an extra step of creating summation array:

aux[0] = 0 aux[i] = aux[i-1] + probabilities[i-1] 

Note that this array is monotonically increasing, and aux[i]-aux[i-1] = probabilities[i-1], and probabilities[n] = 1.

Draw a random real (or double) number in range (0,1) (let it be r), and choose the focus with index i such that aux[i-1] <= r < aux[i].

The later can be done efficiently with binary search, since your array is sorted.

So, by drawing a number with uniform distribution (0,1), it has exactly aux[i]-aux[i-1] = probabilities[i] chance to be in the range of element i.

Sign up to request clarification or add additional context in comments.

1 Comment

Also I should choose first such i because aux[i] may be 0. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.