This repository has been archived by the owner on Nov 18, 2022. It is now read-only.
/
stanmodel.jl
207 lines (181 loc) · 5.42 KB
/
stanmodel.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import Base: show
"""
# Available top level Method
### Method
```julia
* Sample::Method : Sampling
* Optimize::Method : Optimization
* Diagnose::Method : Diagnostics
* Variational::Method : Variational Bayes
```
"""
abstract type Method end
const DataDict = Dict{String, Any}
mutable struct Random
seed::Int64
end
Random(;seed::Number=-1) = Random(seed)
mutable struct Output
file::String
diagnostic_file::String
refresh::Int64
end
Output(;file::String="", diagnostic_file::String="", refresh::Number=100) =
Output(file, diagnostic_file, refresh)
mutable struct Stanmodel
name::String
nchains::Int
num_warmup::Int
num_samples::Int
thin::Int
id::Int
model::String
model_file::String
monitors::Vector{String}
data_file::String
command::Vector{Base.AbstractCmd}
method::Method
random::Random
init_file::String
output::Output
printsummary::Bool
pdir::String
tmpdir::String
output_format::Symbol
end
"""
# Method Stanmodel
Create a Stanmodel.
### Constructors
```julia
Stanmodel(
method=Sample();
name="noname",
nchains=4,
num_warmup=1000,
num_samples=1000,
thin=1,
model="",
monitors=String[],
random=Random(),
output=Output(),
printsummary=false,
pdir::String=pwd(),
tmpdir::String=joinpath(pwd(), "tmp"),
output_format=:array
)
```
### Required arguments
```julia
* `method::Method` : See ?Method
```
### Optional arguments
```julia
* `name::String` : Name for the model
* `nchains::Int` : Number of chains
* `num_warmup::Int` : Number of samples used for num_warmup
* `num_samples::Int` : Sample iterations
* `thin::Int` : Stan thinning factor
* `model::String` : Stan program source
* `monitors::String[] ` : Variables saved for post-processing
* `random::Random` : Random seed settings
* `output::Output` : File output options
* `printsummary=true` : Show computed stan summary
* `pdir::String` : Working directory
* `tmpdir::String` : Directory where output files are stored
* `output_format::Symbol ` : Output format
```
### CmdStan.jl supports 3 output_format values:
```julia
1. :array # Returns an array of draws (default)
2. :mcmcchains # Return an MCMCChains.Chains object
3. :dataframes # Return an DataFrames.DataFrame object
4. :namedtuple # Returns a NamedTuple object
The first option (the default) returns an Array{Float64, 3} with ndraws,
nvars, nchains as indices.
The 2nd option returns an MCMCChains.Chains object, the 3rd a DataFrame
object and the final option returns a NamedTuple.
```
### Example
```julia
stanmodel = Stanmodel(num_samples=1200, thin=2, name="bernoulli",
model=bernoullimodel);
```
### Related help
```julia
?stan : Run a Stanmodel
?CmdStan.Sample : Sampling settings
?CmdStan.Method : List of available methods
?CmdStan.Output : Output file settings
?CmdStan.DataDict : Input data
?CmdStan.convert_a3d : Options for output formats
```
"""
function Stanmodel(
method=Sample();
name="noname",
nchains=4,
num_warmup=1000,
num_samples=1000,
thin=1,
model="",
monitors=String[],
random=Random(),
output=Output(),
printsummary=true,
pdir::String=pwd(),
tmpdir::String=joinpath(pwd(), "tmp"),
output_format::Symbol=:array)
if !isdir(tmpdir)
mkdir(tmpdir)
end
model_file = "$(name).stan"
if length(model) > 0
update_model_file(joinpath(tmpdir, "$(name).stan"), strip(model))
end
id::Int=0
data_file::String=""
init_file::String=""
cmdarray = fill(``, nchains)
if num_samples != 1000
method.num_samples=num_samples
end
if num_warmup != 1000
method.num_warmup=num_warmup
end
if thin != 1
method.thin=thin
end
Stanmodel(name, nchains,
num_warmup, num_samples, thin,
id, model, model_file, monitors,
data_file, cmdarray, method, random,
init_file, output, printsummary, pdir, tmpdir, output_format);
end
function model_show(io::IO, m::Stanmodel, compact)
println(io, " name = \"$(m.name)\"")
println(io, " nchains = $(m.nchains)")
println(io, " num_samples = $(m.num_samples)")
println(io, " num_warmup = $(m.num_warmup)")
println(io, " thin = $(m.thin)")
println(io, " monitors = $(m.monitors)")
println(io, " model_file = \"$(m.model_file)\"")
println(io, " data_file = \"$(m.data_file)\"")
println(io, " output = Output()")
println(io, " file = \"$(m.output.file)\"")
println(io, " diagnostics_file = \"$(m.output.diagnostic_file)\"")
println(io, " refresh = $(m.output.refresh)")
println(io, " pdir = \"$(m.pdir)\"")
println(io, " tmpdir = \"$(m.tmpdir)\"")
println(io, " output_format = :$(m.output_format)")
if isa(m.method, Sample)
sample_show(io, m.method, compact)
elseif isa(m.method, Optimize)
optimize_show(io, m.method, compact)
elseif isa(m.method, Variational)
variational_show(io, m.method, compact)
else
diagnose_show(io, m.method, compact)
end
end
show(io::IO, m::Stanmodel) = model_show(io, m, false)