Click here to Skip to main content
15,895,746 members
Home / Discussions / Algorithms
   

Algorithms

 
GeneralRe: A Formula Pin
cp987616-Apr-08 14:22
cp987616-Apr-08 14:22 
GeneralAssigning random lives: an algorithm Pin
DQNOK15-Apr-08 13:15
professionalDQNOK15-Apr-08 13:15 
GeneralRe: Assigning random lives: an algorithm Pin
Alan Balkany16-Apr-08 4:33
Alan Balkany16-Apr-08 4:33 
GeneralRe: Assigning random lives: an algorithm Pin
DQNOK16-Apr-08 5:53
professionalDQNOK16-Apr-08 5:53 
GeneralRe: Assigning random lives: an algorithm Pin
Alan Balkany16-Apr-08 6:03
Alan Balkany16-Apr-08 6:03 
GeneralRe: Assigning random lives: an algorithm Pin
DQNOK16-Apr-08 6:07
professionalDQNOK16-Apr-08 6:07 
GeneralRe: Assigning random lives: an algorithm Pin
DQNOK16-Apr-08 6:24
professionalDQNOK16-Apr-08 6:24 
Generalgenerating random values from a PDF: GOT IT. [modified] Pin
DQNOK16-Apr-08 7:36
professionalDQNOK16-Apr-08 7:36 
DQNOK wrote:
Do you know the algorithm for using the rand() function (which generates a uniform PDF) to generate values within a specified PDF?


I answered my own question.

For a PDF called 'y(x)' (where x is the random varaible), and a computer library function rand() that returns a random value in the range [0, 1) from a uniform PDF, we seek to map rand's return value (call it rx) to the correct x.

Effectively, we are looking for x such that the area under the curve y(x) LEFT OF x is equal to the area under the uniform PDF left of rx. But since rand produces values within the unit interval [0,1), rx IS the area under the curve left of rx. So, we seek x such that:

rx = integral from -infinity to x of y(x)

Exactly HOW we evaluate the integral (i.e. solve for x) is an implementation detail that depends on how y(x) is defined. If the integral is already given via a formula, we just use a standard non-linear scalar solver, like a Newton-Raphson. It not, then perhaps I would just use a stepping approach, something like:
ndx = getBestStartingIndex(rx);
x = X_STARTS[ndx];  // X_STARTS[ndx] <= rx < X_STARTS[ndx+1]
area = AREAS[ndx];  // precomputed areas based on x.
old_y = y(x);
while( area < rx )
{
   x += dx;
   new_y = y(x);
   area += dx * (old_y + new_y)/2 ; //trapezoid rule
   old_y = new_y;
}
return x;

Which can be polished for better precision, time-performance etc.

David
---------
Empirical studies indicate that 20% of the people drink 80% of the beer. With C++ developers, the rule is that 80% of the developers understand at most 20% of the language. It is not the same 20% for different people, so don't count on them to understand each other's code.
http://yosefk.com/c++fqa/picture.html#fqa-6.6

---------

modified on Thursday, April 17, 2008 9:28 AM

GeneralRe: Assigning random lives: an algorithm Pin
DQNOK16-Apr-08 6:02
professionalDQNOK16-Apr-08 6:02 
Generalhamilton algorithm Pin
phap12-Apr-08 4:17
phap12-Apr-08 4:17 
GeneralRe: hamilton algorithm Pin
soap brain12-Apr-08 4:51
soap brain12-Apr-08 4:51 
QuestionRe: hamilton algorithm Pin
CPallini12-Apr-08 5:42
mveCPallini12-Apr-08 5:42 
GeneralRe: hamilton algorithm Pin
soap brain12-Apr-08 5:48
soap brain12-Apr-08 5:48 
GeneralRe: hamilton algorithm Pin
phap12-Apr-08 6:52
phap12-Apr-08 6:52 
GeneralRe: hamilton algorithm Pin
pmarfleet12-Apr-08 8:07
pmarfleet12-Apr-08 8:07 
GeneralRe: hamilton algorithm Pin
73Zeppelin22-Apr-08 1:03
73Zeppelin22-Apr-08 1:03 
GeneralRe: hamilton algorithm Pin
soap brain22-Apr-08 1:06
soap brain22-Apr-08 1:06 
GeneralRe: hamilton algorithm Pin
73Zeppelin22-Apr-08 1:33
73Zeppelin22-Apr-08 1:33 
GeneralRe: hamilton algorithm Pin
soap brain22-Apr-08 1:58
soap brain22-Apr-08 1:58 
GeneralRe: hamilton algorithm Pin
73Zeppelin22-Apr-08 2:10
73Zeppelin22-Apr-08 2:10 
GeneralRe: hamilton algorithm Pin
soap brain22-Apr-08 2:49
soap brain22-Apr-08 2:49 
GeneralRe: hamilton algorithm Pin
73Zeppelin22-Apr-08 3:30
73Zeppelin22-Apr-08 3:30 
GeneralRe: hamilton algorithm [modified] Pin
soap brain22-Apr-08 17:23
soap brain22-Apr-08 17:23 
GeneralRe: hamilton algorithm Pin
73Zeppelin22-Apr-08 22:21
73Zeppelin22-Apr-08 22:21 
GeneralRe: hamilton algorithm Pin
soap brain22-Apr-08 22:30
soap brain22-Apr-08 22:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.