File size: 2,863 Bytes
71d3a93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f766a1
71d3a93
6f766a1
71d3a93
6f766a1
 
 
 
 
 
 
 
 
71d3a93
6f766a1
71d3a93
6f766a1
71d3a93
 
6f766a1
 
 
71d3a93
 
6f766a1
 
 
 
 
 
 
 
 
 
 
 
 
 
71d3a93
 
 
 
 
 
 
 
 
6f766a1
 
71d3a93
 
6f766a1
 
 
 
 
 
 
 
71d3a93
 
 
 
 
 
 
6f766a1
71d3a93
 
 
 
 
 
 
 
 
 
 
 
 
6f766a1
 
71d3a93
 
 
 
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
---
license: other
library_name: scikit-learn
pipeline_tag: tabular-classification
tags:
  - cybersecurity
  - vulnerability-detection
  - defect-prediction
  - random-forest
  - scikit-learn
  - joblib
  - nasa-promise
---

# Agent 1 — Structural Risk Random Forest

Random Forest that estimates defect risk from 21 NASA/PROMISE code metrics.
Part of a three-agent DevSecOps auditor (metrics → transformer → gatekeeper).

## Model details

| Field | Value |
|---|---|
| Version | 1.0.0 (initial release) |
| Artifact | `model.joblib` (bundle: `model`, `raw_cols`, `feature_cols`, `threshold`) |
| Task | Binary tabular classification (defect / no defect) |
| Threshold | **0.3183** (tuned on training CV with F2) |
| Training data | NASA/PROMISE PC1 (`pc1.csv`) |
| Dataset version | Not pinned — static PC1 snapshot, no revision hash recorded |
| Seed | 42 |

`metadata.json` mirrors the dataset, threshold, and best params.

## Input features

21 numeric metrics:

```
loc, vg, evg, ivg, n, v, l, d, i, e, b, t,
locode, locomment, loccodeandcomment, loblank,
uniq_op, uniq_opnd, total_op, total_opnd, branchcount
```

Five derived features are added before prediction. Final `feature_cols` order
(26 columns, must match exactly):

```
loc, vg, evg, ivg, n, v, l, d, i, e, b, t,
locode, locomment, loccodeandcomment, loblank,
uniq_op, uniq_opnd, total_op, total_opnd, branchcount,
complexity_density, effort_per_loc, comment_ratio, branch_density, operand_reuse
```

## Preprocessing

- All 21 inputs must be numeric; no missing values (nothing is imputed).
- Derive ratios with `+1` denominators:

```
complexity_density = vg / (loc + 1)
effort_per_loc     = e / (loc + 1)
comment_ratio      = locomment / (loc + 1)
branch_density     = branchcount / (loc + 1)
operand_reuse      = total_opnd / (uniq_opnd + 1)
```

- Select `b["feature_cols"]` in the order above.

## Output

Raw: `predict_proba(X)[0, 1]` — defect risk in `[0, 1]`, uncalibrated.
`high_risk = score >= 0.3183`.

Reference wrapper output:

```json
{"struct_risk_score": 0.5105, "high_risk": true, "threshold": 0.3183, "status": "ok", "reason": ""}
```

## Usage

```python
import joblib
import pandas as pd

b = joblib.load("model.joblib")
df = pd.DataFrame([metrics])[b["raw_cols"]].apply(pd.to_numeric)

df["complexity_density"] = df["vg"] / (df["loc"] + 1)
df["effort_per_loc"]     = df["e"] / (df["loc"] + 1)
df["comment_ratio"]      = df["locomment"] / (df["loc"] + 1)
df["branch_density"]     = df["branchcount"] / (df["loc"] + 1)
df["operand_reuse"]      = df["total_opnd"] / (df["uniq_opnd"] + 1)

X = df[b["feature_cols"]]
score = b["model"].predict_proba(X)[0, 1]
high_risk = score >= b["threshold"]
```

Trained on NASA PROMISE PC1. Metrics from other languages or domains are
out-of-distribution.

## License

Not specified. Update the `license` field before publishing.