File size: 4,222 Bytes
484e3bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Risk scoring using gradient boosting and ensemble methods.
"""

import numpy as np
import pandas as pd
from typing import Dict, List, Optional, Any, Tuple
from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier
from sklearn.model_selection import cross_val_score


class RiskScorer:
    """
    Nonlinear risk scoring using ensemble methods.

    Uses Gradient Boosting and Random Forests to discover nonlinear
    patterns in geopolitical risk factors.
    """

    def __init__(self, method: str = 'gradient_boosting'):
        """
        Initialize risk scorer.

        Parameters
        ----------
        method : str
            Method to use ('gradient_boosting', 'random_forest', 'ensemble')
        """
        self.method = method
        self.model = None
        self.feature_names = None
        self.is_trained = False

    def train(
        self,
        X: pd.DataFrame,
        y: np.ndarray,
        n_estimators: int = 100,
        max_depth: int = 5
    ) -> Dict[str, Any]:
        """
        Train risk scoring model.

        Parameters
        ----------
        X : pd.DataFrame
            Feature matrix
        y : np.ndarray
            Risk labels (0 = low risk, 1 = high risk)
        n_estimators : int
            Number of estimators
        max_depth : int
            Maximum tree depth

        Returns
        -------
        dict
            Training results
        """
        self.feature_names = X.columns.tolist()

        if self.method == 'gradient_boosting':
            self.model = GradientBoostingClassifier(
                n_estimators=n_estimators,
                max_depth=max_depth,
                random_state=42
            )
        elif self.method == 'random_forest':
            self.model = RandomForestClassifier(
                n_estimators=n_estimators,
                max_depth=max_depth,
                random_state=42
            )
        else:
            raise ValueError(f"Unknown method: {self.method}")

        # Train
        self.model.fit(X, y)
        self.is_trained = True

        # Cross-validation score
        cv_scores = cross_val_score(self.model, X, y, cv=5)

        return {
            'cv_mean': cv_scores.mean(),
            'cv_std': cv_scores.std(),
            'feature_importance': self.get_feature_importance()
        }

    def predict_risk(self, X: pd.DataFrame) -> np.ndarray:
        """
        Predict risk scores.

        Parameters
        ----------
        X : pd.DataFrame
            Features

        Returns
        -------
        np.ndarray
            Risk probabilities
        """
        if not self.is_trained:
            raise ValueError("Model not trained yet")

        return self.model.predict_proba(X)[:, 1]

    def get_feature_importance(self) -> Dict[str, float]:
        """
        Get feature importance scores.

        Returns
        -------
        dict
            Feature importance
        """
        if not self.is_trained:
            raise ValueError("Model not trained yet")

        importance = self.model.feature_importances_
        return dict(zip(self.feature_names, importance))

    def explain_prediction(self, X: pd.DataFrame, index: int) -> Dict[str, Any]:
        """
        Explain a specific prediction.

        Parameters
        ----------
        X : pd.DataFrame
            Features
        index : int
            Index of sample to explain

        Returns
        -------
        dict
            Explanation
        """
        if not self.is_trained:
            raise ValueError("Model not trained yet")

        sample = X.iloc[index:index+1]
        risk_score = self.predict_risk(sample)[0]

        # Feature contributions (simplified)
        feature_values = sample.iloc[0].to_dict()
        feature_importance = self.get_feature_importance()

        contributions = {
            feat: feature_values[feat] * feature_importance[feat]
            for feat in feature_values.keys()
        }

        return {
            'risk_score': risk_score,
            'top_risk_factors': sorted(contributions.items(), key=lambda x: x[1], reverse=True)[:5],
            'feature_values': feature_values
        }