Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
StabilisedGLLS Class Reference

Numerically stabilised general linear least squares. More...

#include <qle/math/stabilisedglls.hpp>

+ Collaboration diagram for StabilisedGLLS:

Public Types

enum  Method { None , MaxAbs , MeanStdDev }
 

Public Member Functions

template<class xContainer , class yContainer , class vContainer >
 StabilisedGLLS (const xContainer &x, const yContainer &y, const vContainer &v, const Method method=MeanStdDev)
 
const Array & transformedCoefficients () const
 
const Array & transformedResiduals () const
 
const Array & transformedStandardErrors () const
 
const Array & transformedError () const
 
const Array & xMultiplier () const
 Transformation parameters (u => (u + shift) * multiplier for u = x, y) More...
 
const Array & xShift () const
 
const Real yMultiplier () const
 
const Real yShift () const
 
Size size () const
 
Size dim () const
 
template<class xType , class vContainer >
Real eval (xType x, vContainer &v, typename boost::enable_if< typename boost::is_arithmetic< xType >::type >::type *=0)
 evaluate regression function in terms of original x, y More...
 
template<class xType , class vContainer >
Real eval (xType x, vContainer &v, typename boost::disable_if< typename boost::is_arithmetic< xType >::type >::type *=0)
 evaluate regression function in terms of original x, y More...
 

Protected Member Functions

template<class xContainer , class yContainer , class vContainer >
void calculate (xContainer x, yContainer y, vContainer v, typename boost::enable_if< typename boost::is_arithmetic< typename xContainer::value_type >::type >::type *=0)
 
template<class xContainer , class yContainer , class vContainer >
void calculate (xContainer x, yContainer y, vContainer v, typename boost::disable_if< typename boost::is_arithmetic< typename xContainer::value_type >::type >::type *=0)
 

Protected Attributes

Array a_
 
Array err_
 
Array residuals_
 
Array standardErrors_
 
Array xMultiplier_
 
Array xShift_
 
Real yMultiplier_
 
Real yShift_
 
Method method_
 
QuantLib::ext::shared_ptr< GeneralLinearLeastSquares > glls_
 

Detailed Description

Numerically stabilised general linear least squares.

The input data is linearly transformed before performing the linear least squares fit. The linear least squares fit on the transformed data is done using the GeneralLinearLeastSquares class.

Definition at line 51 of file stabilisedglls.hpp.

Member Enumeration Documentation

◆ Method

enum Method
Enumerator
None 
MaxAbs 
MeanStdDev 

Definition at line 53 of file stabilisedglls.hpp.

53 {
54 None, // No stabilisation
55 MaxAbs, // Divide x and y values by max of abs of values (per x coordinate, y)
56 MeanStdDev // Subtract mean and divide by std dev (per x coordinate, y)
57 };

Constructor & Destructor Documentation

◆ StabilisedGLLS()

StabilisedGLLS ( const xContainer &  x,
const yContainer &  y,
const vContainer &  v,
const Method  method = MeanStdDev 
)

Definition at line 102 of file stabilisedglls.hpp.

104 : a_(v.end() - v.begin(), 0.0), err_(v.end() - v.begin(), 0.0), residuals_(y.end() - y.begin()),
105 standardErrors_(v.end() - v.begin()), method_(method) {
106 calculate(x, y, v);
107}
void calculate(xContainer x, yContainer y, vContainer v, typename boost::enable_if< typename boost::is_arithmetic< typename xContainer::value_type >::type >::type *=0)
+ Here is the call graph for this function:

Member Function Documentation

◆ transformedCoefficients()

const Array & transformedCoefficients ( ) const

Definition at line 61 of file stabilisedglls.hpp.

61{ return glls_->coefficients(); }
QuantLib::ext::shared_ptr< GeneralLinearLeastSquares > glls_
+ Here is the caller graph for this function:

◆ transformedResiduals()

const Array & transformedResiduals ( ) const

Definition at line 62 of file stabilisedglls.hpp.

62{ return glls_->residuals(); }

◆ transformedStandardErrors()

const Array & transformedStandardErrors ( ) const

Definition at line 63 of file stabilisedglls.hpp.

63{ return glls_->standardErrors(); }

◆ transformedError()

const Array & transformedError ( ) const

Definition at line 64 of file stabilisedglls.hpp.

64{ return glls_->error(); }

◆ xMultiplier()

const Array & xMultiplier ( ) const

Transformation parameters (u => (u + shift) * multiplier for u = x, y)

Definition at line 67 of file stabilisedglls.hpp.

+ Here is the caller graph for this function:

◆ xShift()

const Array & xShift ( ) const

Definition at line 68 of file stabilisedglls.hpp.

68{ return xShift_; }

◆ yMultiplier()

const Real yMultiplier ( ) const

Definition at line 69 of file stabilisedglls.hpp.

+ Here is the caller graph for this function:

◆ yShift()

const Real yShift ( ) const

Definition at line 70 of file stabilisedglls.hpp.

70{ return yShift_; }

◆ size()

Size size ( ) const

