pmmlruntime 0.1.0

PMML 4.4 runtime — session-based execution, Arrow batch, hardened XML
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! GeneralRegressionModel evaluation — PPMatrix / ParamMatrix with logistic softmax.
//!
//! Implements `GeneralRegressionModel` (GLM / multinomial logistic). For each
//! `Parameter`, its `PPCell`s define a predictor `x` as product of factor contrasts
//! (`Factor` categorical, per-category contrast matrix) and covariate values. `eta`
//! per `targetCategory` is `Σ beta * x` from `ParamMatrix`; probabilities are
//! `softmax(eta)` with the reference category `eta = 0` (`exp(0) = 1`). The
//! category with maximal probability is predicted. Ties to `TargetIr`/`Output` layers
//! for display.
//!
//! # What belongs here
//!
//! - [`evaluate_general_regression`] — predicted `Value` only.
//! - [`evaluate_general_regression_with_probs`] — predicted plus `HashMap<String,f64>` probabilities.
//!
//! # Performance
//!
//! `O(parameters * ppcells + param_matrix)` to compute `param_x` plus `O(targets)` for `softmax`.
//! Small hash maps; no per-row allocation beyond those maps.

use crate::base::Value;
use crate::ir::GeneralRegressionIr;
use std::collections::HashMap;

/// Evaluate a [`GeneralRegressionIr`] and return the predicted value.
///
/// Thin wrapper around [`evaluate_general_regression_with_probs`] that discards
/// the probability map. Predicts the `targetCategory` with maximal softmax probability
/// (`eta = Σ beta * x`, `p = exp(eta) / Σ exp(eta)` with reference `eta=0`).
///
/// # Parameters
///
/// - `gr`: Lowered general-regression model (`GeneralRegressionIr`) with `parameters`, `factors`, `covariates`,
///   `pp_matrix`, `param_matrix`, `target_reference_category`.
/// - `values`: Dense `&[Value]` indexed by [`FieldId`](crate::base::FieldId).
/// - `_field_names`: Unused (reserved for future factor aliasing); pass empty map.
/// - `symbol_names`: `SymbolId → string` for parsing discrete `Value`s as `f64` and for output keys.
/// - `name_to_id`: `predictorName → FieldId` for resolving `PPCell.predictorName`.
///
/// # Returns
///
/// `Discrete(predicted_category)` or `Missing` when no `ParamMatrix` entry wins.
///
/// # Panics
///
/// Never panics. All `FieldId` indexing is bounds-checked; missing inputs yield `x = 0` for that parameter.
///
/// # Performance
///
/// Same as [`evaluate_general_regression_with_probs`]: one `HashMap` traversal per parameter plus softmax.
///
/// # Examples
///
/// ```
/// use pmmlruntime::base::{FieldId, SymbolId, Value};
/// use pmmlruntime::ir::*;
/// use std::collections::HashMap;
/// use pmmlruntime::engine::models::general_regression::evaluate_general_regression;
///
/// let fid = FieldId(0);
/// let s_low = SymbolId(1);
/// let s_high = SymbolId(2);
/// let mut name_to_id = HashMap::new(); name_to_id.insert("x".into(), fid);
/// let mut symbol_names = HashMap::new(); symbol_names.insert(s_low, "Low".into()); symbol_names.insert(s_high, "High".into());
/// let gr = GeneralRegressionIr {
///     function_name: "classification".into(),
///     mining_schema: MiningSchemaIr { active_fields: vec![fid], target_field: None, field_metas: vec![], missing_value_replacement: None },
///     output: vec![], model_type: Some("multinomialLogistic".into()), target_variable_name: None, target_reference_category: Some(s_high),
///     parameters: vec![ParameterIr { name: "p0".into(), label: None }],
///     factors: vec![], covariates: vec![fid],
///     pp_matrix: vec![PPCellIr { value: SymbolId(0), predictor_name: "x".into(), parameter_name: "p0".into() }],
///     param_matrix: vec![PCellIr { target_category: Some(s_low), parameter_name: "p0".into(), beta: 0.5 }],
/// };
/// let pred = evaluate_general_regression(&gr, &[Value::Continuous(2.0)], &HashMap::new(), &symbol_names, &name_to_id);
/// assert!(matches!(pred, Value::Discrete(_)));
/// ```
pub fn evaluate_general_regression(
    gr: &GeneralRegressionIr,
    values: &[Value],
    _field_names: &HashMap<crate::base::FieldId, String>,
    symbol_names: &HashMap<crate::base::SymbolId, String>,
    name_to_id: &HashMap<String, crate::base::FieldId>,
) -> Value {
    let (pred, _probs) =
        evaluate_general_regression_with_probs(gr, values, _field_names, symbol_names, name_to_id);
    pred
}

