Below, I have defined a function using Which to select model parameters based upon various conditions. My questions are: Is there any danger in writing this function using Recursion?
Is there a more efficient way to write this function that does not require recursion? I'm also only showing an example Uniform distribution here, however the distributions used in the actual program will be a derived distribution from some input data.
1. The first condition selects random variates from a distribution.
2. The second condition selects random variates from a different distribution.
3. The third condition performs a random walk on the random variate selected from 2.
The function below is called repeatedly and after each call I determine if the algorithm has selected a better set of parameters given some fitting criteria. This particular model fitting strategy is known as Sequential Monte Carlo or Particle Filtering. While I have been using standard MCMC, my goal is now to maximize the coverage of parameter space in an efficient manner.
Clear[selectParameters,startingDistributions,y]
m2={k1p,km1};
startingDistributions[model_]:={UniformDistribution[{0.`,50.`}],UniformDistribution[{0.5`,100.`}]};
selectParameters[model_,epsilon_Integer,i_]:=
y[i]=
Which[
epsilon==1,
(model[[#1]]->RandomVariate[startingDistributions[model][[#1]]]&)/@Range[Length[model]],
epsilon>1&&i==1,
(model[[#1]]->RandomVariate[startingDistributions[model][[#1]]]&)/@Range[Length[model]],
epsilon>1&&i>1,
(model[[#1]]->y[i-1][[#1,2]]+0.025 y[i-1][[#1,2]] RandomReal[{-1,1}]&)/@Range[Length[model]]]
test=Table[selectParameters[m2,2,i],{i,1,100}];
lp={k1p,km1}/.test;
ListPlot[{lp[[All,1]],lp[[All,2]]},Joined->True]