Class: AdaBoost::ContingencyTable

Inherits:
Object
  • Object
show all
Defined in:
lib/adaboost/contingency_table.rb

Instance Method Summary collapse

Constructor Details

#initializeContingencyTable

Returns a new instance of ContingencyTable.



5
6
7
# File 'lib/adaboost/contingency_table.rb', line 5

def initialize
  @table = [[0, 0], [0, 0]]
end

Instance Method Details

#accuracyObject



93
94
95
# File 'lib/adaboost/contingency_table.rb', line 93

def accuracy
  (true_positive + true_negative) / total_population.to_f
end

#add_prediction(y, h) ⇒ Object



25
26
27
# File 'lib/adaboost/contingency_table.rb', line 25

def add_prediction(y, h)
  @table[class_to_index(y)][class_to_index(h)] += 1
end

#class_to_index(k) ⇒ Object



178
179
180
# File 'lib/adaboost/contingency_table.rb', line 178

def class_to_index(k)
  (k > 0) ? 1 : 0
end

#condition_negativeObject



53
54
55
# File 'lib/adaboost/contingency_table.rb', line 53

def condition_negative
  false_positive + true_negative
end

#condition_positiveObject



49
50
51
# File 'lib/adaboost/contingency_table.rb', line 49

def condition_positive
  true_positive + false_negative
end

#diagnostic_odds_ratioObject



125
126
127
# File 'lib/adaboost/contingency_table.rb', line 125

def diagnostic_odds_ratio
  positive_likelihood_ratio / negative_likelihood_ratio.to_f
end

#fall_outObject



77
78
79
# File 'lib/adaboost/contingency_table.rb', line 77

def fall_out
  false_positive_rate
end

#false_discovery_rateObject



105
106
107
# File 'lib/adaboost/contingency_table.rb', line 105

def false_discovery_rate
  false_positive / outcome_positive.to_f
end

#false_negativeObject



21
22
23
# File 'lib/adaboost/contingency_table.rb', line 21

def false_negative
  @table[1][0]
end

#false_negative_rateObject



81
82
83
# File 'lib/adaboost/contingency_table.rb', line 81

def false_negative_rate
  false_negative / condition_positive.to_f
end

#false_omission_rateObject



109
110
111
# File 'lib/adaboost/contingency_table.rb', line 109

def false_omission_rate
  false_negative / outcome_negative.to_f
end

#false_positiveObject



13
14
15
# File 'lib/adaboost/contingency_table.rb', line 13

def false_positive
  @table[0][1]
end

#false_positive_rateObject



73
74
75
# File 'lib/adaboost/contingency_table.rb', line 73

def false_positive_rate
  false_positive / condition_negative.to_f
end

#negative_likelihood_ratioObject



121
122
123
# File 'lib/adaboost/contingency_table.rb', line 121

def negative_likelihood_ratio
  false_negative_rate / true_negative_rate.to_f
end

#negative_predictive_valueObject



113
114
115
# File 'lib/adaboost/contingency_table.rb', line 113

def negative_predictive_value
  true_negative / outcome_negative.to_f
end

#outcome_negativeObject



33
34
35
# File 'lib/adaboost/contingency_table.rb', line 33

def outcome_negative
  true_negative + false_negative
end

#outcome_positiveObject



29
30
31
# File 'lib/adaboost/contingency_table.rb', line 29

def outcome_positive
  true_positive + false_positive
end

#positive_likelihood_ratioObject



117
118
119
# File 'lib/adaboost/contingency_table.rb', line 117

def positive_likelihood_ratio
  true_positive_rate / false_positive_rate.to_f
end

#positive_predictive_valueObject



97
98
99
# File 'lib/adaboost/contingency_table.rb', line 97

def positive_predictive_value
  true_positive / outcome_positive.to_f
end

#precisionObject



101
102
103
# File 'lib/adaboost/contingency_table.rb', line 101

def precision
  positive_predictive_value
end

#predicted_condition_negativeObject



