Class: Statsample::MLE::Logit
- Defined in:
- lib/statsample/mle/logit.rb
Overview
Logit MLE estimation. See Statsample::Regression for methods to generate a logit regression. Usage:
mle=Statsample::MLE::Logit.new
mle.newton_raphson(x,y)
beta=mle.parameters
likehood=mle.likehood(x, y, beta)
iterations=mle.iterations
Constant Summary
Constants inherited from BaseMLE
BaseMLE::ITERATIONS, BaseMLE::MIN_DIFF, BaseMLE::MIN_DIFF_PARAMETERS
Instance Attribute Summary
Attributes inherited from BaseMLE
#iterations, #output, #parameters, #stop_criteria, #var_cov_matrix, #verbose
Instance Method Summary collapse
-
#f(b, xi) ⇒ Object
F(B’Xi).
-
#first_derivative(x, y, p) ⇒ Object
First derivative of log-likehood function x: Matrix (NxM) y: Matrix (Nx1) p: Matrix (Mx1).
-
#likehood_i(xi, yi, b) ⇒ Object
Likehood for x_i vector, y_i scalar and b parameters.
-
#log_likehood_i(xi, yi, b) ⇒ Object
Log Likehood for x_i vector, y_i scalar and b parameters.
-
#second_derivative(x, y, p) ⇒ Object
Second derivative of log-likehood function x: Matrix (NxM) y: Matrix (Nx1) p: Matrix (Mx1).
Methods inherited from BaseMLE
#initialize, #likehood, #log_likehood, #newton_raphson, #set_default_parameters
Constructor Details
This class inherits a constructor from Statsample::MLE::BaseMLE
Instance Method Details
#f(b, xi) ⇒ Object
F(B’Xi)
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/statsample/mle/logit.rb', line 15 def f(b,xi) p_bx=(xi*b)[0,0] res=(1.0/(1.0+Math::exp(-p_bx))) if res==0.0 res=1e-15 elsif res==1.0 res=0.999999999999999 end res end |
#first_derivative(x, y, p) ⇒ Object
First derivative of log-likehood function x: Matrix (NxM) y: Matrix (Nx1) p: Matrix (Mx1)
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/statsample/mle/logit.rb', line 39 def first_derivative(x,y,p) raise "x.rows!=y.rows" if x.row_size!=y.row_size raise "x.columns!=p.rows" if x.column_size!=p.row_size n = x.row_size k = x.column_size fd = Array.new(k) k.times {|i| fd[i] = [0.0]} n.times do |i| row = x.row(i).to_a value1 = (1-y[i,0]) -p_plus(row,p) k.times do |j| fd[j][0] -= value1*row[j] end end Matrix.rows(fd, true) end |
#likehood_i(xi, yi, b) ⇒ Object
Likehood for x_i vector, y_i scalar and b parameters
26 27 28 |
# File 'lib/statsample/mle/logit.rb', line 26 def likehood_i(xi,yi,b) (f(b,xi)**yi)*((1-f(b,xi))**(1-yi)) end |
#log_likehood_i(xi, yi, b) ⇒ Object
Log Likehood for x_i vector, y_i scalar and b parameters
30 31 32 33 |
# File 'lib/statsample/mle/logit.rb', line 30 def log_likehood_i(xi,yi,b) fbx=f(b,xi) (yi.to_f*Math::log(fbx))+((1.0-yi.to_f)*Math::log(1.0-fbx)) end |
#second_derivative(x, y, p) ⇒ Object
Second derivative of log-likehood function x: Matrix (NxM) y: Matrix (Nx1) p: Matrix (Mx1)
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/statsample/mle/logit.rb', line 60 def second_derivative(x,y,p) raise "x.rows!=y.rows" if x.row_size!=y.row_size raise "x.columns!=p.rows" if x.column_size!=p.row_size n = x.row_size k = x.column_size sd = Array.new(k) k.times do |i| arr = Array.new(k) k.times{ |j| arr[j]=0.0} sd[i] = arr end n.times do |i| row = x.row(i).to_a p_m = p_minus(row,p) k.times do |j| k.times do |l| sd[j][l] -= p_m *(1-p_m)*row[j]*row[l] end end end Matrix.rows(sd, true) end |