Is there an idiomatic Julian way to deal with these cases without a large
pre-allocation or push? I understand why your suggestion here would be
faster than the original. This application aside, I see a lot of push!()
usage in some of the libs (eg saving state in Optim.jl, readdlm in base,
etc).

Linked lists (they only live in DataStructures.jl?) have all the obvious
problems that Stefan points out (scattered in memory), but prevent big
re-allocations if you can take the performance hit on any calculations.

Happy to be schooled here.

Cameron

On Thursday, May 1, 2014, John Myles White <johnmyleswh...@gmail.com> wrote:

> For this kind of problem, you could allocate a big matrix in advance and
> fill it in column-by-column, rather than push onto an array. Something
> roughly like the following would work:
>
> theta = Array(Float64, 3, 10_000)
> theta[:, 1] = [2.0, 3.0, 4.0]
> for k= 2:10_000
>       theta_cur = theta[:, k - 1]
>       theta_prop = theta_cur + rand(3)
>       likelihoodratio = computelr(theta_prop, theta_curr)
>       if rand() < min(1,likelihoodratio)
>               theta[:, k] = theta_prop
>       else
>               theta[:, k] = theta_curr
>       end
> end
>
> You could make something much faster still with devectorization. The code
> for simulated annealing in Optim.jl might be helpful in this regard,
> although it doesn't retain the full history of the chain.
>
>  -- John
>
> On May 1, 2014, at 9:46 AM, Ethan Anderes 
> <ethanande...@gmail.com<javascript:;>>
> wrote:
>
> > Ok, here is quickest example that I came up with.
> >
> >
> > theta_init = [2.0, 3.0, 4.0]
> > chain = Array{Float64,1}[init]
> > for k=1:10_000
> >       theta_prop = chain[end]
> >       theta_prop += rand(3)
> >       likelihoodratio = computelr(theta_prop, theta_curr)
> >       if rand() < minimum([1,likelihoodratio])
> >               push!(chain, theta_prop)
> >       else
> >               push!(chain, theta_curr)
> >       end
> > end
> >
> > I'm generating a markov chain of vectors. The code proposes a new state
> vector, theta_prop, and pushes it to chain if it satisfies a criterion. The
> problem is that I may run it for 10_000 steps, look at the results and then
> decide to run it for longer... so preinitializing isn't as natural.
> >
> > BTW: thanks a ton...I can't believe I'm having Stefan look at my code!
> >
> >
>
>

Reply via email to