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.