use crate::base::Value;
use crate::ir::{RegressionIr, RegressionNormalizationMethod};
pub fn evaluate_regression(reg: &RegressionIr, values: &[Value]) -> Value {
if reg.regression_tables.is_empty() {
return Value::Missing;
}
if reg.regression_tables.len() == 1 {
let tbl = ®.regression_tables[0];
let mut sum = tbl.intercept;
for np in &tbl.numeric_predictors {
let idx = np.field.as_usize();
let actual = if idx < values.len() {
values[idx]
} else {
Value::Missing
};
if let Value::Continuous(v) = actual {
let powered = v.powi(np.exponent);
sum += np.coefficient * powered;
} else if actual.is_missing() {
continue;
}
}
for cp in &tbl.categorical_predictors {
let idx = cp.field.as_usize();
let actual = if idx < values.len() {
values[idx]
} else {
Value::Missing
};
let matches = match actual {
Value::Discrete(sid) => sid == cp.value,
_ => false,
};
if matches {
sum += cp.coefficient;
}
}
let normalized = apply_normalization(sum, reg.normalization_method);
return Value::Continuous(normalized);
}
let mut best_score = f64::NEG_INFINITY;
let mut best_category: Option<crate::base::SymbolId> = None;
for tbl in ®.regression_tables {
let mut sum = tbl.intercept;
for np in &tbl.numeric_predictors {
let idx = np.field.as_usize();
let actual = if idx < values.len() {
values[idx]
} else {
Value::Missing
};
if let Value::Continuous(v) = actual {
sum += np.coefficient * v.powi(np.exponent);
}
}
for cp in &tbl.categorical_predictors {
let idx = cp.field.as_usize();
let actual = if idx < values.len() {
values[idx]
} else {
Value::Missing
};
if let Value::Discrete(sid) = actual {
if sid == cp.value {
sum += cp.coefficient;
}
}
}
let norm = apply_normalization(sum, reg.normalization_method);
if norm > best_score {
best_score = norm;
best_category = tbl.target_category;
}
}
if let Some(cat) = best_category {
Value::Discrete(cat)
} else {
Value::Missing
}
}
pub(crate) fn apply_normalization(score: f64, method: RegressionNormalizationMethod) -> f64 {
match method {
RegressionNormalizationMethod::None => score,
RegressionNormalizationMethod::Softmax => 1.0 / (1.0 + (-score).exp()), RegressionNormalizationMethod::Logit => 1.0 / (1.0 + (-score).exp()),
RegressionNormalizationMethod::Exp => score.exp(),
RegressionNormalizationMethod::SimpleMax => score, RegressionNormalizationMethod::Probit => {
0.5 * (1.0 + libm::erf(score / std::f64::consts::SQRT_2))
}
_ => score,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::base::FieldId;
use crate::ir::*;
#[test]
fn single_table_regression() {
let f_input = FieldId(0);
let reg = RegressionIr {
function_name: "regression".into(),
mining_schema: MiningSchemaIr {
active_fields: vec![f_input],
target_field: None,
field_metas: vec![],
missing_value_replacement: None,
},
regression_tables: vec![RegressionTableIr {
intercept: 0.0,
target_category: None,
numeric_predictors: vec![NumericPredictorIr {
field: f_input,
coefficient: 2.0,
exponent: 1,
}],
categorical_predictors: vec![],
}],
normalization_method: RegressionNormalizationMethod::None,
targets: vec![],
output: vec![],
};
let vals = vec![Value::Continuous(2.5)];
assert_eq!(evaluate_regression(®, &vals), Value::Continuous(5.0));
}
}