Module: Apache::Stat::Inference

Defined in:
lib/ruby-band/apache/stat/inference.rb

Defined Under Namespace

Classes: Chi_square, T_test

Class Method Summary collapse

Class Method Details

.chi_square(*args) ⇒ Object

1) Computes the Chi-Square statistic comparing observed and expected frequency counts.

  • Args :

    • Array -> must be a bidimensional RubyArray.

2) Computes the Chi-Square statistic associated with a chi-square test of independence

based on the input counts array, viewed as a two-way table.
  • Args :

    • Array1 -> must be a RubyArray.

    • Array2 -> must be a RubyArray.



53
54
55
56
57
58
59
60
61
62
# File 'lib/ruby-band/apache/stat/inference.rb', line 53

def self.chi_square(*args)
  if args.length == 2
    Chi_square.chi_square_two_arrays(*args)
  elsif args.length == 1
    raise ArgumentError,"RubyArray must be bidimensional" unless args[0].is_2d?
    Chi_square.chi_square_2d(*args)
  else
    raise ArgumentError, 'Function *args should be two RubyArrays or a bidimensional RubyArray'
  end
end

.chi_square_dataset_compare(observed1, observed2) ⇒ Object

Compare two datasets stored in Ruby Arrays



65
66
67
68
69
70
# File 'lib/ruby-band/apache/stat/inference.rb', line 65

def self.chi_square_dataset_compare(observed1,observed2)
  obj = ChiSquareTest.new
  val = obj.chiSquareDataSetsComparison(observed1.to_java(:long),observed2.to_java(:long)) 
  p_value = obj.chiSquareTestDataSetsComparison(observed1.to_java(:long),observed2.to_java(:long))
  return val,p_value          
end

.mann_whitney_u(array1, array2) ⇒ Object

An implementation of the Mann-Whitney U test (also called Wilcoxon rank-sum test)

  • Args :

    • Array1 -> must be a RubyArray.

    • Array2 -> must be a RubyArray.



77
78
79
80
81
82
83
84
# File 'lib/ruby-band/apache/stat/inference.rb', line 77

def self.mann_whitney_u(array1,array2)
  obj = MannWhitneyUTest.new
  first = array1.to_java :double
  second = array2.to_java :double 
  value = obj.mannWhitneyU first,second
  p_value = obj.mannWhitneyUTest first,second
  return value,p_value
end

.one_way_anova(bidimensional_array) ⇒ Object

Implements one-way ANOVA (analysis of variance) statistics. Tests for differences between two or more categories of univariate data (for example, the body mass index of accountants, lawyers, doctors and computer programmers). When two categories are given, this is equivalent to the TTest.

  • Args :

    • bidimensional_array -> a 2d RubyArray



139
140
141
142
143
144
145
146
147
148
# File 'lib/ruby-band/apache/stat/inference.rb', line 139

def self.one_way_anova(bidimensional_array)
  collection = ArrayList.new 
  bidimensional_array.each do |array|
    collection.add(array.to_java :double)
  end
  obj = OneWayAnova.new
  f_value = obj.anovaFValue(collection)
  p_value = obj.anovaPValue(collection)
  return f_value,p_value
end

.t_test(sample_1, sample_2, homoscedastic = false, paired = false) ⇒ Object

An implementation for Student’s t-tests

  • Args :

    • sample_1 -> an array of numeric values representing a sample

    • sample_2 -> an array of numeric values representing a sample

    • homoscedastic -> set to true for equal variance assumption

    • paired -> set to true if you want to perform a ‘paired’ t test



123
124
125
126
127
128
129
130
131
# File 'lib/ruby-band/apache/stat/inference.rb', line 123

def self.t_test(sample_1,sample_2,homoscedastic=false,paired=false)
  if homoscedastic == true
    T_test.homoscedastic(sample_1,sample_2)
  elsif paired == true
    T_test.paired(sample_1,sample_2)
  else
    T_test.t(sample_1,sample_2)
  end 
end

.wilcoxon_test(array_1, array_2) ⇒ Object

An implementation of the Wilcoxon signed-rank test

  • Args :

    • Array1 -> must be a RubyArray.

    • Array2 -> must be a RubyArray.



19
20
21
22
23
24
25
26
# File 'lib/ruby-band/apache/stat/inference.rb', line 19

def self.wilcoxon_test(array_1,array_2)
  obj = WilcoxonSignedRankTest.new
  first = Core::Utils::double_to_a(array_1)
  second = Core::Utils::double_to_a(array_2)
  val = obj.wilcoxonSignedRank first, second
  p_val = obj.wilcoxonSignedRankTest first, second, true.to_java(:boolean) 
  return val,p_val
end