Definition at line 72 of file stabilisedglls.hpp.

72{ return glls_->residuals().size(); }

◆ dim()

Size dim ( ) const

Definition at line 73 of file stabilisedglls.hpp.

73{ return glls_->coefficients().size(); }

◆ eval() [1/2]

Real eval ( xType  x,
vContainer &  v,
typename boost::enable_if< typename boost::is_arithmetic< xType >::type >::type *  = 0 
)

evaluate regression function in terms of original x, y

Definition at line 248 of file stabilisedglls.hpp.

249 {
250 QL_REQUIRE(v.size() == glls_->dim(),
251 "StabilisedGLLS::eval(): v size (" << v.size() << ") must be equal to dim (" << glls_->dim());
252 Real tmp = 0.0;
253 for (Size i = 0; i < v.size(); ++i) {
254 tmp += glls_->coefficients()[i] * v[i]((x + xShift_[0]) * xMultiplier_[0]);
255 }
256 return tmp / yMultiplier_ - yShift_;
257}
+ Here is the caller graph for this function:

◆ eval() [2/2]

Real eval ( xType  x,
vContainer &  v,
typename boost::disable_if< typename boost::is_arithmetic< xType >::type >::type *  = 0 
)

evaluate regression function in terms of original x, y

Definition at line 260 of file stabilisedglls.hpp.

261 {
262 QL_REQUIRE(v.size() == glls_->dim(),
263 "StabilisedGLLS::eval(): v size (" << v.size() << ") must be equal to dim (" << glls_->dim());
264 Real tmp = 0.0;
265 for (Size i = 0; i < v.size(); ++i) {
266 xType xNew(x.end() - x.begin());
267 for (Size j = 0; j < static_cast<Size>(x.end() - x.begin()); ++j) {
268 xNew[j] = (x[j] + xShift_[j]) * xMultiplier_[j];
269 }
270 tmp += glls_->coefficients()[i] * v[i](xNew);
271 }
272 return tmp / yMultiplier_ - yShift_;
273}

◆ calculate() [1/2]

void calculate ( xContainer  x,
yContainer  y,
vContainer  v,
typename boost::enable_if< typename boost::is_arithmetic< typename xContainer::value_type >::type >::type *  = 0 
)
protected

Definition at line 110 of file stabilisedglls.hpp.

112 {
113
114 std::vector<Real> xData(x.end() - x.begin(), 0.0), yData(y.end() - y.begin(), 0.0);
115 xMultiplier_ = Array(1, 1.0);
116 xShift_ = Array(1, 0.0);
117 yMultiplier_ = 1.0;
118 yShift_ = 0.0;
119
120 switch (method_) {
121 case None:
122 break;
123 case MaxAbs: {
124 Real mx = 0.0, my = 0.0;
125 for (Size i = 0; i < static_cast<Size>(x.end() - x.begin()); ++i) {
126 mx = std::max(std::abs(x[i]), mx);
127 }
128 if (!QuantLib::close_enough(mx, 0.0))
129 xMultiplier_[0] = 1.0 / mx;
130 for (Size i = 0; i < static_cast<Size>(y.end() - y.begin()); ++i) {
131 my = std::max(std::abs(y[i]), my);
132 }
133 if (!QuantLib::close_enough(my, 0.0))
134 yMultiplier_ = 1.0 / my;
135 break;
136 }
137 case MeanStdDev: {
138 accumulator_set<Real, stats<boost::accumulators::tag::mean, boost::accumulators::tag::variance> > acc;
139 for (Size i = 0; i < static_cast<Size>(x.end() - x.begin()); ++i) {
140 acc(x[i]);
141 }
142 xShift_[0] = -mean(acc);
143 Real tmp = boost::accumulators::variance(acc);
144 if (!QuantLib::close_enough(tmp, 0.0))
145 xMultiplier_[0] = 1.0 / std::sqrt(tmp);
146 accumulator_set<Real, stats<boost::accumulators::tag::mean, boost::accumulators::tag::variance> > acc2;
147 for (Size i = 0; i < static_cast<Size>(y.end() - y.begin()); ++i) {
148 acc2(y[i]);
149 }
150 yShift_ = -mean(acc2);
151 Real tmp2 = boost::accumulators::variance(acc2);
152 if (!QuantLib::close_enough(tmp2, 0.0))
153 yMultiplier_ = 1.0 / std::sqrt(tmp2);
154 break;
155 }
156 default:
157 QL_FAIL("unknown stabilisation method");
158 }
159
160 for (Size i = 0; i < static_cast<Size>(x.end() - x.begin()); ++i) {
161 xData[i] = (x[i] + xShift_[0]) * xMultiplier_[0];
162 }
163 for (Size i = 0; i < static_cast<Size>(y.end() - y.begin()); ++i) {
164 yData[i] = (y[i] + yShift_) * yMultiplier_;
165 }
166
167 glls_ = QuantLib::ext::make_shared<GeneralLinearLeastSquares>(xData, yData, v);
168}
+ Here is the caller graph for this function:

◆ calculate() [2/2]

