File size: 14,106 Bytes
6835659
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
MSCI Sensitivity Analysis

Tests whether MSCI is sensitive to controlled semantic perturbations.
This addresses RQ1: "Is MSCI sensitive to controlled semantic perturbations?"

Key tests:
- Perturbation gradient: 0%, 25%, 50%, 75%, 100% semantic mismatch
- Expected: monotonic MSCI decrease with increasing perturbation
- If not monotonic: MSCI may be unreliable
"""

from __future__ import annotations

import json
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
import numpy as np
from scipy import stats


@dataclass
class PerturbationLevel:
    """A single perturbation level result."""
    level: float  # 0.0 = baseline, 1.0 = complete mismatch
    label: str
    msci_scores: List[float] = field(default_factory=list)
    n_samples: int = 0

    @property
    def mean_msci(self) -> float:
        """Mean MSCI at this perturbation level."""
        return np.mean(self.msci_scores) if self.msci_scores else 0.0

    @property
    def std_msci(self) -> float:
        """Standard deviation of MSCI."""
        return np.std(self.msci_scores, ddof=1) if len(self.msci_scores) > 1 else 0.0

    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary."""
        return {
            "level": self.level,
            "label": self.label,
            "n_samples": len(self.msci_scores),
            "mean_msci": self.mean_msci,
            "std_msci": self.std_msci,
            "min_msci": min(self.msci_scores) if self.msci_scores else None,
            "max_msci": max(self.msci_scores) if self.msci_scores else None,
        }


