/usr/include/ql/experimental/math/tcopulapolicy.hpp is in libquantlib0-dev 1.7.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2014 Jose Aparicio
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#ifndef quantlib_tcopula_policy_hpp
#define quantlib_tcopula_policy_hpp
#include <ql/errors.hpp>
#include <ql/utilities/disposable.hpp>
#include <ql/experimental/math/convolvedstudentt.hpp>
#include <boost/math/distributions/students_t.hpp>
#include <boost/bind.hpp>
#include <vector>
namespace QuantLib {
/*! \brief Sudent-T Latent Model's copula policy.
Describes the copula of a set of normalized Student-T independent random
factors to be fed into the latent variable model.
The latent model requires the independent variables to be of unit variance
so the policy expects the factors coefficients to be as usual and the T
variables to be normalized, the normalization is performed by the policy.
To normalize the random variables they are divided by the square root of
the variance of each T (\f$ \frac{\nu}{\nu-2}\f$)
*/
class TCopulaPolicy {
public:
/*! Stores the parameters defining the factors random variable
T-distributions. As it is now the latent models are restricted to
having the same distribution for all idiosyncratic factors, so only
one parameter is needed for them.
*/
typedef
struct {
std::vector<Integer> tOrders;
} initTraits;
/*! Delayed initialization of the distribution parameters and caches.
To be called by the latent model. */
/* \todo
Explore other constructors, with different vector dimensions, defining
simpler combinations (only one correlation, only one variable) might
simplify memory.
*/
explicit TCopulaPolicy(
const std::vector<std::vector<Real> >& factorWeights =
std::vector<std::vector<Real> >(),
const initTraits& vals = initTraits());
//! Number of independent random factors.
Size numFactors() const {
return latentVarsInverters_.size() + varianceFactors_.size() - 1;
}
//! returns a copy of the initialization arguments
//... better to have a cache?
initTraits getInitTraits() const {
initTraits data;
data.tOrders.resize(distributions_.size());
std::transform(distributions_.begin(), distributions_.end(),
data.tOrders.begin(),
boost::bind(
&boost::math::students_t_distribution<>::degrees_of_freedom, _1)
);
return data;
}
const std::vector<Real>& varianceFactors() const {
return varianceFactors_;
}
/*! Cumulative probability of the indexed latent variable
@param iVariable The index of the latent variable requested.
*/
Probability cumulativeY(Real val, Size iVariable) const {
#if defined(QL_EXTRA_SAFETY_CHECKS)
QL_REQUIRE(iVariable < latentVarsCumul_.size(),
"Latent variable index out of bounds.");
#endif
return latentVarsCumul_[iVariable](val);
}
//! Cumulative probability of the idiosyncratic factors (all the same)
Probability cumulativeZ(Real z) const {
return boost::math::cdf(distributions_.back(), z /
varianceFactors_.back());
}
/*! Probability density of a given realization of values of the systemic
factors (remember they are independent).
Intended to be used in numerical integration of an arbitrary function
depending on those values.
*/
Probability density(const std::vector<Real>& m) const {
#if defined(QL_EXTRA_SAFETY_CHECKS)
QL_REQUIRE(m.size() == distributions_.size()-1,
"Incompatible sample and latent model sizes");
#endif
Real prodDensities = 1.;
for(Size i=0; i<m.size(); i++)
prodDensities *= boost::math::pdf(distributions_[i],
m[i] /varianceFactors_[i]) /varianceFactors_[i];
// accumulate lambda
return prodDensities;
}
/*! Returns the inverse of the cumulative distribution of the (modelled)
latent variable (as indexed by iVariable). Involves the convolution
of the factors' distributions.
*/
Real inverseCumulativeY(Probability p, Size iVariable) const {
#if defined(QL_EXTRA_SAFETY_CHECKS)
QL_REQUIRE(iVariable < latentVarsCumul_.size(),
"Latent variable index out of bounds.");
#endif
return latentVarsInverters_[iVariable](p);
}
/*! Returns the inverse of the cumulative distribution of the
idiosincratic factor. The LM here is limited to all idiosincratic
factors following the same distribution.
*/
Real inverseCumulativeZ(Probability p) const {
return boost::math::quantile(distributions_.back(), p)
* varianceFactors_.back();
}
/*! Returns the inverse of the cumulative distribution of the
systemic factor iFactor.
*/
Real inverseCumulativeDensity(Probability p, Size iFactor) const {
#if defined(QL_EXTRA_SAFETY_CHECKS)
QL_REQUIRE(iFactor < distributions_.size()-1,
"Random factor variable index out of bounds.");
#endif
return boost::math::quantile(distributions_[iFactor], p)
* varianceFactors_[iFactor];
}
//to use this (by default) version, the generator must be a uniform one.
Disposable<std::vector<Real> >
allFactorCumulInverter(const std::vector<Real>& probs) const;
private:
mutable std::vector<boost::math::students_t_distribution<> >
distributions_;
mutable std::vector<Real> varianceFactors_;
mutable std::vector<CumulativeBehrensFisher> latentVarsCumul_;
mutable std::vector<InverseCumulativeBehrensFisher>
latentVarsInverters_;
};
}
#endif
|