Mopti
Mopti is a multivariate optimization library in Ruby.
Mopti supports Nelder-Mead simplex method and Scaled Conjugate Gradient.
Installation
Add this line to your application's Gemfile:
gem 'mopti'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install mopti
Documentation
Usage
Example 1. Linear Regression with Nelder-Mead simplex method
require 'numo/narray'
require 'numo/gnuplot'
require 'mopti'
obj_fnc = proc do |w, x, y|
n_samples = x.shape[0]
((y - x.dot(w))**2).sum.fdiv(n_samples)
end
x = Numo::DFloat[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = 3 * x + 2
xb = Numo::DFloat.vstack([x, [1] * x.size]).transpose.dup
res = Mopti::minimize(algorithm: 'Nelder-Mead', fnc: obj_fnc, x_init: Numo::DFloat.zeros(2), args: [xb, y])
pp res
a, b = res[:x].to_a
Numo.gnuplot do
set(terminal: 'png')
set(output: 'example1.png')
plot [[0, 10], [a * x[0] + b, a * x[-1] + b], w: :lines, lw: 1, t: 'parameters'],
[x, y, pt: 6, ps: 2, t: 'data']
end
$ brew install gnuplot
$ gem install mopti numo-narray numo-gnuplot
$ ruby example1.rb
{:x=>Numo::DFloat(view)#shape=[2]
[3, 2],
:n_fev=>177,
:n_iter=>91,
:fnc=>2.290874014308807e-13}
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/yoshoku/mopti. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
Code of Conduct
Everyone interacting in the Mopti project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.