Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #12342 > unrolled thread

improving speed of simulation using random numbers

Started byfelipe.benguria@gmail.com
First post2012-11-15 09:03 +0000
Last post2012-11-16 06:57 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  improving speed of simulation using random numbers felipe.benguria@gmail.com - 2012-11-15 09:03 +0000
    Re: improving speed of simulation using random numbers Roland Franzius <roland.franzius@uos.de> - 2012-11-16 06:57 +0000

#12342 — improving speed of simulation using random numbers

Fromfelipe.benguria@gmail.com
Date2012-11-15 09:03 +0000
Subjectimproving speed of simulation using random numbers
Message-ID<k82b4l$7kt$1@smc.vnet.net>
Dear all, 

I am trying to compute an expected value using simulation.
I have a random number x with density function d[x]. I want to compute the expected value of function f[x], which is equal to the integral of f[x] times 
d[x] over x.  
In my case, it is difficult to compute the integral so I simulate N values for x and compute the average of f[x] over all N simulated values.

My problem is that my code takes to long for my purposes: this is a part of a larger program and is making it unfeasible in terms of time. 

The following code provides an example of the situation, and my question is how could I reduce the time this takes. THanks a lot for your help

g[x_]:= x^2

 
mydensity[myparameter_]:= ProbabilityDistribution[myparameter*(t)^(-myparameter - 1), {t, 1, Infinity}]

randomnum[myparameter_] := RandomVariate[draw[myparameter], 50]

 
Timing[Sum[g[randomnum[5][[i]]], {i, 1, 50}]]

Out[1353]= {0.64, 81.7808}

This takes 0.6 seconds in my computer and that is way too long for my full program ( I do this many times).

Thanks again,
Felipe

[toc] | [next] | [standalone]


#12355

FromRoland Franzius <roland.franzius@uos.de>
Date2012-11-16 06:57 +0000
Message-ID<k84o4r$g6k$1@smc.vnet.net>
In reply to#12342
Am 15.11.2012 10:03, schrieb felipe.benguria@gmail.com:
> Dear all,
>
> I am trying to compute an expected value using simulation.
> I have a random number x with density function d[x]. I want to compute the expected value of function f[x], which is equal to the integral of f[x] times
> d[x] over x.
> In my case, it is difficult to compute the integral so I simulate N values for x and compute the average of f[x] over all N simulated values.
>
> My problem is that my code takes to long for my purposes: this is a part of a larger program and is making it unfeasible in terms of time.
>
> The following code provides an example of the situation, and my question is how could I reduce the time this takes. THanks a lot for your help
>
> g[x_]:= x^2
>
>
> mydensity[myparameter_]:= ProbabilityDistribution[myparameter*(t)^(-myparameter - 1), {t, 1, Infinity}]
>
> randomnum[myparameter_] := RandomVariate[draw[myparameter], 50]
>
>
> Timing[Sum[g[randomnum[5][[i]]], {i, 1, 50}]]
>
> Out[1353]= {0.64, 81.7808}
>
> This takes 0.6 seconds in my computer and that is way too long for my full program ( I do this many times).



As always for distributions with lengthy algebraic descriptions:

Get a numerical exact expression in the real variable x for the 
distribution function of the random variable named "X"

In: CDF["X"][x_] (* = Prob[ "X" < x ] *) := expression

Make a table with coordinates exchanged

In: TableCDF["X"]=Table[ {CDF["X"][x], x}, {x,xmin,xmax,dx}]

If equally distributed points don't fit properly, distribute the 
interpolations points on the x-axis properly until by using a suitable 
monotone function of x instead of x itself, until you see a sufficiently 
accurate fitting by Plotting both graphs.

Interpolate the table data as an approximation of the inverse of the 
distribution function  CDF["X"]^-1 : {0,1} -> domainOfValues["X"]

In: InverseCDF["X"] = Interpolation[TableCDF["X"]]

Now you can use the standard definition of a random generator for 
CDF[X], namely the map given by CDF[X]^-1 of equally distributed reals 
in {0,1}

draw[]:=lastdraw=InverseCDF["X"][RandomReal[{0,1}]]

The variable lastdraw holds the last random choice executed in case you 
need the value of the last pick again immediately.

The random generator for samples of n now has the form

draw[n_]:=InverseCDF["X"]/@ RandomReal[{0,1},n]

In my experience these kind of random generators work at no time nearly.

I have not checked if part or all of this trivial procedure is present 
in Mathematica v. 8 already.

Have a look at

http://reference.wolfram.com/mathematica/tutorial/ContinuousDistributions.html

http://reference.wolfram.com/mathematica/ref/RandomVariate.html?q=RandomVariate&lang=en

Most commonly used random number generators for named distributions are 
incorporated as RandomVariates.

-- 

Roland Franzius

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web