/// Evaluate a [`GeneralRegressionIr`] and return the predicted value plus class probabilities.
///
/// Builds `param_x: parameterName → x` (product of contrasts/covariates per `PPMatrix`), then
/// `eta_target = Σ beta * x` per `PCell.target_category` (reference category `eta = 0`). Probabilities
/// are `softmax` (`exp(eta)/Σ exp(eta)`) and the maximal category is returned. Missing inputs produce
/// `x = 0` for that parameter. Factor contrasts are looked up via `factor.matrix[row][col]` where
/// `row` is the input category's index and `col` is the `PPCell.value` category's column.
///
/// # Parameters
///
/// Same as [`evaluate_general_regression`]: `gr`, `values`, `_field_names`, `symbol_names`, `name_to_id`.
///
/// # Returns
///
/// `(predicted, probs)` where `predicted: Value::Discrete` is the winning category and
/// `probs: HashMap<category_string, f64>` covers all distinct `target_category` values plus the reference.
/// `probs` is empty only when `param_matrix` is empty.
///
/// # Panics
///
/// Never panics. All indexing is bounds-checked; `softmax` uses `exp` with IEEE 754 handling.
///
/// # Performance
///
/// `O(parameters * ppcells + param_matrix + targets)` with two hash maps (`param_x`, `target_etas`).
///
/// # Examples
///
/// ```
/// use pmmlruntime::base::{FieldId, SymbolId, Value};
/// use pmmlruntime::ir::*;
/// use std::collections::HashMap;
/// use pmmlruntime::engine::models::general_regression::evaluate_general_regression_with_probs;
///
/// let fid = FieldId(0);
/// let s_low = SymbolId(1);
/// let s_high = SymbolId(2);
/// let mut name_to_id = HashMap::new(); name_to_id.insert("x".into(), fid);
/// let mut symbol_names = HashMap::new(); symbol_names.insert(s_low, "Low".into()); symbol_names.insert(s_high, "High".into());
/// let gr = GeneralRegressionIr {
///     function_name: "classification".into(),
///     mining_schema: MiningSchemaIr { active_fields: vec![fid], target_field: None, field_metas: vec![], missing_value_replacement: None },
///     output: vec![], model_type: Some("multinomialLogistic".into()), target_variable_name: None, target_reference_category: Some(s_high),
///     parameters: vec![ParameterIr { name: "p0".into(), label: None }],
///     factors: vec![], covariates: vec![fid],
///     pp_matrix: vec![PPCellIr { value: SymbolId(0), predictor_name: "x".into(), parameter_name: "p0".into() }],
///     param_matrix: vec![PCellIr { target_category: Some(s_low), parameter_name: "p0".into(), beta: 0.0 }],
/// };
/// let (pred, probs) = evaluate_general_regression_with_probs(&gr, &[Value::Continuous(1.0)], &HashMap::new(), &symbol_names, &name_to_id);
/// assert!(probs.contains_key("Low"));
/// assert!(probs.contains_key("High"));
/// ```
pub fn evaluate_general_regression_with_probs(
    gr: &GeneralRegressionIr,
    values: &[Value],
    _field_names: &HashMap<crate::base::FieldId, String>,
    symbol_names: &HashMap<crate::base::SymbolId, String>,
    name_to_id: &HashMap<String, crate::base::FieldId>,
) -> (Value, HashMap<String, f64>) {
    // Build map from parameterName to list of PPCells
    let mut param_to_ppcells: HashMap<String, Vec<&crate::ir::PPCellIr>> = HashMap::new();
    for ppc in &gr.pp_matrix {
        param_to_ppcells
            .entry(ppc.parameter_name.clone())
            .or_default()
            .push(ppc);
    }

    // Helper to get FieldId for predictorName
    let get_fid =
        |pred_name: &str| -> Option<crate::base::FieldId> { name_to_id.get(pred_name).copied() };

    // For each parameter, compute x
    let mut param_x: HashMap<String, f64> = HashMap::new();
    for param in &gr.parameters {
        let ppcells = param_to_ppcells.get(&param.name);
        if ppcells.is_none() || ppcells.unwrap().is_empty() {
            // Constant term
            param_x.insert(param.name.clone(), 1.0);
            continue;
        }
        let mut x: f64 = 1.0;
        let mut missing = false;
        for ppc in ppcells.unwrap() {
            let pred_name = &ppc.predictor_name;
            if let Some(fid) = get_fid(pred_name) {
                let idx = fid.as_usize();
                let val = if idx < values.len() {
                    values[idx]
                } else {
                    Value::Missing
                };
                if val.is_missing() {
                    missing = true;
                    break;
                }
                // Check if this predictor is a Factor
                if let Some(factor) = gr.factors.iter().find(|f| f.name == fid) {
                    // Factor: value is SymbolId, need to find column for this parameter
                    // PPCell value indicates which category's column? For factor, column is index of PPCell value in factor categories
                    let ppc_value_sid = ppc.value;
                    let col_idx_opt = factor
                        .categories
                        .iter()
                        .position(|&cat| cat == ppc_value_sid);
                    if let Some(col_idx) = col_idx_opt {
                        // Find input category row
                        if let Value::Discrete(input_sid) = val {
                            if let Some(row_idx) =
                                factor.categories.iter().position(|&cat| cat == input_sid)
                            {
                                if row_idx < factor.matrix.len()
                                    && col_idx < factor.matrix[row_idx].len()
                                {
                                    let contrast = factor.matrix[row_idx][col_idx];
                                    x *= contrast;
                                } else {
                                    // Fallback: if matrix not available or out of bounds, use 0 or 1?
                                    // For Simple with 1 column, we have matrix row for each category
                                    // If col out of bounds, treat as 0
                                    x = 0.0;
                                    missing = true;
                                    break;
                                }
                            } else {
                                // Input category not found, treat as 0
                                x = 0.0;
                                missing = true;
                                break;
                            }
                        } else {
                            // Factor input should be discrete
                            x = 0.0;
                            missing = true;
                            break;
                        }
                    } else {
                        // PPCell value not found in factor categories, maybe value is "1" for covariate? But this is factor, so should be found
                        // If not found, try to treat as covariate?
                        // For safety, set x=0
                        x = 0.0;
                        missing = true;
                        break;
                    }
                } else if gr.covariates.contains(&fid) {
                    // Covariate: x = input continuous value (ignore PPCell value "1")
                    match val {
                        Value::Continuous(f) => x *= f,
                        Value::Discrete(sid) => {
                            if let Some(s) = symbol_names.get(&sid) {
                                if let Ok(f) = s.parse::<f64>() {
                                    x *= f;
                                } else {
                                    x = 0.0;
                                    missing = true;
                                    break;
                                }
                            } else {
                                x = 0.0;
                                missing = true;
                                break;
                            }
                        }
                        Value::Missing => {
                            missing = true;
                            break;
                        }
                    }
                } else {
                    // Unknown predictor type, treat as continuous if possible
                    match val {
                        Value::Continuous(f) => x *= f,
                        Value::Discrete(sid) => {
                            if let Some(s) = symbol_names.get(&sid) {
                                if let Ok(f) = s.parse::<f64>() {
                                    x *= f;
                                } else {
                                    // For factor without matrix, treat as indicator: 1 if matches PPCell value else 0
                                    let ppc_sid = ppc.value;
                                    if sid == ppc_sid {
                                        x *= 1.0;
                                    } else {
                                        x *= 0.0;
                                    }
                                }
                            } else {
                                x = 0.0;
                                missing = true;
                                break;
                            }
                        }
                        Value::Missing => {
                            missing = true;
                            break;
                        }
                    }
                }
            } else {
                // Predictor not found, treat as missing
                missing = true;
                break;
            }
        }
        if missing {
            x = 0.0;
        }
        param_x.insert(param.name.clone(), x);
    }

    // Now compute eta per target category
    // Collect distinct target categories from param_matrix plus reference
    let mut target_etas: HashMap<Option<crate::base::SymbolId>, f64> = HashMap::new();
    // For each distinct target in param_matrix
    for pcell in &gr.param_matrix {
        let cat = pcell.target_category;
        target_etas.entry(cat).or_insert(0.0);
    }
    // Also ensure reference category is included with eta 0
    if let Some(ref_cat) = gr.target_reference_category {
        target_etas.entry(Some(ref_cat)).or_insert(0.0);
    } else {
        // If no reference, we still need to ensure at least one target
        // If param_matrix has only Low, reference is High implicitly
        // We already have Low, so High will be reference with 0
        // But we don't know High's SymbolId. We can infer from DataDictionary or from target categories not in param_matrix?
        // For now, if target_reference_category is None but we have only Low, we will add a synthetic reference with None
        if !target_etas.contains_key(&None) {
            // Check if there's a category not in param_matrix that is the reference
            // For fixture, salCat has categories Low and High, with High as reference, but param_matrix only has Low
            // So we need to add None as reference
            target_etas.entry(None).or_insert(0.0);
        }
    }

    // Compute eta for each target
    for pcell in &gr.param_matrix {
        let cat = pcell.target_category;
        let beta = pcell.beta;
        let param_name = &pcell.parameter_name;
        if let Some(&x) = param_x.get(param_name) {
            if let Some(eta) = target_etas.get_mut(&cat) {
                *eta += beta * x;
            }
        }
    }

    // Now compute probabilities via softmax (multinomialLogistic)
    // For binary with reference, p_target = exp(eta_target) / (1 + sum exp(eta_other))
    // For n categories, p = exp(eta) / sum exp(eta) where reference eta=0 => exp(0)=1
    let mut exp_etas: HashMap<Option<crate::base::SymbolId>, f64> = HashMap::new();
    let mut sum_exp = 0.0;
    for (cat, eta) in &target_etas {
        let exp_eta = eta.exp();
        exp_etas.insert(*cat, exp_eta);
        sum_exp += exp_eta;
    }

    // If reference was None, we need to handle probabilities for actual categories
    // For fixture, target categories are Low and High (reference). We have etas for Low and for None (reference High with 0)
    // So sum_exp = exp(eta_low) + 1
    // p_low = exp(eta_low)/sum, p_high = 1/sum
    let mut probs: HashMap<String, f64> = HashMap::new();
    let mut best_cat: Option<crate::base::SymbolId> = None;
    let mut best_prob = f64::NEG_INFINITY;

    // Need to map SymbolId to string for output
    // For each target, compute prob and find best
    for (cat_opt, exp_eta) in &exp_etas {
        let prob = exp_eta / sum_exp;
        if let Some(cat_sid) = cat_opt {
            if let Some(cat_str) = symbol_names.get(cat_sid) {
                probs.insert(cat_str.clone(), prob);
                if prob > best_prob {
                    best_prob = prob;
                    best_cat = Some(*cat_sid);
                }
            }
        } else {
            // Reference category with None: need to find its string via target_reference_category
            if let Some(ref_sid) = gr.target_reference_category {
                if let Some(cat_str) = symbol_names.get(&ref_sid) {
                    probs.insert(cat_str.clone(), prob);
                    if prob > best_prob {
                        best_prob = prob;
                        best_cat = Some(ref_sid);
                    }
                }
            } else {
                // Try to infer reference category string as "High" for fixture?
                // Look for category not in param_matrix but in DataDictionary for salCat
                //  we can try to find symbol for High via searching symbol_names for "High"
                if let Some((sid, _)) = symbol_names.iter().find(|(_, s)| *s == "High") {
                    probs.insert("High".to_string(), prob);
                    if prob > best_prob {
                        best_prob = prob;
                        best_cat = Some(*sid);
                    }
                } else {
                    // Fallback: use "reference"
                    probs.insert("reference".to_string(), prob);
                }
            }
        }
    }

    // Also handle case where reference was None but we inserted None entry: need to ensure High prob is correctly mapped
    // For fixture, we inserted None with exp 1, but we mapped it to High via symbol search above, so it's ok

    let predicted = if let Some(cat) = best_cat {
        Value::Discrete(cat)
    } else {
        // Fallback to first param_matrix category
        if let Some(first) = gr.param_matrix.first() {
            if let Some(cat) = first.target_category {
                Value::Discrete(cat)
            } else {
                Value::Missing
            }
        } else {
            Value::Missing
        }
    };

    (predicted, probs)
}