# How to change the method in matlab stan

Hi guys,

I am trying to change the attribute of the stanmodel in Matlab method to sample but when I run the algorithm, it changes to variational automatically. This is while the default of method must be sample and I need sample method to be able to change the iteration and many attributes only work with sample method. Can you please give me some hint about this (how to change the method to sample). Thank you in advance.

Sampling should be the default. Could you show the code you are using to call Stan?

Aki

Hi,

This is my code:

evol_tree_code = {
βdata {β
β int<lower=0> T; β
β int<lower=0> N; β
β real<lower=0,upper=1> Fhat[N,T]; β
β matrix<lower=0,upper=1> [N,N] UTree; β
β}β
βparameters {β
β real<lower= 0,upper=0.3> mu[N]; β
β vector<lower=0> [N] delta_tau; β
β}β
βtransformed parameters {β
β matrix <lower=0> [N,T] numM; β
β matrix <lower=0,upper=1> [N,T] M; β
β matrix <lower=0> [N,T] F; β
β vector<lower=0> [N] tau;β
β real<lower=0> den[T];β
β tau = UTreeββdelta_tau; β
βfor(t in 1:T){β
β den[t] = 0.001;//this should be a small valueβ
β for (n in 1:N){β
β if (tau[n] > t - 1){β
β numM[n,t] = 0.001;β
β }else{β
β numM[n,t] = exp(mu[n]
(t-1 - tau[n]));β
β }β
β den[t] = den[t] + numM[n,t];β
β }β
β for (n in 1:N){β
β M[n,t] = numM[n,t]/ den[t];β
β }β
β}β
β F = UTree*M; β
β}β
β model {β
β for(t in 1:T) {β
β for (n in 1:N){β
β mu[n] ~ uniform(0, 0.3);β
β delta_tau[n] ~ exponential(2);β
β Fhat[n,t] ~ normal(F[n,t],0.01);β
β }β
β }β
β}β

};
%%
Fdata = [1.0000,1.0000,1.0000,1.0000,1.0000,1.0000,1.0000,1.0000,1.
0000,1.0000,1.0000
; 0, 0, 0, 0, 0, 0, 0, 0, 0,1.0000, 0
; 0, 0,0.0500,0.2000,0.2600,0.1000,0.2600,0.6000,0.6600,0.8800,0.
9700
; 0,0.2800, 0, 0,0.7100,0.9100,0.7600,0.4600,0.4000,0.1300,0.0400
; 0, 0, 0, 0, 0,0.2500,0.3900,0.2200,0.4000,0.1400,0.0300
; 0, 0, 0, 0, 0, 0,0.0800,0.1200,0.1200,0.3400,0.400];

%%
Treedata = [ 0 0 0 0 0 0
;0 0 0 1 1 1
;0 0 0 0 0 0
;1 0 0 0 0 0
;0 0 0 0 0 0
;0 0 1 0 0 0];

UTreedata = [ 1 0 0 0 0 0
;1 1 1 1 1 1
; 0 0 1 0 0 0
; 1 0 0 1 0 0
; 0 0 0 0 1 0
; 0 0 1 0 0 1];

%%
T = size(Fdata,2);
N = size(Fdata,1);

%% initial values

delta_tau_init = ones(N,1);
mu_init = zeros(N,1);

evol_tree_dat = struct(βTβ,T,βNβ,N,βFhatβ,Fdata,βUTreeβ,UTreedata);

evol_tree_initial_values = struct(βdelta_tauβ,delta_tau_init,βmuβ,mu_init);

%%
model = StanModel(βinitβ,evol_tree_initial_values,βfile_
overwriteβ,true,βverboseβ,true,βmethodβ,βsampleβ,βmodel_
codeβ,evol_tree_code,βdataβ,evol_tree_dat);
%%

model.compile();

fit_vb = model.vb();

print(fit_vb);

Thank you.

To sample rather than use the variational, replace

fit_vb = model.vb();

with

fit = model.sampling();

Alternatively, you could just use the βstanβ function, which may be a friendlier wrapper.

1 Like

Thank you.