Class: CmdStan::Model

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/cmdstan/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.expand_path(stan_file) if stan_file

  @stan_file = stan_file
  @exe_file = exe_file || stan_file.sub(/.stan\z/, "")
  @name = File.basename(@exe_file)

  if compile && !exe_file
    self.compile
  end
end

Instance Attribute Details

#exe_fileObject (readonly)

Returns the value of attribute exe_file.



5
6
7
# File 'lib/cmdstan/model.rb', line 5

def exe_file
  @exe_file
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/cmdstan/model.rb', line 5

def name
  @name
end

#stan_fileObject (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

#codeObject



26
27
28
# File 'lib/cmdstan/model.rb', line 26

def code
  File.read(stan_file)
end

#compileObject



20
21
22
23
24
# File 'lib/cmdstan/model.rb', line 20

def compile
  Dir.chdir(CmdStan.path) do
    run_command "make", @exe_file
  end
end

#optimize(data:, seed: nil, inits: nil, algorithm: nil, iter: nil) ⇒ Object



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
# File 'lib/cmdstan/model.rb', line 72

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



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
# File 'lib/cmdstan/model.rb', line 30

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

  chain ||= 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