cmd_stan_rb 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -1
- data/Gemfile.lock +1 -1
- data/README.md +13 -2
- data/demo.ipynb +89 -75
- data/examples/exponential.ipynb +58 -348
- data/examples/linear_regression.ipynb +31 -7
- data/lib/cmd_stan_rb/version.rb +1 -1
- data/lib/stan/examples.rb +13 -0
- data/lib/stan/model.rb +47 -24
- metadata +7 -6
@@ -9,22 +9,46 @@
|
|
9
9
|
},
|
10
10
|
{
|
11
11
|
"cell_type": "code",
|
12
|
-
"execution_count":
|
12
|
+
"execution_count": null,
|
13
13
|
"metadata": {},
|
14
14
|
"outputs": [
|
15
15
|
{
|
16
16
|
"name": "stderr",
|
17
17
|
"output_type": "stream",
|
18
18
|
"text": [
|
19
|
-
"/Users/robin/.rvm/gems/ruby-2.6.5/gems/cmd_stan_rb-0.3.
|
20
|
-
"/Users/robin/private_projects/cmd_stan_rb/lib/cmd_stan_rb/version.rb:2: warning: previous definition of VERSION was here\n"
|
19
|
+
"/Users/robin.neumann/.rvm/gems/ruby-2.6.5/gems/cmd_stan_rb-0.3.1/lib/cmd_stan_rb/version.rb:2: warning: already initialized constant CmdStanRb::VERSION\n",
|
20
|
+
"/Users/robin.neumann/private_projects/cmd_stan_rb/lib/cmd_stan_rb/version.rb:2: warning: previous definition of VERSION was here\n"
|
21
21
|
]
|
22
22
|
},
|
23
23
|
{
|
24
|
-
"
|
25
|
-
"
|
26
|
-
"
|
27
|
-
|
24
|
+
"ename": "LoadError",
|
25
|
+
"evalue": "cannot load such file -- rubystats",
|
26
|
+
"output_type": "error",
|
27
|
+
"traceback": [
|
28
|
+
"\u001b[31mLoadError\u001b[0m: cannot load such file -- rubystats",
|
29
|
+
"/Users/robin.neumann/.rvm/rubies/ruby-2.6.5/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'",
|
30
|
+
"/Users/robin.neumann/.rvm/rubies/ruby-2.6.5/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'",
|
31
|
+
"(pry):3:in `<main>'",
|
32
|
+
"/Users/robin.neumann/.rvm/gems/ruby-2.6.5/gems/pry-0.13.1/lib/pry/pry_instance.rb:290:in `eval'",
|
33
|
+
"/Users/robin.neumann/.rvm/gems/ruby-2.6.5/gems/pry-0.13.1/lib/pry/pry_instance.rb:290:in `evaluate_ruby'",
|
34
|
+
"/Users/robin.neumann/.rvm/gems/ruby-2.6.5/gems/pry-0.13.1/lib/pry/pry_instance.rb:659:in `handle_line'",
|
35
|
+
"/Users/robin.neumann/.rvm/gems/ruby-2.6.5/gems/pry-0.13.1/lib/pry/pry_instance.rb:261:in `block (2 levels) in eval'",
|
36
|
+
"/Users/robin.neumann/.rvm/gems/ruby-2.6.5/gems/pry-0.13.1/lib/pry/pry_instance.rb:260:in `catch'",
|
37
|
+
"/Users/robin.neumann/.rvm/gems/ruby-2.6.5/gems/pry-0.13.1/lib/pry/pry_instance.rb:260:in `block in eval'",
|
38
|
+
"/Users/robin.neumann/.rvm/gems/ruby-2.6.5/gems/pry-0.13.1/lib/pry/pry_instance.rb:259:in `catch'",
|
39
|
+
"/Users/robin.neumann/.rvm/gems/ruby-2.6.5/gems/pry-0.13.1/lib/pry/pry_instance.rb:259:in `eval'",
|
40
|
+
"/Users/robin.neumann/.rvm/gems/ruby-2.6.5/gems/iruby-0.4.0/lib/iruby/backend.rb:66:in `eval'",
|
41
|
+
"/Users/robin.neumann/.rvm/gems/ruby-2.6.5/gems/iruby-0.4.0/lib/iruby/backend.rb:12:in `eval'",
|
42
|
+
"/Users/robin.neumann/.rvm/gems/ruby-2.6.5/gems/iruby-0.4.0/lib/iruby/kernel.rb:90:in `execute_request'",
|
43
|
+
"/Users/robin.neumann/.rvm/gems/ruby-2.6.5/gems/iruby-0.4.0/lib/iruby/kernel.rb:49:in `dispatch'",
|
44
|
+
"/Users/robin.neumann/.rvm/gems/ruby-2.6.5/gems/iruby-0.4.0/lib/iruby/kernel.rb:38:in `run'",
|
45
|
+
"/Users/robin.neumann/.rvm/gems/ruby-2.6.5/gems/iruby-0.4.0/lib/iruby/command.rb:110:in `run_kernel'",
|
46
|
+
"/Users/robin.neumann/.rvm/gems/ruby-2.6.5/gems/iruby-0.4.0/lib/iruby/command.rb:40:in `run'",
|
47
|
+
"/Users/robin.neumann/.rvm/gems/ruby-2.6.5/gems/iruby-0.4.0/bin/iruby:5:in `<top (required)>'",
|
48
|
+
"/Users/robin.neumann/.rvm/gems/ruby-2.6.5/bin/iruby:23:in `load'",
|
49
|
+
"/Users/robin.neumann/.rvm/gems/ruby-2.6.5/bin/iruby:23:in `<main>'",
|
50
|
+
"/Users/robin.neumann/.rvm/gems/ruby-2.6.5/bin/ruby_executable_hooks:24:in `eval'",
|
51
|
+
"/Users/robin.neumann/.rvm/gems/ruby-2.6.5/bin/ruby_executable_hooks:24:in `<main>'"
|
28
52
|
]
|
29
53
|
}
|
30
54
|
],
|
data/lib/cmd_stan_rb/version.rb
CHANGED
data/lib/stan/examples.rb
CHANGED
@@ -18,6 +18,19 @@
|
|
18
18
|
}
|
19
19
|
}
|
20
20
|
end
|
21
|
+
|
22
|
+
# Convenience method to cross check on bundle console if all pieces
|
23
|
+
# are glued together correctly. Should get supported by far more test
|
24
|
+
# cases, but until I managed this this is a good assistence 😅
|
25
|
+
def run_bernoulli_integration_test
|
26
|
+
model = Stan::Model.new("test_#{rand(1000000)}") do
|
27
|
+
Stan::Examples.bernoulli
|
28
|
+
end
|
29
|
+
model.compile
|
30
|
+
model.data = {"N"=>4, "y"=>[1,1,0,0]}
|
31
|
+
result = model.fit(warm_up: 5, samples: 10)
|
32
|
+
result.theta
|
33
|
+
end
|
21
34
|
end
|
22
35
|
end
|
23
36
|
end
|
data/lib/stan/model.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
module Stan
|
2
2
|
class Model
|
3
3
|
class NoDataGivenError < StandardError ;; end
|
4
|
+
class InvalidNameError < StandardError ;; end
|
4
5
|
|
5
|
-
attr_accessor :
|
6
|
-
attr_reader :model_string, :model_file
|
6
|
+
attr_accessor :data
|
7
|
+
attr_reader :compiled_model_path, :model_string, :model_file,
|
8
|
+
:name, :last_compiled_at
|
7
9
|
|
8
10
|
class << self
|
9
11
|
def load(name)
|
@@ -12,18 +14,11 @@
|
|
12
14
|
end
|
13
15
|
|
14
16
|
def initialize(name, &block)
|
17
|
+
raise InvalidNameError.new("Please ensure that the model name does not start with a number!") if (name.to_s =~ /^\d/)
|
15
18
|
@name = name
|
16
|
-
|
17
|
-
# I'd like to invent a convenient DSL that
|
18
|
-
# is similar to writing "direct" Stan code,
|
19
|
-
# but to do so I need to get a better understanding
|
20
|
-
# of Stan as a language/DSL itself. Until that
|
21
|
-
# I "approximate" my goal by using a block for initialization,
|
22
|
-
# where the block always needs to return the Stan model
|
23
|
-
# as a string. You can load it from a file or write in directly!
|
24
19
|
@model_string = block.call if block_given?
|
25
20
|
|
26
|
-
|
21
|
+
FileUtils.mkdir_p(model_directory) unless Dir.exists?(model_directory)
|
27
22
|
|
28
23
|
if File.exists?(filename)
|
29
24
|
load_model_file
|
@@ -37,15 +32,38 @@
|
|
37
32
|
#
|
38
33
|
|
39
34
|
def compile
|
40
|
-
system(commands[:compile])
|
41
|
-
|
35
|
+
if system(commands[:compile].to_s)
|
36
|
+
@last_compiled_at = Time.now
|
37
|
+
true
|
38
|
+
else
|
39
|
+
false
|
40
|
+
end
|
42
41
|
end
|
43
42
|
|
44
|
-
def fit
|
43
|
+
def fit(warmup: 1000, samples: 1000, save_warmup: false, thin: 1)
|
45
44
|
raise NoDataGivenError.new("Please specify your model's data before running simulations!") if data.nil?
|
46
|
-
|
47
|
-
|
48
|
-
|
45
|
+
|
46
|
+
additional_arguments =
|
47
|
+
[
|
48
|
+
"num_samples=#{samples.to_i}",
|
49
|
+
"num_warmup=#{warmup.to_i}",
|
50
|
+
"save_warmup=#{!!save_warmup ? 1 : 0}",
|
51
|
+
"thin=#{thin}",
|
52
|
+
].join(" ")
|
53
|
+
|
54
|
+
FileUtils.chmod(0755, "#{CmdStanRb.configuration.model_dir}/#{name}/#{name}")
|
55
|
+
|
56
|
+
commands[:fit] = [
|
57
|
+
commands[:model_binary],
|
58
|
+
"sample",
|
59
|
+
additional_arguments,
|
60
|
+
"data",
|
61
|
+
"file=#{data_file.path}"
|
62
|
+
].join(" ")
|
63
|
+
|
64
|
+
`#{commands[:fit].to_s}`
|
65
|
+
|
66
|
+
Stan::FitResult.new(output_csv)
|
49
67
|
end
|
50
68
|
|
51
69
|
def show
|
@@ -53,16 +71,15 @@
|
|
53
71
|
end
|
54
72
|
|
55
73
|
def destroy
|
56
|
-
|
57
|
-
`rm -rvf #{path}`
|
58
|
-
{state: :ok, destroyed_path: path}
|
74
|
+
FileUtils.rm_rf(model_directory)
|
59
75
|
end
|
60
76
|
|
61
77
|
def commands
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
78
|
+
@commands ||=
|
79
|
+
{
|
80
|
+
compile: "make -C #{CmdStanRb.configuration.cmdstan_dir} #{target}",
|
81
|
+
model_binary: "#{CmdStanRb.configuration.model_dir}/#{name}/#{name}"
|
82
|
+
}
|
66
83
|
end
|
67
84
|
|
68
85
|
private
|
@@ -102,5 +119,11 @@
|
|
102
119
|
"#{CmdStanRb.configuration.model_dir}/#{name}/#{name}.stan"
|
103
120
|
end
|
104
121
|
|
122
|
+
def output_csv
|
123
|
+
File.read("output.csv")
|
124
|
+
rescue
|
125
|
+
""
|
126
|
+
end
|
127
|
+
|
105
128
|
end
|
106
129
|
end
|
metadata
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cmd_stan_rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- 'Robin Neumann
|
8
8
|
|
9
9
|
'
|
10
|
-
autorequire:
|
10
|
+
autorequire:
|
11
11
|
bindir: exe
|
12
12
|
cert_chain: []
|
13
|
-
date: 2020-
|
13
|
+
date: 2020-08-09 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rspec
|
@@ -52,6 +52,7 @@ files:
|
|
52
52
|
- demo.ipynb
|
53
53
|
- examples/exponential.ipynb
|
54
54
|
- examples/linear_regression.ipynb
|
55
|
+
- examples/scumazon_sales_data.csv
|
55
56
|
- lib/cmd_stan_rb.rb
|
56
57
|
- lib/cmd_stan_rb/configuration.rb
|
57
58
|
- lib/cmd_stan_rb/version.rb
|
@@ -66,7 +67,7 @@ metadata:
|
|
66
67
|
homepage_uri: https://github.com/neumanrq/cmd_stan_rb
|
67
68
|
source_code_uri: https://github.com/neumanrq/cmd_stan_rb
|
68
69
|
changelog_uri: https://github.com/neumanrq/cmd_stan_rb
|
69
|
-
post_install_message:
|
70
|
+
post_install_message:
|
70
71
|
rdoc_options: []
|
71
72
|
require_paths:
|
72
73
|
- lib
|
@@ -81,8 +82,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
82
|
- !ruby/object:Gem::Version
|
82
83
|
version: '0'
|
83
84
|
requirements: []
|
84
|
-
rubygems_version: 3.0.
|
85
|
-
signing_key:
|
85
|
+
rubygems_version: 3.0.8
|
86
|
+
signing_key:
|
86
87
|
specification_version: 4
|
87
88
|
summary: Ruby interface to CmdStan, the command line interface to Stan, a high performance
|
88
89
|
statistical computing platform
|