Project

mopti

0.0
No release in over a year
Multivariate Optimization Library in Ruby. Mopti supports Nelder-Mead simplex method and Scaled Conjugate Gradient.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 0.9.1
 Project Readme

Mopti

Build Status Gem Version BSD 3-Clause License Documentation

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'

# Define objective function.
obj_fnc = proc do |w, x, y|
  n_samples = x.shape[0]
  ((y - x.dot(w))**2).sum.fdiv(n_samples)
end

# Explanatory variables.
x = Numo::DFloat[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Dependent variables.
y = 3 * x + 2

# Extend variable for intercept.
xb = Numo::DFloat.vstack([x, [1] * x.size]).transpose.dup

# Optimize parameter vectors.
res = Mopti::minimize(algorithm: 'Nelder-Mead', fnc: obj_fnc, x_init: Numo::DFloat.zeros(2), args: [xb, y])

# Output result.
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}

example1

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.