{"record":{"id":"69e99c22bf5366d2","repo":"nautechsystems/nautilus_trader","slug":"invalid-confidence-for-expectedshortfall","errorCode":null,"errorMessage":"Invalid `confidence` for `ExpectedShortfall`","messagePattern":"Invalid `confidence` for `ExpectedShortfall`","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/analysis/src/statistics/expected_shortfall.rs","lineNumber":83,"sourceCode":"    ///\n    /// Returns an error if `confidence` is not finite and in the range `(0, 1)`.\n    pub fn new_checked(confidence: Option<f64>) -> anyhow::Result<Self> {\n        let confidence = confidence.unwrap_or(0.95);\n        check_predicate_true(\n            confidence.is_finite() && confidence > 0.0 && confidence < 1.0,\n            \"confidence must be finite and in the range (0, 1)\",\n        )?;\n        Ok(Self { confidence })\n    }\n\n    /// Creates a new [`ExpectedShortfall`] instance.\n    ///\n    /// # Panics\n    ///\n    /// Panics if `confidence` is not finite and in the range `(0, 1)`.\n    #[must_use]\n    pub fn new(confidence: Option<f64>) -> Self {\n        Self::new_checked(confidence).expect(\"Invalid `confidence` for `ExpectedShortfall`\")\n    }\n}\n\nimpl Display for ExpectedShortfall {\n    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {\n        write!(f, \"Expected Shortfall (confidence {})\", self.confidence)\n    }\n}\n\nimpl PortfolioStatistic for ExpectedShortfall {\n    type Item = f64;\n\n    fn name(&self) -> String {\n        self.to_string()\n    }\n\n    fn calculate_from_returns(&self, raw_returns: &Returns) -> Option<Self::Item> {\n        if !self.check_valid_returns(raw_returns) {","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/analysis/src/statistics/expected_shortfall.rs#L65-L101","documentation":"ExpectedShortfall::new is the infallible constructor wrapping new_checked, which validates that confidence is finite and strictly within (0, 1) (e.g. 0.95 or 0.99). The expect panics when the supplied value fails validation, since the panicking constructor is meant for known-good constants.","triggerScenarios":"Calling ExpectedShortfall::new with Some(x) where x <= 0, x >= 1, or x is NaN/infinity; also None default path if its internal default were invalid (it is not, so practically only bad Some values).","commonSituations":"Confidence expressed as a percentage (95.0 instead of 0.95); reading the value from config as '95%' string remainder; NaN propagating from parsed config or computed parameters.","solutions":["Pass confidence as a fraction strictly between 0 and 1 (e.g. 0.99, not 99).","Validate/clamp the config value before constructing: require x.is_finite() && 0.0 < x && x < 1.0.","Use ExpectedShortfall::new_checked(...) and handle the Result instead of the panicking new()."],"exampleFix":"// before\nlet es = ExpectedShortfall::new(Some(95.0)); // panics: 95 not in (0,1)\n\n// after\nlet es = ExpectedShortfall::new(Some(0.95));","handlingStrategy":"validation","validationCode":"# python caller guard\nconfidence = config[\"confidence\"]  # e.g. 95 meaning percent\nconfidence = confidence / 100.0 if confidence > 1.0 else confidence\nassert 0.0 < confidence < 1.0, \"confidence must be in (0, 1)\"\nes = ExpectedShortfall(confidence)","typeGuard":"fn valid_confidence(c: f64) -> bool { c.is_finite() && c > 0.0 && c < 1.0 }","tryCatchPattern":"# Rust side: prefer the checked constructor\nmatch ExpectedShortfall::new_checked(Some(c)) {\n    Ok(es) => es,\n    Err(e) => { log::error!(\"bad confidence {c}: {e}\"); ExpectedShortfall::new(None) }\n}","preventionTips":["Always express confidence as a fraction in (0, 1), never a percentage.","Validate config-sourced floats with is_finite() and range checks before constructing statistics.","Prefer the *_new_checked constructors when values come from user input."],"tags":["rust","panic","statistics","expected-shortfall","validation","argument"],"backgroundTag":"argument-out-of-range","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}