4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rubyで負の二項分布のヒストグラムを描く

4
Last updated at Posted at 2018-06-08

科学計算用のRubyのプロジェクトである Ruby Numo のツールを使って、負の二項分布のヒストグラムを描いてみました。
fuga2.png

準備

Gnuplot と numo/gnuplot のインストール

sudo apt install gnuplot
gem install numo-gnuplot

GSL と numo/gsl のインストール

GNU Scientific Library (GSL)をインストールします。2018年6月8日時点では ruby-numo は GSL バージョン 2.4 に対応していませんので、必要に応じて 2.3 をソースコードからインストールします。

GSL のインストール

cd gsl-2.3
./configure
make -j4
sudo checkinstall
# make DESTDIR=/path/you/want/to/install/ install

numo/gsl のインストール

git clone https://github.com/ruby-numo/numo-gsl
cd numo-gsl
rake build
gem install pkg/numo-gsl-0.1.2.gem # -- --with-gsl-lib=usr/local/lib
# gem install pkg/numo-gsl-0.1.2.gem -- --with-gsl-lib=/path/you/want/to/install/usr/local/lib

ヒストグラムを描出する

require 'numo/gsl'
require 'numo/gnuplot'

h = Numo::GSL::Histogram.new(20)
h.set_ranges_uniform(0,40)

g = Numo::GSL::Rng::Rand.new
a = g.negative_binomial(0.2, 3.5, 50000)

a.each do |x|
  h.increment x
end

freq = h.bin
center_of_range = h.range.to_a.each_cons(2).map{|a,b| (a+b)/2.0}

Numo.gnuplot do
  set title:"負の二項分布"
  set style:[:fill, :solid, {border:-1}]
  set xtics:["rotate by": -45]
  plot center_of_range, freq, u:"2:xtic(1)", w: :boxes, lc:{rgb:"orange"}, t:"p=0.2, n=3.5"
end

gets # 終了まち

結果
fuga2.png

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?