Class: CmdStan::Model
Instance Attribute Summary collapse
-
#exe_file ⇒ Object
readonly
Returns the value of attribute exe_file.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#stan_file ⇒ Object
readonly
Returns the value of attribute stan_file.
Instance Method Summary collapse
- #code ⇒ Object
- #compile ⇒ Object
-
#initialize(stan_file: nil, exe_file: nil, compile: true) ⇒ Model
constructor
A new instance of Model.
- #optimize(data:, seed: nil, inits: nil, algorithm: nil, iter: nil) ⇒ Object
- #sample(data:, chains: nil, seed: nil, inits: nil, warmup_iters: nil, sampling_iters: nil) ⇒ Object
Constructor Details
#initialize(stan_file: nil, exe_file: nil, compile: true) ⇒ Model
Returns a new instance of Model.
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/cmdstan/model.rb', line 7 def initialize(stan_file: nil, exe_file: nil, compile: true) # convert to absolute path stan_file = File.(stan_file) if stan_file @stan_file = stan_file @exe_file = exe_file || stan_file.sub(/.stan\z/, extension) @name = File.basename(@exe_file, extension) if compile && !exe_file self.compile end end |
Instance Attribute Details
#exe_file ⇒ Object (readonly)
Returns the value of attribute exe_file.
5 6 7 |
# File 'lib/cmdstan/model.rb', line 5 def exe_file @exe_file end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/cmdstan/model.rb', line 5 def name @name end |
#stan_file ⇒ Object (readonly)
Returns the value of attribute stan_file.
5 6 7 |
# File 'lib/cmdstan/model.rb', line 5 def stan_file @stan_file end |
Instance Method Details
#code ⇒ Object
30 31 32 |
# File 'lib/cmdstan/model.rb', line 30 def code File.read(stan_file) end |
#compile ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/cmdstan/model.rb', line 20 def compile unless ENV["CMDSTAN"] || CmdStan.cmdstan_installed? CmdStan.install_cmdstan end Dir.chdir(CmdStan.path) do run_command make_command, @exe_file, "PRECOMPILED_HEADERS=false" end end |
#optimize(data:, seed: nil, inits: nil, algorithm: nil, iter: nil) ⇒ Object
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 |
# File 'lib/cmdstan/model.rb', line 76 def optimize(data:, seed: nil, inits: nil, algorithm: nil, iter: nil) data_file = Tempfile.new(["cmdstan", ".json"]) data_file.write(data.to_json) data_file.close output_file = Tempfile.new(["cmdstan", ".csv"]) diagnostic_file = Tempfile.new(["cmdstan", ".csv"]) args = [@exe_file] # random args += ["random", "seed=#{seed.to_i}"] if seed # data args += ["data", "file=#{data_file.path}"] if inits init_file = Tempfile.new(["cmdstan", ".json"]) init_file.write(inits.to_json) init_file.close args << "init=#{init_file.path}" end # output args += ["output", "file=#{output_file.path}", "diagnostic_file=#{diagnostic_file.path}"] # method args << "method=optimize" args << "algorithm=#{algorithm.to_s.downcase}" if algorithm args << "iter=#{iter.to_i}" if iter run_command(*args) MLE.new(output_file) end |
#sample(data:, chains: nil, seed: nil, inits: nil, warmup_iters: nil, sampling_iters: nil) ⇒ Object
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 |
# File 'lib/cmdstan/model.rb', line 34 def sample(data:, chains: nil, seed: nil, inits: nil, warmup_iters: nil, sampling_iters: nil) data_file = Tempfile.new(["cmdstan", ".json"]) data_file.write(data.to_json) data_file.close chains ||= 4 output_files = [] chains.times do |chain| output_file = Tempfile.new(["cmdstan", ".csv"]) args = [@exe_file, "id=#{chain + 1}"] # random args += ["random", "seed=#{seed.to_i}"] if seed # data args += ["data", "file=#{data_file.path}"] if inits init_file = Tempfile.new(["cmdstan", ".json"]) init_file.write(inits.to_json) init_file.close args << "init=#{init_file.path}" end # output args += ["output", "file=#{output_file.path}"] # method args += ["method=sample"] args << "num_warmup=#{warmup_iters.to_i}" if warmup_iters args << "num_samples=#{sampling_iters.to_i}" if sampling_iters args += ["algorithm=hmc", "adapt", "engaged=1"] run_command(*args) output_files << output_file end MCMC.new(output_files) end |