void calculate ( xContainer  x,
yContainer  y,
vContainer  v,
typename boost::disable_if< typename boost::is_arithmetic< typename xContainer::value_type >::type >::type *  = 0 
)
protected

Definition at line 171 of file stabilisedglls.hpp.

173 {
174
175 QL_REQUIRE(x.end() - x.begin() > 0, "StabilisedGLLS::calculate(): x container is empty");
176 QL_REQUIRE(x[0].end() - x[0].begin() > 0, "StabilisedGLLS:calculate(): x contains empty point(s)");
177
178 std::vector<Array> xData(x.end() - x.begin(), Array(x[0].end() - x[0].begin(), 0.0));
179 std::vector<Real> yData(y.end() - y.begin(), 0.0);
180 xMultiplier_ = Array(x[0].end() - x[0].begin(), 1.0);
181 xShift_ = Array(x[0].end() - x[0].begin(), 0.0);
182 yMultiplier_ = 1.0;
183 yShift_ = 0.0;
184
185 switch (method_) {
186 case None:
187 break;
188 case MaxAbs: {
189 Array m(x[0].end() - x[0].begin(), 0.0);
190 Real my = 0.0;
191 for (Size i = 0; i < static_cast<Size>(x.end() - x.begin()); ++i) {
192 for (Size j = 0; j < m.size(); ++j) {
193 m[j] = std::max(std::abs(x[i][j]), m[j]);
194 }
195 }
196 for (Size j = 0; j < m.size(); ++j) {
197 if (!QuantLib::close_enough(m[j], 0.0))
198 xMultiplier_[j] = 1.0 / m[j];
199 }
200 for (Size i = 0; i < static_cast<Size>(y.end() - y.begin()); ++i) {
201 my = std::max(std::abs(y[i]), my);
202 }
203 if (!QuantLib::close_enough(my, 0.0))
204 yMultiplier_ = 1.0 / my;
205 break;
206 }
207 case MeanStdDev: {
208 std::vector<accumulator_set<Real, stats<boost::accumulators::tag::mean, boost::accumulators::tag::variance> > > acc(x[0].end() - x[0].begin());
209 for (Size i = 0; i < static_cast<Size>(x.end() - x.begin()); ++i) {
210 for (Size j = 0; j < acc.size(); ++j) {
211 acc[j](x[i][j]);
212 }
213 }
214 for (Size j = 0; j < acc.size(); ++j) {
215 xShift_[j] = -mean(acc[j]);
216 Real tmp = boost::accumulators::variance(acc[j]);
217 if (!QuantLib::close_enough(tmp, 0.0))
218 xMultiplier_[j] = 1.0 / std::sqrt(tmp);
219 }
220 accumulator_set<Real, stats<boost::accumulators::tag::mean, boost::accumulators::tag::variance> > acc2;
221 for (Size i = 0; i < static_cast<Size>(y.end() - y.begin()); ++i) {
222 acc2(y[i]);
223 }
224 yShift_ = -mean(acc2);
225 Real tmp2 = boost::accumulators::variance(acc2);
226 if (!QuantLib::close_enough(tmp2, 0.0))
227 yMultiplier_ = 1.0 / std::sqrt(tmp2);
228 break;
229 }
230 default:
231 QL_FAIL("unknown stabilisation method");
232 break;
233 }
234
235 for (Size i = 0; i < static_cast<Size>(x.end() - x.begin()); ++i) {
236 for (Size j = 0; j < xMultiplier_.size(); ++j) {
237 xData[i][j] = (x[i][j] + xShift_[j]) * xMultiplier_[j];
238 }
239 }
240 for (Size i = 0; i < static_cast<Size>(y.end() - y.begin()); ++i) {
241 yData[i] = (y[i] + yShift_) * yMultiplier_;
242 }
243
244 glls_ = QuantLib::ext::make_shared<GeneralLinearLeastSquares>(xData, yData, v);
245}

Member Data Documentation

◆ a_

Array a_
protected

Definition at line 85 of file stabilisedglls.hpp.

◆ err_

Array err_
protected

Definition at line 85 of file stabilisedglls.hpp.

◆ residuals_

Array residuals_
protected

Definition at line 85 of file stabilisedglls.hpp.

◆ standardErrors_

Array standardErrors_
protected

Definition at line 85 of file stabilisedglls.hpp.

◆ xMultiplier_

Array xMultiplier_
protected

Definition at line 85 of file stabilisedglls.hpp.

◆ xShift_

Array xShift_
protected

Definition at line 85 of file stabilisedglls.hpp.

◆ yMultiplier_

Real yMultiplier_
protected

Definition at line 86 of file stabilisedglls.hpp.

◆ yShift_

Real yShift_
protected

Definition at line 86 of file stabilisedglls.hpp.

◆ method_

Method method_
protected

Definition at line 87 of file stabilisedglls.hpp.

◆ glls_

QuantLib::ext::shared_ptr<GeneralLinearLeastSquares> glls_
protected

Definition at line 88 of file stabilisedglls.hpp.