Class: Stan::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/stan/model.rb

Defined Under Namespace

Classes: InvalidNameError, NoDataGivenError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Model

Returns a new instance of Model.

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/stan/model.rb', line 16

def initialize(name, &block)
  raise InvalidNameError.new("Please ensure that the model name does not start with a number!") if (name.to_s =~ /^\d/)
  @name = name
  @model_string = block.call if block_given?

  FileUtils.mkdir_p(model_directory) unless Dir.exists?(model_directory)

  if File.exists?(filename)
    load_model_file
  else
    create_model_file!
  end
end

Instance Attribute Details

#compiled_model_pathObject (readonly)

Returns the value of attribute compiled_model_path.



7
8
9
# File 'lib/stan/model.rb', line 7

def compiled_model_path
  @compiled_model_path
end

#dataObject

Returns the value of attribute data.



6
7
8
# File 'lib/stan/model.rb', line 6

def data
  @data
end

#last_compiled_atObject (readonly)

Returns the value of attribute last_compiled_at.



7
8
9
# File 'lib/stan/model.rb', line 7

def last_compiled_at
  @last_compiled_at
end

#model_fileObject (readonly)

Returns the value of attribute model_file.



7
8
9
# File 'lib/stan/model.rb', line 7

def model_file
  @model_file
end

#model_stringObject (readonly)

Returns the value of attribute model_string.



7
8
9
# File 'lib/stan/model.rb', line 7

def model_string
  @model_string
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/stan/model.rb', line 7

def name
  @name
end

Class Method Details

.load(name) ⇒ Object



11
12
13
# File 'lib/stan/model.rb', line 11

def load(name)
  new(name)
end

Instance Method Details

#commandsObject



77
78
79
80
81
82
83
# File 'lib/stan/model.rb', line 77

def commands
  @commands ||=
    {
      compile: "make -C #{CmdStanRb.configuration.cmdstan_dir} #{target}",
      model_binary: "#{CmdStanRb.configuration.model_dir}/#{name}/#{name}"
    }
end

#compileObject

Main interactions



34
35
36
37
38
39
40
41
# File 'lib/stan/model.rb', line 34

def compile
  if system(commands[:compile].to_s)
    @last_compiled_at = Time.now
    true
  else
    false
  end
end

#destroyObject



73
74
75
# File 'lib/stan/model.rb', line 73

def destroy
  FileUtils.rm_rf(model_directory)
end

#fit(warmup: 1000, samples: 1000, save_warmup: false, thin: 1) ⇒ Object

Raises:



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

def fit(warmup: 1000, samples: 1000, save_warmup: false, thin: 1)
  raise NoDataGivenError.new("Please specify your model's data before running simulations!") if data.nil?

  additional_arguments =
   [
     "num_samples=#{samples.to_i}",
     "num_warmup=#{warmup.to_i}",
     "save_warmup=#{!!save_warmup ? 1 : 0}",
     "thin=#{thin}",
   ].join(" ")

  FileUtils.chmod(0755, "#{CmdStanRb.configuration.model_dir}/#{name}/#{name}")

  commands[:fit] = [
    commands[:model_binary],
    "sample",
    additional_arguments,
    "data",
    "file=#{data_file.path}"
  ].join(" ")

  `#{commands[:fit].to_s}`

  Stan::FitResult.new(output_csv)
end

#showObject



69
70
71
# File 'lib/stan/model.rb', line 69

def show
  `#{CmdStanRb.configuration.cmdstan_dir}/bin/stansummary output.csv`
end