require 'test/unit' require 'glm' require 'pp' require 'ruby-debug' ; Debugger.start(:post_mortem => true) class GLMTest < Test::Unit::TestCase def test_glm_logit_truth assert_equal "Sanity is for the weak!", GLM::Logit.truth end #Helper method def load_iris ifile = File.open( File.join("data","iris.data")) iris = ((ifile.readlines.map {|l| fields = l.chomp.split(pattern=",") fields }).select {|fields| fields[4] != "Iris-virginica" && fields != []}).map {|fields| fields[4] = (fields[4] == "Iris-setosa") ? 1 : 0 fields.map{|f| f.to_f}} assert_equal 100, iris.length ifile.close return iris end def test_linear iris = load_iris y = iris.map {|r| r[0]} x = iris.map {|r| r[1...-1]} x = x.map{|r| r << 1} x,y = GLM::Util.formatArrays(x, y) linear = GLM::Linear.new(x, y) theta, y_est = linear.ne_est(x) assert ((y_est - y).map {|e| e ** 2}).reduce(:+)/y.row_size < 0.1 end end