Giter Club home page Giter Club logo

cmdstan-ruby's Introduction

CmdStan.rb

Bayesian inference for Ruby, powered by CmdStan

Build Status

Installation

Add this line to your application’s Gemfile:

gem "cmdstan"

Getting Started

Create a Stan file, like bernoulli.stan

data {
  int<lower=0> N;
  int<lower=0,upper=1> y[N];
}
parameters {
  real<lower=0,upper=1> theta;
}
model {
  theta ~ beta(1,1);
  y ~ bernoulli(theta);
}

Compile the model (this can take a few minutes the first time as CmdStan downloads and builds)

model = CmdStan::Model.new(stan_file: "bernoulli.stan")

Fit the model

data = {"N" => 10, "y" => [0, 1, 0, 0, 0, 0, 0, 0, 0, 1]}
fit = model.sample(data: data, chains: 5)

Summarize the results

fit.summary

Load a compiled model

model = CmdStan::Model.new(exe_file: "bernoulli")

Check out Strata for shipping models

Maximum Likelihood Estimation

mle = model.optimize(data: data)
mle.optimized_params

Reference

Check if CmdStan is installed

CmdStan.cmdstan_installed?

Install CmdStan manually

CmdStan.install_cmdstan

Credits

This library is modeled after the CmdStanPy API.

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

git clone https://github.com/ankane/cmdstan-ruby.git
cd cmdstan-ruby
bundle install
bundle exec rake test

cmdstan-ruby's People

Contributors

ankane avatar blackrez avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

cmdstan-ruby's Issues

Linux ARM 64 support

Hello,

The installation of rb-prophet fails on linux arm64.

I think this is due to the usage of stanc for amd64 processors.

Building native extensions. This could take a while...
ERROR:  Error installing prophet-rb:
	ERROR: Failed to build gem native extension.

    current directory: /home/nabil/.rvm/gems/ruby-3.1.0/gems/prophet-rb-0.2.5/ext/prophet
/home/nabil/.rvm/rubies/ruby-3.1.0/bin/ruby -I /home/nabil/.rvm/rubies/ruby-3.1.0/lib/ruby/3.1.0 -r ./siteconf20220207-43224-e9mn2a.rb extconf.rb
g++ -std=c++1y -pthread -D_REENTRANT -Wno-sign-compare -Wno-ignored-attributes      -I stan/lib/stan_math/lib/tbb_2020.3/include    -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.9 -I stan/lib/stan_math/lib/boost_1.75.0 -I stan/lib/stan_math/lib/sundials_5.7.0/include    -DBOOST_DISABLE_ASSERTS          -c -MT stan/src/stan/model/model_header.hpp.gch -MT stan/src/stan/model/model_header.d -MM -E -MG -MP -MF stan/src/stan/model/model_header.d stan/src/stan/model/model_header.hpp
g++ -std=c++1y -pthread -D_REENTRANT -Wno-sign-compare -Wno-ignored-attributes      -I stan/lib/stan_math/lib/tbb_2020.3/include    -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.9 -I stan/lib/stan_math/lib/boost_1.75.0 -I stan/lib/stan_math/lib/sundials_5.7.0/include    -DBOOST_DISABLE_ASSERTS          -c -MT src/cmdstan/main.o -MM -E -MG -MP -MF src/cmdstan/main.d src/cmdstan/main.cpp

--- Translating Stan model to C++ code ---
bin/stanc  --o=/tmp/prophet.hpp /tmp/prophet.stan
bash: bin/stanc: cannot execute binary file: Exec format error
make: *** [make/program:50: /tmp/prophet.hpp] Error 126
/home/nabil/.rvm/gems/ruby-3.1.0/gems/cmdstan-0.1.8/lib/cmdstan/utils.rb:24:in `run_command': Command failed (CmdStan::Error)
	from /home/nabil/.rvm/gems/ruby-3.1.0/gems/cmdstan-0.1.8/lib/cmdstan/model.rb:22:in `block in compile'
	from /home/nabil/.rvm/gems/ruby-3.1.0/gems/cmdstan-0.1.8/lib/cmdstan/model.rb:21:in `chdir'
	from /home/nabil/.rvm/gems/ruby-3.1.0/gems/cmdstan-0.1.8/lib/cmdstan/model.rb:21:in `compile'
	from /home/nabil/.rvm/gems/ruby-3.1.0/gems/cmdstan-0.1.8/lib/cmdstan/model.rb:16:in `initialize'
	from extconf.rb:13:in `new'
	from extconf.rb:13:in `<main>'

extconf failed, exit code 1

Binaries for cmdstan are available for linux-arm64, what is the best way to support arm64 ?

Thanks.

make -j launches too many jobs

Hi! I was enabling prophet support to blazer and had to compile this gem, but the make -j launches a lot of concurrent jobs and it nearly crashed my dev machine. Would you accept a patch that removes it or maybe adds an env variable to set the amount of jobs?

Ideas

Ideas

  • Add more methods and options
  • Add support for multiple cores - use parallel gem
  • Add support for Windows

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.