/src/quantlib/ql/models/volatility/constantestimator.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2006 Joseph Wang |
5 | | |
6 | | This file is part of QuantLib, a free-software/open-source library |
7 | | for financial quantitative analysts and developers - http://quantlib.org/ |
8 | | |
9 | | QuantLib is free software: you can redistribute it and/or modify it |
10 | | under the terms of the QuantLib license. You should have received a |
11 | | copy of the license along with this program; if not, please email |
12 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
13 | | <http://quantlib.org/license.shtml>. |
14 | | |
15 | | This program is distributed in the hope that it will be useful, but WITHOUT |
16 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
17 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
18 | | */ |
19 | | |
20 | | #include <ql/models/volatility/constantestimator.hpp> |
21 | | |
22 | | namespace QuantLib { |
23 | | TimeSeries<Volatility> |
24 | 0 | ConstantEstimator::calculate(const TimeSeries<Volatility>& volatilitySeries) { |
25 | 0 | TimeSeries<Volatility> retval; |
26 | 0 | const std::vector<Volatility> u = volatilitySeries.values(); |
27 | 0 | TimeSeries<Volatility>::const_iterator prev, next, cur, start; |
28 | 0 | cur = volatilitySeries.begin(); |
29 | 0 | std::advance(cur, size_); |
30 | | // ICK. This could probably be made a lot more efficient |
31 | 0 | for (Size i=size_; i < volatilitySeries.size(); i++) { |
32 | 0 | Size j; |
33 | 0 | Real sumu2=0.0, sumu=0.0; |
34 | 0 | for (j=i-size_; j <i; j++) { |
35 | 0 | sumu += u[j]; |
36 | 0 | sumu2 += u[j]*u[j]; |
37 | 0 | } |
38 | 0 | Real s = std::sqrt(sumu2/(Real)size_ - sumu*sumu / (Real) size_ / |
39 | 0 | (Real) (size_+1)); |
40 | 0 | retval[cur->first] = s; |
41 | 0 | ++cur; |
42 | 0 | } |
43 | 0 | return retval; |
44 | 0 | } |
45 | | |
46 | | } |
47 | | |