Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor some code #28

Merged
merged 9 commits into from
Jul 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions lib/statsample-glm/glm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ module GLM
def self.compute(data_set, dependent_column, method, opts={})
opts[:method] = method

# TODO: Remove this const_get jugaad after 1.9.3 support is removed.

Kernel.const_get("Statsample").const_get("GLM").const_get("#{method.capitalize}").new data_set,
dependent_column, opts
Kernel.const_get(
"Statsample::GLM::#{method.capitalize}"
).new data_set, dependent_column, opts
end
end
end
36 changes: 22 additions & 14 deletions lib/statsample-glm/glm/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
module Statsample
module GLM
class Base
extend Gem::Deprecate

def initialize ds, y, opts={}
@opts = opts

set_default_opts_if_any

@data_set = ds.dup(ds.vectors.to_a - [y])
@dependent = ds[y]
@data_set = ds.delete_vector y

if @opts[:constant]
add_constant_vector @opts[:constant]
Expand All @@ -25,13 +26,12 @@ def initialize ds, y, opts={}
algorithm = @opts[:algorithm].upcase
method = @opts[:method].capitalize

# TODO: Remove this const_get jugaad after 1.9.3 support is removed.

@regression = Kernel.const_get("Statsample").const_get("GLM")
.const_get("#{algorithm}").const_get("#{method}")
.new(@data_set, @dependent, @opts)
@regression = Object.const_get(
"Statsample::GLM::#{algorithm}::#{method}"
).new @data_set, @dependent, @opts
end
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@v0dro I see your comment to remove const_get. Is there a way to do it without using const_get?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the need of chaining basically.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the current change fine or should I change to something like Object.const_get('Daru::Vector')?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep a single const_get. In other words go with the SO answer.



# Returns the coefficients of trained model
#
# @param [Symbol] as_a Specifies the form of output
Expand Down Expand Up @@ -73,29 +73,33 @@ def coefficients as_a=:vector
# require 'statsample-glm'
# data_set = Daru::DataFrame.from_csv "spec/data/logistic.csv"
# glm = Statsample::GLM.compute data_set, "y", :logistic, {constant: 1}
# glm.standard_error
# glm.standard_errors
# # #<Daru::Vector:25594060 @name = nil @metadata = {} @size = 3 >
# # nil
# # 0 0.4130813039878828
# # 1 0.7194644911927432
# # 2 0.40380565497038895
#
def standard_error as_a=:vector
def standard_errors as_a=:vector
case as_a
when :hash
se = {}
@data_set.vectors.to_a.each_with_index do |f,i|
se[f.to_sym] = @regression.standard_error[i]
se[f.to_sym] = @regression.standard_errors[i]
end
se
when :array
@regression.standard_error.to_a
@regression.standard_errors.to_a
when :vector
@regression.standard_error
@regression.standard_errors
else
raise ArgumentError, "as_a has to be one of :array, :hash, or :vector"
end
end

# standard_error will be removed soon
alias :standard_error :standard_errors
deprecate :standard_error, :standard_errors, 2017, 1

def iterations
@regression.iterations
Expand Down Expand Up @@ -154,13 +158,17 @@ def residuals
# require 'statsample-glm'
# data_set = Daru::DataFrame.from_csv "spec/data/logistic.csv"
# glm = Statsample::GLM.compute data_set, "y", :logistic, constant: 1
# glm.degree_of_freedom
# glm.degrees_of_freedom
# # => 47
#
def degree_of_freedom
@regression.degree_of_freedom
def degrees_of_freedom
@regression.degrees_of_freedom
end

# degrees_of_freedom will be removed soon
alias :degree_of_freedom :degrees_of_freedom
deprecate :degree_of_freedom, :degrees_of_freedom, 2017, 1

# Returns the optimal value of the log-likelihood function when using MLE algorithm.
# The optimal value is the value of the log-likelihood function at the MLE solution.
#
Expand Down
8 changes: 4 additions & 4 deletions lib/statsample-glm/glm/irls/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module GLM
module IRLS
class Base

attr_reader :coefficients, :standard_error, :iterations,
:fitted_mean_values, :residuals, :degree_of_freedom
attr_reader :coefficients, :standard_errors, :iterations,
:fitted_mean_values, :residuals, :degrees_of_freedom

def initialize data_set, dependent, opts={}
@data_set = data_set.to_matrix
Expand Down Expand Up @@ -61,13 +61,13 @@ def irls

@coefficients = create_vector(b.column_vectors[0])
@iterations = max_iter
@standard_error = create_vector(hessian(@data_set,b).inverse
@standard_errors = create_vector(hessian(@data_set,b).inverse
.diagonal
.map{ |x| -x}
.map{ |y| Math.sqrt(y) })
@fitted_mean_values = create_vector measurement(@data_set,b).to_a.flatten
@residuals = @dependent - @fitted_mean_values
@degree_of_freedom = @dependent.count - @data_set.column_size
@degrees_of_freedom = @dependent.count - @data_set.column_size
end

def create_vector arr
Expand Down
6 changes: 3 additions & 3 deletions lib/statsample-glm/glm/mle/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module GLM
module MLE
class Base
attr_reader :coefficients, :iterations,
:fitted_mean_values, :residuals, :degree_of_freedom,
:fitted_mean_values, :residuals, :degrees_of_freedom,
:log_likelihood

MIN_DIFF_PARAMETERS=1e-2
Expand All @@ -27,15 +27,15 @@ def initialize data_set, dependent, opts
@log_likelihood = _log_likelihood x, y, @coefficients
@fitted_mean_values = create_vector measurement(x, @coefficients).to_a.flatten
@residuals = @dependent - @fitted_mean_values
@degree_of_freedom = @dependent.count - x.column_size
@degrees_of_freedom = @dependent.count - x.column_size

# This jugad is done because the last vector index for Normal is sigma^2
# which we dont want to return to the user.
@coefficients = create_vector(self.is_a?(Statsample::GLM::MLE::Normal) ?
@coefficients.to_a.flatten[0..-2] : @coefficients.to_a.flatten)
end

def standard_error
def standard_errors
out = []

@data_set.vectors.to_a.each_index do |i|
Expand Down
6 changes: 3 additions & 3 deletions spec/logistic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@
end

it "reports correct standard deviations as a Daru::Vector" do
expect_similar_vector(@glm.standard_error, [0.4390, 0.4270, 0.3819,1.9045], 0.001)
expect_similar_vector(@glm.standard_errors, [0.4390, 0.4270, 0.3819,1.9045], 0.001)
end

it "reports correct regression coefficients as an array" do
expect_similar_array(@glm.coefficients(:array), [0.3270, 0.8147, -0.4031,-5.3658], 0.001)
end

it "reports correct standard deviations as an array" do
expect_similar_array(@glm.standard_error(:array), [0.4390, 0.4270, 0.3819,1.9045], 0.001)
expect_similar_array(@glm.standard_errors(:array), [0.4390, 0.4270, 0.3819,1.9045], 0.001)
end

it "reports correct regression coefficients as a hash" do
Expand All @@ -71,7 +71,7 @@
end

it "reports correct standard deviations as a hash" do
expect_similar_hash(@glm.standard_error(:hash), {:a => 0.4390, :b => 0.4270, :c => 0.3819,
expect_similar_hash(@glm.standard_errors(:hash), {:a => 0.4390, :b => 0.4270, :c => 0.3819,
:constant => 1.9045}, 0.001)
end

Expand Down