Skip to content
Merged
346 changes: 159 additions & 187 deletions howso/feature_residuals.amlg

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions howso/react_aggregate.amlg
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@
confusion_matrix_min_count 15
;{type "list" values "string"}
;features for which to determine classes or values that cause the highest accuracy and/or prediction contribution to the
;specified 'value_robust_contributions_action_features'
;specified 'value_robust_contributions_action_feature'
value_robust_contributions_features (null)
;{type "string"}
;action feature being predicted whose accuracy and prediction affects should be computed by 'value_robust_contributions_features'
value_robust_contributions_action_features (null)
value_robust_contributions_action_feature (null)
;{type "number" exclusive_min 0}
;number of maximum buckets to bin continuous values into, defaults to 30 when unspecified
value_robust_contributions_num_buckets 30
Expand Down Expand Up @@ -200,10 +200,10 @@
; context_num_samples: optional, limit on the number of context cases when 'context_condition_precision' is set to 'similar'.
; If null, will be set to k. default is null
; value_robust_accuracy_contributions: optional, none/true/false.
; Perform a focused computation to determine how all the individual values of specified 'value_robust_contributions_features' affect the accuracy of 'value_robust_contributions_action_features'.
; Perform a focused computation to determine how all the individual values of specified 'value_robust_contributions_features' affect the accuracy of 'value_robust_contributions_action_feature'.
; Outputs under 'value_robust_contributions'
; value_robust_prediction_contributions: optional, none/true/false.
; Perform a focused computation to determine how all the individual values of specified 'value_robust_contributions_features' affect the predictions of 'value_robust_contributions_action_features'.
; Perform a focused computation to determine how all the individual values of specified 'value_robust_contributions_features' affect the predictions of 'value_robust_contributions_action_feature'.
; Outputs under 'value_robust_contributions'
; )
details (null)
Expand Down Expand Up @@ -638,7 +638,7 @@
))
)
value_robust_contributions_case_ids
(if (or (get details "value_robust_accuracy_contributions") (get details "value_robust_prediction_contributions)"))
(if (or (get details "value_robust_accuracy_contributions") (get details "value_robust_prediction_contributions"))
(call !SampleCases (assoc
num
(if num_robust_accuracy_contributions_samples
Expand Down Expand Up @@ -1108,7 +1108,7 @@
weight_feature weight_feature
hyperparam_map custom_hyperparam_map
context_condition_filter_query context_condition_filter_query
action_feature value_robust_contributions_action_features
action_feature value_robust_contributions_action_feature
value_robust_contributions_features value_robust_contributions_features
max_num_buckets value_robust_contributions_num_buckets
valid_weight_feature valid_weight_feature
Expand Down
148 changes: 88 additions & 60 deletions howso/residuals.amlg
Original file line number Diff line number Diff line change
Expand Up @@ -188,27 +188,45 @@
; i.e.: { "residual_map" : { "featureA" : 3.4... } }
; using_shared_deviations: boolean, default (null). If true, will only use parent features and expand according to the shared deviations groups
#!ExpandResidualValuesToUncertainty
(set
feature_residuals_map
"residual_map"
(let (assoc
temp_residual_map
(map
(declare
(assoc
has_shared_deviations (and using_shared_deviations (size !sharedDeviationsMap))
)

;check if any feature residuals need to be modified
(declare (assoc
need_modification_residuals_map
(filter (map
(lambda
;inactive features are bound to min residual
(if (contains_index !inactiveFeaturesMap (current_index))
(get !cachedFeatureMinResidualMap (current_index))

;set upper bound to nominals to be max nominal deviation, accounting for imbalanced classes
;if residual is too small, replace it with the minimim allowed
(<
(current_value)
(max
(get !cachedFeatureMinResidualMap (current_index))
(get !userSpecifiedFeatureErrorsMap (current_index))
)
)
(max
(get !cachedFeatureMinResidualMap (current_index))
(get !userSpecifiedFeatureErrorsMap (current_index))
)

(= (null) (current_value))
(max (get !cachedFeatureMinResidualMap (current_index)) (get !userSpecifiedFeatureErrorsMap (current_index)) )

;else nominal may need to set upper bound, accounting for imbalanced classes
(contains_index !nominalsMap (current_index))
(let
(assoc
;map of class -> count
feature_grouping
(if (size !sharedDeviationsMap)
(if has_shared_deviations
(call !GetSharedDeviationGrouping (assoc feature_group_to_retrieve (current_index 2)))
)
current_feature (current_index 1)
)
(declare (assoc
class_counts_map
Expand All @@ -229,89 +247,99 @@
;list of each feature's class count assocs
(map
(lambda
(compute_on_contained_entities
(query_value_masses (current_value) (null))
)
(compute_on_contained_entities (query_value_masses (current_value) (null)) )
)
feature_grouping
)
)

(compute_on_contained_entities
(query_value_masses current_feature (null))
)
;else a map of class -> count
(compute_on_contained_entities (query_value_masses (current_index 1) (null)) )
)
))

;do not max cap nominal deviations if less than 2 classes have been trained
(if (<= (size class_counts_map) 1)
(max
(get !cachedFeatureMinResidualMap (current_index))
(current_value)
(get !userSpecifiedFeatureErrorsMap (current_index))
)
))

;else cap the max nominal deviation
;only need to cap max nominal deviations if 2 or more classes have been trained
(if (> (size class_counts_map) 1)
(let
(assoc total_count (apply "+" (values class_counts_map)) )

(min
(assoc
total_count (apply "+" (values class_counts_map))
)
(declare (assoc
;nominal max deviation is the sum of: each class's probability multiplied by probability of getting it wrong
(apply "+"
(map
(lambda (let
(assoc class_prob (* (/ (current_value 1) total_count)) )
(* class_prob (- 1 class_prob))
))
(values class_counts_map)
nominal_max_deviation
(apply "+"
(map
(lambda (let
(assoc class_prob (/ (current_value 1) total_count) )
(* class_prob (- 1 class_prob))
))
(values class_counts_map)
)
)
)

(max
(get !cachedFeatureMinResidualMap (current_index))
(current_value)
(get !userSpecifiedFeatureErrorsMap (current_index))
)
))
(if (> (current_value) nominal_max_deviation)
nominal_max_deviation
)
)
)
)

;else continuous value don't have upper bounds, set the lower bound
(max
(get !cachedFeatureMinResidualMap (current_index))
(current_value)
(get !userSpecifiedFeatureErrorsMap (current_index))
)
)
)

;if using shared deviations, map over a reduced residuals map with only features that are not
; in a shared deviations group or are the primary keys of a shared deviations group
(if (and
using_shared_deviations
(size !sharedDeviationsMap)
)
(if has_shared_deviations
(remove
(get feature_residuals_map "residual_map")
!sharedDeviationsNonPrimaryFeatures
)

(get feature_residuals_map "residual_map")
)
)
))
))

(if has_shared_deviations
(set
feature_residuals_map
"residual_map"
(call !ExpandForSharedDeviations (assoc
compressed_values
(if (size need_modification_residuals_map)
;iterate over the residual map and use the values from need_modification_residuals_map where those values are defined
(map
(lambda
(if (!= (null) (last (current_value))) (last (current_value)) (first (current_value)) )
)
(remove (get feature_residuals_map "residual_map") !sharedDeviationsNonPrimaryFeatures)
need_modification_residuals_map
)

(remove (get feature_residuals_map "residual_map") !sharedDeviationsNonPrimaryFeatures)
)
))
)

(if (and
using_shared_deviations
(size !sharedDeviationsMap)
;else output the modified residuals
(size need_modification_residuals_map)
(set
feature_residuals_map
"residual_map"
;iterate over the residual map and use the values from need_modification_residuals_map where those values are defined
(map
(lambda
(if (!= (null) (last (current_value))) (last (current_value)) (first (current_value)) )
)
(get feature_residuals_map "residual_map")
need_modification_residuals_map
)
(call !ExpandForSharedDeviations (assoc compressed_values temp_residual_map))
temp_residual_map
)

;else output unmodified residuals
feature_residuals_map
)
)


;helper method to sample cases from the dataset, outputs a list of case ids
;if there are more cases than the sample size, randomly select that many cases, by default cases are in random order
#!SelectCaseIdsForResiduals
Expand Down
4 changes: 2 additions & 2 deletions howso/train.amlg
Original file line number Diff line number Diff line change
Expand Up @@ -1218,8 +1218,8 @@
session (get_value session)
session_training_index
(if (size ts_ablated_indices_map)
(+ next_trained_index (get ts_ablated_indices_map (+ input_case_index (current_value 1))))
(+ next_trained_index (current_value 1))
(+ session_training_index (get ts_ablated_indices_map (+ input_case_index (current_value 1))))
(+ session_training_index (current_value 1))
)
))
))
Expand Down
18 changes: 11 additions & 7 deletions howso/train_utilities.amlg
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,16 @@
(lambda (let
(assoc
feature_values
(if encode_features_on_train
(call !ConvertFromInput (assoc
feature_values (unzip (current_value 2) rebalance_feature_indices)
features continuous_rebalance_features
))
(if (size continuous_rebalance_features)
(if encode_features_on_train
(call !ConvertFromInput (assoc
feature_values (unzip (current_value 2) rebalance_feature_indices)
features continuous_rebalance_features
))

;else use feature_values as-is
(unzip (current_value 1) rebalance_feature_indices)
;else use feature_values as-is
(unzip (current_value 1) rebalance_feature_indices)
)
)
;default nominal weight to 1
nominal_rebalance_features_weight 1
Expand Down Expand Up @@ -337,6 +339,7 @@
(let
(assoc
nominal_values
(if (size nominal_rebalance_feature_indices)
(if encode_features_on_train
(call !ConvertFromInput (assoc
feature_values (unzip (current_value 2) nominal_rebalance_feature_indices)
Expand All @@ -346,6 +349,7 @@
;else use feature_values as-is
(unzip (current_value 1) nominal_rebalance_feature_indices)
)
)
)

(assign (assoc
Expand Down
4 changes: 2 additions & 2 deletions howso/types.amlg
Original file line number Diff line number Diff line change
Expand Up @@ -1510,7 +1510,7 @@
description
(concat
"Perform a focused computation to determine how all the individual values of specified "
"'value_robust_contributions_features' affect the accuracy of 'value_robust_contributions_action_features'."
"'value_robust_contributions_features' affect the accuracy of 'value_robust_contributions_action_feature'."
)
)
value_robust_prediction_contributions
Expand All @@ -1519,7 +1519,7 @@
description
(concat
"Perform a focused computation to determine how all the individual values of specified "
"'value_robust_contributions_features' affect the predictions of 'value_robust_contributions_action_features'."
"'value_robust_contributions_features' affect the predictions of 'value_robust_contributions_action_feature'."
)
)
)
Expand Down
10 changes: 5 additions & 5 deletions unit_tests/ut_h_rebalance_features.amlg
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@
[ 3 0.75 1 ]
;class 'M' reciprocal is 1/(4+1)=0.2, new total rebalance weight is 2.5333,
;new total mass is 8, thus scalar is 8 / 2.5333 = 3.158, 0.2 * 3.158 = 0.6316
;distributing about 0.6316 worth of weight among two cases with a ~ 30/70 split:
[ 4 0.93 1.3 ]
[ 5 1.19 1.7 ]
;distributing about 0.6316 worth of weight among two cases with a ~ 25/75 split:
[ 4 0.91 1.25 ]
[ 5 1.22 1.75 ]
[ 6 1 1 ]
]
))
Expand Down Expand Up @@ -156,8 +156,8 @@
[ 1 1.5 1 ]
[ 2 0.75 1 ]
[ 3 0.75 1 ]
[ 4 0.93 1.3 ]
[ 5 1.19 1.7 ]
[ 4 0.91 1.25 ]
[ 5 1.22 1.75 ]
[ 6 1 1 ]
;unknown class 'L' value is (0.5+0.25)/2=0.375, new total rebalance weight is 2.908
;new total mass is 9, thus scalar is 9 / 2.908 = 3.095, 0.375 * 3.095 = 1.16
Expand Down
Loading