QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
constantestimator.cpp
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
22namespace QuantLib {
23 TimeSeries<Volatility>
26 const std::vector<Volatility> u = volatilitySeries.values();
27 TimeSeries<Volatility>::const_iterator prev, next, cur, start;
28 cur = volatilitySeries.begin();
29 std::advance(cur, size_);
30 // ICK. This could probably be made a lot more efficient
31 for (Size i=size_; i < volatilitySeries.size(); i++) {
32 Size j;
33 Real sumu2=0.0, sumu=0.0;
34 for (j=i-size_; j <i; j++) {
35 sumu += u[j];
36 sumu2 += u[j]*u[j];
37 }
38 Real s = std::sqrt(sumu2/(Real)size_ - sumu*sumu / (Real) size_ /
39 (Real) (size_+1));
40 retval[cur->first] = s;
41 ++cur;
42 }
43 return retval;
44 }
45
46}
47
TimeSeries< Volatility > calculate(const TimeSeries< Volatility > &) override
Container for historical data.
Definition: timeseries.hpp:51
Container::const_iterator const_iterator
Definition: timeseries.hpp:110
const_iterator begin() const
Definition: timeseries.hpp:158
std::vector< T > values() const
returns the historical data
Definition: timeseries.hpp:349
Size size() const
returns the number of historical data including null ones
Definition: timeseries.hpp:307
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35