Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
cboengine.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2020 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file cbo.cpp
20 \brief collateralized bond obligation pricing engine
21*/
22
23#include <fstream>
24#include <iostream>
25#include <stdio.h>
26
28
29namespace QuantExt {
30
31Stats::Stats(std::vector<Real> data) : data_(data), mean_(0.0), std_(0.0), max_(0.0), min_(0.0) {
32 for (Size i = 0; i < data_.size(); i++) {
33 mean_ += data_[i];
34 std_ += data_[i] * data_[i];
35 }
36 mean_ /= data_.size();
37 std_ /= data_.size();
38 std_ = sqrt(std_ - mean_ * mean_);
39 max_ = *(std::max_element(data_.begin(), data_.end()));
40 min_ = *(std::min_element(data_.begin(), data_.end()));
41}
42
43Distribution Stats::histogram(Size bins, Real xmin, Real xmax) {
44 Distribution dist(bins, std::max(min_, xmin), std::min(max_, xmax));
45 for (Size i = 0; i < data_.size(); i++) {
46 if (data_[i] > xmax)
47 data_[i] = xmax;
48 if (data_[i] < xmin)
49 data_[i] = xmin;
50 dist.add(data_[i]);
51 }
52 dist.normalize();
53 return dist;
54}
55
56void print(Distribution& dist, std::string fileName) {
57 std::ofstream file;
58 file.open(fileName.c_str());
59 if (!file.is_open())
60 QL_FAIL("error opening file " << fileName);
61 file.setf(std::ios::scientific, std::ios::floatfield);
62 file.setf(std::ios::showpoint);
63 file.precision(4);
64 for (Size i = 0; i < dist.size(); i++)
65 file << i << " " << dist.x(i) << " " << dist.density(i) << std::endl;
66 file.close();
67}
68
69} // namespace QuantExt
collateralized bond obligation pricing engine
Distribution histogram(Size bins, Real xmin=-QL_MAX_REAL, Real xmax=QL_MAX_REAL)
Definition: cboengine.cpp:43
Stats(std::vector< Real > data)
Definition: cboengine.cpp:31
std::vector< Real > data_
Definition: cboengine.hpp:56
RandomVariable sqrt(RandomVariable x)
void print(Distribution &dist, std::string fileName)
Definition: cboengine.cpp:56