C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
BaseAPIResourceView.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Runtime.Serialization;
5
11
12// Disable warnings about snake_case function names in this method. While it goes against
13// C# best-practice, it's done intentionally to follow the API conventions closely.
14#pragma warning disable IDE1006
15
17{
20 {
21 #region Private Fields
23 private YLTSimulationResult _ylt;
24
26 private YELTSimulationResult _yelt;
27 #endregion Private Fields
28
29 #region Public Properties
31 [DataMember(Order = 11)]
32 [NotNull]
34
36 [DataMember(Order = 12)]
38 public string ylt_id { get; set; }
39
43 [DataMember(Order = 13)]
45 public string target_currency { get; set; }
46
50 public YLTSimulationResult ylt => _ylt ?? (_ylt = new YLTSimulationResult(this));
51
55 public YELTSimulationResult yelt => _yelt ?? (_yelt = new YELTSimulationResult(this));
56 #endregion Public Properties
57
58 #region Public Metrics Requests
59 #region Tail Metrics
69 public TailMetrics tail_metrics(double probability,
70 MetricsOptions options = null, int? timeout = null)
71 {
73 return this.GetSubResource<TailMetrics>(new WindowMetricsRequest(
74 ProbabilityWindow.Tail(probability)), options.GetParameters(), timeout);
75 }
76
92 #endregion Tail Metrics
93
94 #region Window Metrics
102 MetricsOptions options = null, int? timeout = null)
103 {
105 return this.GetSubResource<WindowMetrics>(new WindowMetricsRequest(range),
106 options.GetParameters(), timeout);
107 }
108
124 #endregion Window Metrics
125
126 #region Co-Metrics
137 IReference<T> component, CoMetricsOptions options = null, int? timeout = null)
139 {
141 return this.GetSubResource<CoMetrics>(new CoMetricsRequest(range),
143 }
144
163
164 #region Private Helper Methods
174 {
175 return options.GetParameters()
176 .AddQueryParameter("component_id", component.ref_id)
177 .AddQueryParameter("component_type", GetComponentTypeFromReference(component));
178 }
179
183 private string GetComponentTypeFromReference<T>(IReference<T> component)
185 {
186 // Get the most accurate type we can for the referenced component.
187 Type referenceType = component.ResourceType;
188 // Convert the reference type to the string the server expects.
189 string type = typeof(ILayerView).IsAssignableFrom(referenceType) ? "LayerView" :
190 typeof(PortfolioView).IsAssignableFrom(referenceType) ? "PortfolioView" : null;
191 // Throw an exception if the type could not be resolved.
192 if (type == null)
193 throw new ArgumentException("The component must be a reference to either a " +
194 "LayerView or PortfolioView type. Supplied reference was of type: " +
195 referenceType.NiceTypeName());
196 return type;
197 }
198 #endregion Private Helper Methods
199 #endregion Co-Metrics
200
201 #region Exceedance Probability
211 ExceedanceProbabilityOptions options = null, int? timeout = null)
212 {
215 new ExceedanceProbRequest(threshold), options.GetParameters(), timeout);
216 }
217
234 #endregion Exceedance Probability
235
236 #region Back Allocation
250 [Obsolete("POST a BackAllocatedLayer structure instead.")]
251 public BackAllocations back_allocations(string source_id, int? timeout = null)
252 {
253 // Make the request from the server.
255 new BackAllocationsRequest(),
256 new RequestParameters().AddQueryParameter(nameof(source_id), source_id), timeout);
257 }
258 #endregion Back Allocation
259 #endregion Public Metrics Requests
260
262 protected override void AfterMembersCloned(APIType _)
263 {
264 base.AfterMembersCloned(_);
265 _ylt = null;
266 _yelt = null;
267 }
268
269 #region Helper Methods
273 string resource, RequestParameters parameters, int? timeout)
274 {
275 if (values == null || values.Count == 0)
276 throw new ArgumentException("The list of values cannot be null or empty.");
277 // If the user has supplied a list of exactly one probability, the server will
278 // return a single value rather than a list, so we must convert the result to a list.
279 // Otherwise, submit as a normal vectorized request and expect a list of results back.
280 return values.Count == 1 ?
281 new List<T> { this.GetSubResource<T>(resource, parameters, timeout) } :
282 this.GetSubResource<List<T>>(resource, parameters, timeout);
283 }
284
288 string resource, RequestParameters parameters, int? timeout)
289 {
290 if (values == null || values.Count == 0)
291 throw new ArgumentException("The list of values cannot be null or empty.");
292 // If the user has supplied a list of exactly one probability, the server will
293 // return a single value rather than a list, so we must convert the result to a list.
294 // Otherwise, submit as a normal vectorized request and expect a list of results back.
295 return values.Count == 1 ?
296 new List<T> { this.GetSubResource<T>(resource, parameters, timeout) } :
297 this.GetSubResource<List<T>>(resource, parameters, timeout);
298 }
299 #endregion Helper Methods
300
301 #region Request SubResources
304 internal class WindowMetricsRequest : ResourcePathBuilder
305 {
306 private const string SERVER_ENDPOINT_NAME = "window_metrics";
307 protected override string base_path => SERVER_ENDPOINT_NAME;
309 public WindowMetricsRequest(ProbabilityWindow range) : base(range) { }
311 public WindowMetricsRequest(IEnumerable<ProbabilityWindow> ranges) : base(ranges) { }
312 }
313
316 internal class CoMetricsRequest : ResourcePathBuilder
317 {
318 private const string SERVER_ENDPOINT_NAME = "window_co_metrics";
319 protected override string base_path => SERVER_ENDPOINT_NAME;
321 public CoMetricsRequest(ProbabilityWindow range) : base(range) { }
323 public CoMetricsRequest(IEnumerable<ProbabilityWindow> ranges) : base(ranges) { }
324 }
325
328 internal class ExceedanceProbRequest : ResourcePathBuilder
329 {
330 private const string SERVER_ENDPOINT_NAME = "exceedance_probabilities";
331 protected override string base_path => SERVER_ENDPOINT_NAME;
333 public ExceedanceProbRequest(double threshold) : base(threshold) { }
335 public ExceedanceProbRequest(IEnumerable<double> thresholds) : base(thresholds) { }
336 }
337
340 internal class BackAllocationsRequest : ResourcePathBuilder
341 {
342 private const string SERVER_ENDPOINT_NAME = "back_allocations";
343 protected override string base_path => SERVER_ENDPOINT_NAME;
344 }
345 #endregion Request SubResources
346
347 // TODO: Delete Deprecated Methods after phase-out period.
348 #region Deprecated Public Methods
359 [Obsolete("Please provide a ProbabilityWindow rather than a double tail-probability. " +
360 "This legacy method was less flexible and only returned the co-tail-metrics.")]
362 double probability, IReference<T> component, CoMetricsOptions options)
364 {
365 return co_metrics(ProbabilityWindow.Tail(probability), component, options);
366 }
367
378 [Obsolete("Please provide a list of ProbabilityWindows rather than doubles. " +
379 "This legacy method was less flexible and only returned the co-tail-metrics.")]
386 #endregion Deprecated Public Methods
387 }
388}
PortfolioView and LayerView abstract base class.
WindowMetrics window_metrics(ProbabilityWindow range, MetricsOptions options=null, int? timeout=null)
Returns the window metrics of this view for a given probability range.
List< ExceedanceProbability > exceedance_probability(IEnumerable< double > thresholds, ExceedanceProbabilityOptions options=null, int? timeout=null)
Returns the exceedance probability for this view for the given losses. That is, the probability the s...
ExceedanceProbability exceedance_probability(double threshold, ExceedanceProbabilityOptions options=null, int? timeout=null)
Returns the exceedance probability for this view for the given loss. That is, the probability the spe...
List< TailMetrics > tail_metrics(IEnumerable< double > probabilities, MetricsOptions options=null, int? timeout=null)
Returns the tail metrics of this view at multiple probabilities.
YLTSimulationResult ylt
Allows retrieval of the YLT (Year Loss Table) associated with this view once it has been simulated.
string ylt_id
The hash identifier for the simulation results of this view.
BackAllocations back_allocations(string source_id, int? timeout=null)
Returns a URL to the YELT of this view back-allocated to some component loss source.
string target_currency
The currency this view's YLTs are stored in, and the default currency results are returned in....
YELTSimulationResult yelt
Allows retrieval of the YELT (Year Event Loss Table) associated with this view once it has been simul...
List< WindowMetrics > window_metrics(IEnumerable< ProbabilityWindow > ranges, MetricsOptions options=null, int? timeout=null)
Returns the window metrics of this view for multiple probability ranges.
override void AfterMembersCloned(APIType _)
Invoked following construction if the current instance has been created using a member-wise clone of ...
TailMetrics tail_metrics(double probability, MetricsOptions options=null, int? timeout=null)
Returns the tail metrics of this view at a given probability.
CoMetrics co_metrics< T >(ProbabilityWindow range, IReference< T > component, CoMetricsOptions options=null, int? timeout=null)
Returns the co-metrics of this view for a given probability range.
IReference< AnalysisProfile > analysis_profile
The simulation's analysis profile.
Attribute used to define the default value assigned to the target_currency property by the server whe...
A pass-through to the yelt endpoint for configuring and downloading a per-trial simulation result fro...
A pass-through to the ylt endpoint for configuring and downloading a per-trial simulation result from...
Base class used by all resources.
Base class used by all types and resources.
Definition APIType.cs:8
Describes a collection of resources which can be listed.
Specifies that a property is generated by the server and should not be specified on the client side d...
The structure returned when requesting back-allocated metrics for a view.
Optional parameters which can be specified for co-metrics requests.
static new CoMetricsOptions Default
The default co-metrics request options used when none are specified.
The structure returned when requesting Co-Metrics for a view.
Optional parameters which can be specified for exceedance probability requests.
static new ExceedanceProbabilityOptions Default
The default exceedance probability request options used when none are specified.
The structure returned when requesting Exceedance Probability for a view.
Optional parameters which can be specified for all metrics requests.
static new MetricsOptions Default
The default metrics request options used when none are specified.
The structure returned when requesting Tail Metrics for a view.
The structure returned when requesting Window Metrics for a view, containing the core window distribu...
Represents the Analysis of a Portfolio.
A probability range used to dictate the set of ordered trial losses in a loss distribution that shoul...
static ProbabilityWindow Tail(double tail_probability)
Returns a window representing the tail probability range [0, tail_probability], such that all losses ...
Helper class which makes it easier to build a set of request parameters.
PortfolioView and LayerView interface.
Represents the Analysis of a Layer.