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


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

Re: improving speed of simulation using random numbers

Started byPeter Klamser <klamser@googlemail.com>
First post2012-11-16 07:14 +0000
Last post2012-11-16 07:14 +0000
Articles 1 — 1 participant

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: improving speed of simulation using random numbers Peter Klamser <klamser@googlemail.com> - 2012-11-16 07:14 +0000

#12357 — Re: improving speed of simulation using random numbers

FromPeter Klamser <klamser@googlemail.com>
Date2012-11-16 07:14 +0000
SubjectRe: improving speed of simulation using random numbers
Message-ID<k84p49$gas$1@smc.vnet.net>
... you do not use Mathematica style for programming.

Better is

Timing[Plus @@ Table[RandomReal[], {i, 1, 50}]]

{0., 25.5969}

Peter


2012/11/15 Peter Klamser <klamser@googlemail.com>

> ... you do not use Mathematica style for programming.
>
> Better is
>
> Timing[Plus @@ Table[RandomReal[], {i, 1, 50}]]
>
> {0., 25.5969}
>
> Peter
>
>
> 2012/11/15 <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).
>>
>> Thanks again,
>> Felipe
>>
>>
>

[toc] | [standalone]


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


csiph-web