Class: CmdStan::MCMC

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_files) ⇒ MCMC

private TODO use runset for args



9
10
11
12
# File 'lib/cmdstan/mcmc.rb', line 9

def initialize(output_files)
  @output_files = output_files
  validate_csv_files
end

Instance Attribute Details

#column_namesObject (readonly)

Returns the value of attribute column_names.



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

def column_names
  @column_names
end

#drawsObject (readonly)

Returns the value of attribute draws.



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

def draws
  @draws
end

Instance Method Details

#sampleObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cmdstan/mcmc.rb', line 14

def sample
  sample = []
  @output_files.each_with_index do |output_file, chain|
    i = 0
    CSV.foreach(output_file.path, skip_lines: /^#/, headers: true) do |row|
      (sample[i] ||= [])[chain] = row.to_h.values.map(&:to_f)
      i += 1
    end
    raise "Bug detected" if i != draws
  end
  sample
end

#summaryObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cmdstan/mcmc.rb', line 27

def summary
  csv_file = Tempfile.new
  run_command "#{CmdStan.path}/bin/stansummary", "--csv_file=#{csv_file.path}", *@output_files.map(&:path)

  result = {}
  CSV.foreach(csv_file.path, headers: true, converters: :numeric) do |row|
    value = row.to_h
    name = value.delete("name")
    result[name] = value if name == "lp__" || !name.end_with?("__")
  end
  result
end