C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
ProbabilityWindow.cs
Go to the documentation of this file.
1using System;
3
4namespace AnalyzeRe
5{
11 public class ProbabilityWindow : APIType, IEquatable<ProbabilityWindow>
12 {
13 #region Private Fields
17 private readonly int _minTrialSelection;
18
22 private readonly int _maxTrialSelection;
23
24 // The cached computed hash value for this probability window.
25 private readonly int _hashValue;
26 #endregion Private Fields
27
30 [GreaterThan(0, true), LessThan(1, false)]
31 public double min_probability { get; }
32
35 [GreaterThan(0, false), LessThan(1, true)]
36 public double max_probability { get; }
37
45 public ProbabilityWindow(double min, double max)
46 {
47 min_probability = min;
48 max_probability = max;
49 if (min < 0)
50 throw new ArgumentOutOfRangeException(nameof(min), min,
51 "The min probability must be greater than or equal to zero. The range is [0,1)");
52 if (min >= 1)
53 throw new ArgumentOutOfRangeException(nameof(min), min,
54 "The min probability must be less than one. The range is [0,1)");
55 if (max <= 0)
56 throw new ArgumentOutOfRangeException(nameof(max), max,
57 "The max probability must be greater than zero. The range is (0,1]");
58 if (max > 1)
59 throw new ArgumentOutOfRangeException(nameof(max), max,
60 "The max probability must be less than or equal to one. The range is (0,1]");
61 if (max < min)
62 throw new ArgumentOutOfRangeException(nameof(max), max,
63 "The max probability must be greater or equal to than the min probability.");
64 // Compute internal properties used for optimal hashing and equivalence
65 _minTrialSelection = (int)(min * Int32.MaxValue);
66 _maxTrialSelection = (int)(max * Int32.MaxValue);
67 _hashValue = new { _minTrialSelection, _maxTrialSelection }.GetHashCode();
68 }
69
70 #region Public Static Factory Methods
73 public static ProbabilityWindow All => new ProbabilityWindow(0, 1);
74
79 public static ProbabilityWindow Tail(double tail_probability) => new ProbabilityWindow(0, tail_probability);
80
86 public static ProbabilityWindow FromReturnPeriods(double starting_return_period, double ending_return_period) =>
87 new ProbabilityWindow(1d / ending_return_period, 1d / starting_return_period);
88
93 [Obsolete("Warning: This implicit conversion will be deprecated. You should explicitly create a probability " +
94 "window for a tail distribution using ProbabilityWindow.Tail if that is your intention.")]
95 public static implicit operator ProbabilityWindow(double tail) => Tail(tail);
96 #endregion Public Static Factory Methods
97
98 #region IEquatable Implementation
105 public bool Equals(ProbabilityWindow other) => other != null &&
106 _minTrialSelection == other._minTrialSelection && _maxTrialSelection == other._maxTrialSelection;
107
109 public override bool Equals(object obj) => obj is ProbabilityWindow pw && Equals(pw);
110
112 public override int GetHashCode() => _hashValue;
113 #endregion IEquatable Implementation
114
118 public override string ToString() => $"{min_probability:R}_{max_probability:R}";
119 }
120}
Base class used by all types and resources.
Definition APIType.cs:8
A probability range used to dictate the set of ordered trial losses in a loss distribution that shoul...
bool Equals(ProbabilityWindow other)
Determines whether the specified probability windows have precisely the same minimum and maximum.
override bool Equals(object obj)
static ProbabilityWindow FromReturnPeriods(double starting_return_period, double ending_return_period)
Create a probability window from return period window.
double min_probability
The inclusive lower-bound of the probability window, which will correspond to the largest trial loss ...
double max_probability
The inclusive upper-bound of the probability window, which will correspond to the smallest trial loss...
override string ToString()
static ProbabilityWindow All
Returns a window representing the full probability range [0, 1], such that all trial losses will be i...
override int GetHashCode()
ProbabilityWindow(double min, double max)
Constructs a probability window from a min and max probability.
static ProbabilityWindow Tail(double tail_probability)
Returns a window representing the tail probability range [0, tail_probability], such that all losses ...