Class: Statsample::MLE::BaseMLE
- Inherits:
-
Object
- Object
- Statsample::MLE::BaseMLE
- Defined in:
- lib/statsample/mle.rb
Constant Summary collapse
- ITERATIONS =
100- MIN_DIFF =
1e-5- MIN_DIFF_PARAMETERS =
1e-2
Instance Attribute Summary collapse
-
#iterations ⇒ Object
readonly
Iterations.
-
#output ⇒ Object
Returns the value of attribute output.
-
#parameters ⇒ Object
readonly
Parameters (beta coefficients).
-
#stop_criteria ⇒ Object
Could be :parameters or :mle.
-
#var_cov_matrix ⇒ Object
readonly
Variance - Covariance matrix.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
-
#initialize ⇒ BaseMLE
constructor
Model should be a MLE subclass.
-
#likehood(x, y, b) ⇒ Object
Calculate likehood for matrices x and y, given b parameters.
-
#log_likehood(x, y, b) ⇒ Object
Calculate log likehood for matrices x and y, given b parameters.
-
#newton_raphson(x, y, start_values = nil) ⇒ Object
Newton Raphson with automatic stopping criteria.
-
#set_default_parameters(x) ⇒ Object
Creates a zero matrix Mx1, with M=x.M.
Constructor Details
#initialize ⇒ BaseMLE
Model should be a MLE subclass
30 31 32 33 34 35 36 37 |
# File 'lib/statsample/mle.rb', line 30 def initialize() @verbose = false @output = STDOUT @stop_criteria = :parameters @var_cov_matrix = nil @iterations = nil @parameters = nil end |
Instance Attribute Details
#iterations ⇒ Object (readonly)
Iterations
23 24 25 |
# File 'lib/statsample/mle.rb', line 23 def iterations @iterations end |
#output ⇒ Object
Returns the value of attribute output.
17 18 19 |
# File 'lib/statsample/mle.rb', line 17 def output @output end |
#parameters ⇒ Object (readonly)
Parameters (beta coefficients)
25 26 27 |
# File 'lib/statsample/mle.rb', line 25 def parameters @parameters end |
#stop_criteria ⇒ Object
Could be :parameters or :mle
19 20 21 |
# File 'lib/statsample/mle.rb', line 19 def stop_criteria @stop_criteria end |
#var_cov_matrix ⇒ Object (readonly)
Variance - Covariance matrix
21 22 23 |
# File 'lib/statsample/mle.rb', line 21 def var_cov_matrix @var_cov_matrix end |
#verbose ⇒ Object
Returns the value of attribute verbose.
16 17 18 |
# File 'lib/statsample/mle.rb', line 16 def verbose @verbose end |
Instance Method Details
#likehood(x, y, b) ⇒ Object
Calculate likehood for matrices x and y, given b parameters
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/statsample/mle.rb', line 39 def likehood(x,y,b) prod=1 x.row_size.times{|i| xi=Matrix.rows([x.row(i).to_a.collect{|v| v.to_f}]) y_val=y[i,0].to_f fbx=f(b,x) prod=prod*likehood_i(xi, y_val ,b) } prod end |
#log_likehood(x, y, b) ⇒ Object
Calculate log likehood for matrices x and y, given b parameters
50 51 52 53 54 55 56 57 58 |
# File 'lib/statsample/mle.rb', line 50 def log_likehood(x,y,b) sum=0 x.row_size.times{|i| xi=Matrix.rows([x.row(i).to_a.collect{|v| v.to_f}]) y_val=y[i,0].to_f sum+=log_likehood_i(xi,y_val,b) } sum end |
#newton_raphson(x, y, start_values = nil) ⇒ Object
Newton Raphson with automatic stopping criteria. Based on: Von Tessin, P. (2005). Maximum Likelihood Estimation With Java and Ruby
x-
matrix of dependent variables. Should have nxk dimensions
y-
matrix of independent values. Should have nx1 dimensions
@m-
class for @ming. Could be Normal or Logit
start_values-
matrix of coefficients. Should have 1xk dimensions
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/statsample/mle.rb', line 75 def newton_raphson(x,y, start_values=nil) # deep copy? if start_values.nil? parameters=set_default_parameters(x) else parameters = start_values.dup end k=parameters.row_size cv=Matrix.rows([([1.0]*k)]) last_diff=nil raise "n on y != n on x" if x.row_size!=y.row_size h=nil fd=nil if @stop_criteria==:mle old_likehood=log_likehood(x, y, parameters) else old_parameters=parameters end ITERATIONS.times do |i| @iterations=i+1 puts "Set #{i}" if @verbose h = second_derivative(x,y,parameters) if h.singular? raise "Hessian is singular!" end fd = first_derivative(x,y,parameters) parameters = parameters-(h.inverse*(fd)) if @stop_criteria==:parameters flag=true k.times do |j| diff= ( parameters[j,0] - old_parameters[j,0] ) / parameters[j,0] flag=false if diff.abs >= MIN_DIFF_PARAMETERS @output.puts "Parameters #{j}: #{diff}" if @verbose end if flag @var_cov_matrix = h.inverse*-1.0 return parameters end old_parameters=parameters else begin new_likehood = log_likehood(x,y,parameters) @output.puts "[#{i}]Log-MLE:#{new_likehood} (Diff:#{(new_likehood-old_likehood) / new_likehood})" if @verbose if(new_likehood < old_likehood) or ((new_likehood - old_likehood) / new_likehood).abs < MIN_DIFF @var_cov_matrix = h.inverse*-1.0 #@output.puts "Ok" break; end old_likehood=new_likehood rescue =>e puts "#{e}" #puts "dup" end end end @parameters=parameters parameters end |
#set_default_parameters(x) ⇒ Object
Creates a zero matrix Mx1, with M=x.M
62 63 64 65 66 |
# File 'lib/statsample/mle.rb', line 62 def set_default_parameters(x) fd=[0.0]*x.column_size fd.push(0.1) if self.is_a? Statsample::MLE::Normal parameters = Matrix.columns([fd]) end |