@dataclass
class PerturbationGradient:
    """Results from a perturbation gradient analysis."""
    levels: List[PerturbationLevel]
    is_monotonic: bool
    spearman_correlation: float
    spearman_p: float
    linear_slope: float
    r_squared: float
    sensitivity_score: float  # 0-1, how well MSCI tracks perturbation

    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary."""
        return {
            "levels": [l.to_dict() for l in self.levels],
            "is_monotonic": self.is_monotonic,
            "spearman_correlation": self.spearman_correlation,
            "spearman_p": self.spearman_p,
            "linear_slope": self.linear_slope,
            "r_squared": self.r_squared,
            "sensitivity_score": self.sensitivity_score,
        }


class MSCISensitivityAnalyzer:
    """
    Analyzes MSCI sensitivity to semantic perturbations.

    RQ1: "Is MSCI sensitive to controlled semantic perturbations?"
    H0: MSCI(baseline) = MSCI(perturbed)
    H1: MSCI(baseline) > MSCI(perturbed)
    """

    def __init__(self):
        self.results: Dict[str, Any] = {}

    def analyze_perturbation_gradient(
        self,
        baseline_scores: List[float],
        perturbed_scores_by_level: Dict[float, List[float]],
    ) -> PerturbationGradient:
        """
        Analyze MSCI response to perturbation gradient.

        Args:
            baseline_scores: MSCI scores for unperturbed samples
            perturbed_scores_by_level: Dict mapping perturbation level (0-1) to MSCI scores

        Returns:
            PerturbationGradient with analysis results
        """
        # Build perturbation levels
        levels = [
            PerturbationLevel(
                level=0.0,
                label="baseline",
                msci_scores=baseline_scores,
            )
        ]

        for level, scores in sorted(perturbed_scores_by_level.items()):
            levels.append(
                PerturbationLevel(
                    level=level,
                    label=f"{int(level * 100)}% perturbation",
                    msci_scores=scores,
                )
            )

        # Check monotonicity (MSCI should decrease as perturbation increases)
        means = [l.mean_msci for l in levels]
        is_monotonic = all(means[i] >= means[i + 1] for i in range(len(means) - 1))

        # Compute Spearman correlation between perturbation level and MSCI
        all_levels = []
        all_scores = []
        for level in levels:
            for score in level.msci_scores:
                all_levels.append(level.level)
                all_scores.append(score)

        if len(all_scores) >= 3:
            spearman_result = stats.spearmanr(all_levels, all_scores)
            spearman_rho = spearman_result.correlation
            spearman_p = spearman_result.pvalue

            # Linear regression for slope
            slope, intercept, r_value, p_value, std_err = stats.linregress(
                all_levels, all_scores
            )
            r_squared = r_value ** 2
        else:
            spearman_rho = 0.0
            spearman_p = 1.0
            slope = 0.0
            r_squared = 0.0

        # Sensitivity score: combination of correlation strength and monotonicity
        sensitivity = 0.0
        if spearman_rho < 0:  # Negative correlation expected
            sensitivity = abs(spearman_rho)
            if not is_monotonic:
                sensitivity *= 0.5  # Penalty for non-monotonicity

        return PerturbationGradient(
            levels=levels,
            is_monotonic=is_monotonic,
            spearman_correlation=spearman_rho,
            spearman_p=spearman_p,
            linear_slope=slope,
            r_squared=r_squared,
            sensitivity_score=sensitivity,
        )

    def paired_sensitivity_test(
        self,
        baseline_scores: List[float],
        perturbed_scores: List[float],
        alpha: float = 0.05,
    ) -> Dict[str, Any]:
        """
        Perform paired t-test for sensitivity.

        Tests H0: MSCI(baseline) = MSCI(perturbed)
        vs    H1: MSCI(baseline) > MSCI(perturbed)

        Args:
            baseline_scores: MSCI scores for baseline (same prompts)
            perturbed_scores: MSCI scores for perturbed (same prompts)
            alpha: Significance level

        Returns:
            Dictionary with test results
        """
        from src.experiments.statistical_analysis import paired_ttest, compute_effect_size

        if len(baseline_scores) != len(perturbed_scores):
            raise ValueError("Baseline and perturbed must have same length for paired test")

        # One-sided t-test (baseline > perturbed)
        result = paired_ttest(
            baseline_scores,
            perturbed_scores,
            alpha=alpha,
            alternative="greater",
        )

        # Effect size
        effect_size = compute_effect_size(baseline_scores, perturbed_scores, paired=True)

        # Descriptive stats
        baseline_mean = np.mean(baseline_scores)
        perturbed_mean = np.mean(perturbed_scores)
        mean_drop = baseline_mean - perturbed_mean
        percent_drop = (mean_drop / baseline_mean * 100) if baseline_mean > 0 else 0

        return {
            "test": "paired_t_test_one_sided",
            "hypothesis": "H1: MSCI(baseline) > MSCI(perturbed)",
            "n": len(baseline_scores),
            "baseline_mean": baseline_mean,
            "perturbed_mean": perturbed_mean,
            "mean_drop": mean_drop,
            "percent_drop": percent_drop,
            "t_statistic": result.statistic,
            "p_value": result.p_value,
            "effect_size_d": effect_size,
            "significant": result.significant,
            "interpretation": self._interpret_sensitivity(
                result.significant, effect_size, percent_drop
            ),
        }

    def _interpret_sensitivity(
        self,
        significant: bool,
        effect_size: float,
        percent_drop: float,
    ) -> str:
        """Generate interpretation of sensitivity test."""
        if not significant:
            return "MSCI is NOT significantly sensitive to this perturbation (H0 not rejected)"

        if effect_size > 0.8:
            strength = "highly"
        elif effect_size > 0.5:
            strength = "moderately"
        else:
            strength = "weakly"

        return (
            f"MSCI is {strength} sensitive to perturbation "
            f"(d={effect_size:.2f}, {percent_drop:.1f}% drop)"
        )

    def analyze_from_experiment_results(
        self,
        results_path: Path,
    ) -> Dict[str, Any]:
        """
        Analyze sensitivity from experiment results JSON.

        Args:
            results_path: Path to experiment_results.json

        Returns:
            Sensitivity analysis results
        """
        with Path(results_path).open("r", encoding="utf-8") as f:
            data = json.load(f)

        # Extract MSCI scores by condition
        raw_results = data.get("raw_results", [])
        scores_by_condition: Dict[str, List[float]] = {}

        for result in raw_results:
            if not result.get("success"):
                continue
            condition = result.get("condition", "")
            msci = result.get("scores", {}).get("msci")
            if msci is not None and condition:
                if condition not in scores_by_condition:
                    scores_by_condition[condition] = []
                scores_by_condition[condition].append(msci)

        # Analyze perturbations
        analyses = {}

        # Find baseline conditions
        baseline_conditions = [c for c in scores_by_condition if "baseline" in c]

        for baseline_cond in baseline_conditions:
            mode = baseline_cond.replace("_baseline", "")
            baseline_scores = scores_by_condition[baseline_cond]

            # Find corresponding perturbation conditions
            for cond, scores in scores_by_condition.items():
                if cond == baseline_cond:
                    continue
                if not cond.startswith(mode):
                    continue

                # Perform sensitivity test
                n = min(len(baseline_scores), len(scores))
                if n >= 3:
                    test_result = self.paired_sensitivity_test(
                        baseline_scores[:n], scores[:n]
                    )
                    analyses[f"{baseline_cond}_vs_{cond}"] = test_result

        return {
            "source": str(results_path),
            "conditions_analyzed": list(scores_by_condition.keys()),
            "sensitivity_tests": analyses,
            "summary": self._summarize_sensitivity(analyses),
        }

    def _summarize_sensitivity(self, analyses: Dict[str, Dict]) -> Dict[str, Any]:
        """Summarize sensitivity results."""
        if not analyses:
            return {"conclusion": "No sensitivity tests performed"}

        n_significant = sum(1 for a in analyses.values() if a.get("significant"))
        n_total = len(analyses)

        avg_effect = np.mean([
            a.get("effect_size_d", 0) for a in analyses.values()
        ])
        avg_drop = np.mean([
            a.get("percent_drop", 0) for a in analyses.values()
        ])

        if n_significant == n_total and avg_effect > 0.5:
            verdict = "STRONG SENSITIVITY: MSCI reliably detects perturbations"
        elif n_significant > n_total / 2:
            verdict = "MODERATE SENSITIVITY: MSCI detects most perturbations"
        elif n_significant > 0:
            verdict = "WEAK SENSITIVITY: MSCI detects some perturbations"
        else:
            verdict = "NO SENSITIVITY: MSCI fails to detect perturbations"

        return {
            "n_tests": n_total,
            "n_significant": n_significant,
            "sensitivity_rate": n_significant / n_total if n_total > 0 else 0,
            "average_effect_size": avg_effect,
            "average_percent_drop": avg_drop,
            "verdict": verdict,
        }

    def generate_report(
        self,
        gradient_result: Optional[PerturbationGradient] = None,
        sensitivity_tests: Optional[Dict[str, Dict]] = None,
        output_path: Optional[Path] = None,
    ) -> Dict[str, Any]:
        """
        Generate comprehensive sensitivity report.

        Args:
            gradient_result: Optional perturbation gradient analysis
            sensitivity_tests: Optional dict of sensitivity test results
            output_path: Optional path to save report

        Returns:
            Complete sensitivity report
        """
        report = {
            "analysis_type": "MSCI Sensitivity Analysis",
            "research_question": "RQ1: Is MSCI sensitive to controlled semantic perturbations?",
            "hypothesis": {
                "H0": "MSCI(baseline) = MSCI(perturbed)",
                "H1": "MSCI(baseline) > MSCI(perturbed)",
            },
        }

        if gradient_result:
            report["gradient_analysis"] = gradient_result.to_dict()
            report["gradient_verdict"] = (
                "PASS: MSCI shows monotonic decrease with perturbation"
                if gradient_result.is_monotonic and gradient_result.sensitivity_score > 0.5
                else "FAIL: MSCI does not reliably track perturbation level"
            )

        if sensitivity_tests:
            report["sensitivity_tests"] = sensitivity_tests
            report["summary"] = self._summarize_sensitivity(sensitivity_tests)

        # Overall RQ1 verdict
        verdicts = []
        if gradient_result:
            verdicts.append(gradient_result.sensitivity_score > 0.5)
        if sensitivity_tests:
            summary = report.get("summary", {})
            verdicts.append(summary.get("sensitivity_rate", 0) > 0.5)

        if verdicts:
            report["rq1_verdict"] = (
                "SUPPORTED: MSCI is sensitive to semantic perturbations"
                if all(verdicts)
                else "PARTIALLY SUPPORTED: Mixed sensitivity results"
                if any(verdicts)
                else "NOT SUPPORTED: MSCI is not reliably sensitive"
            )

        if output_path:
            output_path = Path(output_path)
            output_path.parent.mkdir(parents=True, exist_ok=True)
            with output_path.open("w", encoding="utf-8") as f:
                json.dump(report, f, indent=2, ensure_ascii=False)

        return report