45
46
47
# File 'lib/adaboost/contingency_table.rb', line 45

def predicted_condition_negative
  false_negative + true_negative
end

#predicted_condition_positiveObject



41
42
43
# File 'lib/adaboost/contingency_table.rb', line 41

def predicted_condition_positive
  true_positive + false_positive
end

#prevalenceObject



57
58
59
# File 'lib/adaboost/contingency_table.rb', line 57

def prevalence
  condition_positive / total_population.to_f
end

#recallObject



65
66
67
# File 'lib/adaboost/contingency_table.rb', line 65

def recall
  true_positive_rate
end

#sensitivityObject



69
70
71
# File 'lib/adaboost/contingency_table.rb', line 69

def sensitivity
  true_positive_rate
end

#specificityObject



89
90
91
# File 'lib/adaboost/contingency_table.rb', line 89

def specificity
  true_negative_rate
end

#to_sObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/adaboost/contingency_table.rb', line 129

def to_s
  "\nTotal population: %d\t \
  \nCondition positive: %d\t \
  \nCondition negative: %d\t \
  \nPredicted Condition positive: %d\t \
  \nPredicted Condition negative: %d\t \
  \nTrue positive: %d\t \
  \nTrue negative: %d\t \
  \nFalse Negative: %d\t \
  \nFalse Positive: %d\t \
  \nPrevalence = Σ Condition positive / Σ Total population: %f\t \
  \nTrue positive rate (TPR) = Σ True positive / Σ Condition positive: %f\t \
  \nFalse positive rate (FPR) = Σ False positive / Σ Condition negative: %f\t \
  \nFalse negative rate (FNR) = Σ False negative / Σ Condition positive: %f\t \
  \nTrue negative rate (TNR) = Σ True negative / Σ Condition negative: %f\t \
  \nAccuracy (ACC) = Σ True positive \ Σ True negative / Σ Total population: %f\t \
  \nPositive predictive value (PPV) = Σ True positive / Σ Test outcome positive: %f\t \
  \nFalse discovery rate (FDR) = Σ False positive / Σ Test outcome positive: %f\t \
  \nFalse omission rate (FOR) = Σ False negative / Σ Test outcome negative: %f\t \
  \nNegative predictive value (NPV) = Σ True negative / Σ Test outcome negative: %f\t \
  \nPositive likelihood ratio (LR\) = TPR / FPR: %f\t \
  \nNegative likelihood ratio (LR−) = FNR / TNR: %f\t \
  \nDiagnostic odds ratio (DOR) = LR+ / LR−: %f\t" %
  [
    total_population,
    condition_positive,
    condition_negative,
    predicted_condition_positive,
    predicted_condition_negative,
    true_positive,
    true_negative,
    false_negative,
    false_positive,
    prevalence,
    true_positive_rate,
    false_positive_rate,
    false_negative_rate,
    true_negative_rate,
    accuracy,
    positive_predictive_value,
    false_discovery_rate,
    false_omission_rate,
    negative_predictive_value,
    positive_likelihood_ratio,
    negative_likelihood_ratio,
    diagnostic_odds_ratio
  ]
end

#total_populationObject



37
38
39
# File 'lib/adaboost/contingency_table.rb', line 37

def total_population
  @table[0][0] + @table[0][1] + @table[1][0] + @table[1][1]
end

#true_negativeObject



17
18
19
# File 'lib/adaboost/contingency_table.rb', line 17

def true_negative
  @table[0][0]
end

#true_negative_rateObject



85
86
87
# File 'lib/adaboost/contingency_table.rb', line 85

def true_negative_rate
  true_negative / condition_negative.to_f
end

#true_positiveObject



9
10
11
# File 'lib/adaboost/contingency_table.rb', line 9

def true_positive
  @table[1][1]
end

#true_positive_rateObject



61
62
63
# File 'lib/adaboost/contingency_table.rb', line 61

def true_positive_rate
  true_positive / condition_positive.to_f
end