Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'Monte Carlo Optimization' topicStudent Support Forum > General > "Monte Carlo Optimization"

< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic
Author Comment/Response
Bill Simpson
04/21/13 11:00pm

This is not a complete answer to your several questions or even to one question, but it might help you.

I replace your For[] with

(tbl = Table[
(* Open Main For Loop *)
ka[[i]] = ka[[i - 1]] + weight*ka[[i - 1]]*RandomReal[{-1.0, 1.0}]; (*
Other distributions are possible *)
kb[[i]] = kb[[i - 1]] + weight*kb[[i - 1]]*RandomReal[{-1.0, 1.0}]; (*************************************************)
(* This is where the evaluation of the parameter sets begins *)
(*************************************************)
decayscore[[i]] = chi2[ka[[i]], kb[[i]]];(*
This is the function evaluated for each parameter set *)
deltaE[[i]] = decayscore[[i]] - decayscore[[i - 1]]; (*
The change in energy for each step *)
freeE[[i]] = Exp[-beta*deltaE[[i]]]; (*
This is a free energy associated with each step *)
v [[i]] = Min[1, freeE[[i]]];
u[[i]] = RandomReal[{0, 1}];
(**************************************************)
(******** Metropolis Selection Algorithm ********)
(**************************************************)
If[u[[i]] < v[[i]], (* If u < v always accept that parameter set,
else accept with probability u < v *)
ka[[i + 1]] = ka[[i]];
kb[[i + 1]] = kb[[i]];
{BPS, {decayscore[[i]], v[[i]], ka[[i]], kb[[i]], i}},
ka[[i]] = ka[[i - 1]];
kb[[i]] = kb[[i - 1]];
{TS, {decayscore[[i]], v[[i]], ka[[i]], kb[[i]], i}}
]
(* Close Main For Loop // How long does this loop take to run? *)
, {i, runlength-1}];
bestParamSets = Cases[tbl, {BPS, v_} -> v];
thrownsets = Cases[tbl, {TS, v_} -> v];
) // AbsoluteTiming

You can try to compare that side-by-side with your original code.

Mathematica, unlike other languages, does not use {} to group code statements. That is instead usually done with ; and occasionally () can be used when precedence requires.

Table[] is usually a somewhat more functional tool than For[] with assignments.

There are more changes that might be made to the code, but perhaps this will give you some ideas you can use and I hesitate to do more reorganization.

I'm not absolutely certain I have produced identical results to your original code, the RandomReal[] prevents me from directly comparing my result with yours, so please test this carefully before depending on it.

URL: ,

Subject (listing for 'Monte Carlo Optimization')
Author Date Posted
Monte Carlo Optimization Patrick 04/19/13 10:11am
Re: Monte Carlo Optimization Bill Simpson 04/21/13 11:00pm
Re: Re: Monte Carlo Optimization Patrick 04/24/13 7:33pm
Re: Re: Re: Monte Carlo Optimization Bill Simpson 04/25/13 4:38pm
Re: Re: Re: Re: Monte Carlo Optimization Patrick 05/01/13 09:23am
Re: Re: Re: Re: Re: Monte Carlo Optimization Bill Simpson 05/02/13 9:08pm
Re: Re: Re: Re: Re: Re: Monte Carlo Optimization Patrick 05/03/13 1:12pm
< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic