forked from amaggiulli/QLNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStochasticProcessArray.cs
More file actions
129 lines (109 loc) · 4.25 KB
/
Copy pathStochasticProcessArray.cs
File metadata and controls
129 lines (109 loc) · 4.25 KB
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
/*
Copyright (C) 2008 Siarhei Novik ([email protected])
This file is part of QLNet Project https://github.com/amaggiulli/qlnet
QLNet is free software: you can redistribute it and/or modify it
under the terms of the QLNet license. You should have received a
copy of the license along with this program; if not, license is
available at <https://github.com/amaggiulli/QLNet/blob/develop/LICENSE>.
QLNet is a based on QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
The QuantLib license is 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.
*/
using System;
using System.Collections.Generic;
namespace QLNet
{
//! %Array of correlated 1-D stochastic processes
/*! \ingroup processes */
public class StochasticProcessArray : StochasticProcess
{
protected List<StochasticProcess1D> processes_;
protected Matrix sqrtCorrelation_;
public StochasticProcessArray(List<StochasticProcess1D> processes, Matrix correlation)
{
processes_ = processes;
sqrtCorrelation_ = MatrixUtilitites.pseudoSqrt(correlation, MatrixUtilitites.SalvagingAlgorithm.Spectral);
Utils.QL_REQUIRE(processes.Count != 0, () => "no processes given");
Utils.QL_REQUIRE(correlation.rows() == processes.Count, () =>
"mismatch between number of processes and size of correlation matrix");
for (int i = 0; i < processes_.Count; i++)
processes_[i].registerWith(update);
}
// stochastic process interface
public override int size() { return processes_.Count; }
public override Vector initialValues()
{
Vector tmp = new Vector(size());
for (int i = 0; i < size(); ++i)
tmp[i] = processes_[i].x0();
return tmp;
}
public override Vector drift(double t, Vector x)
{
Vector tmp = new Vector(size());
for (int i = 0; i < size(); ++i)
tmp[i] = processes_[i].drift(t, x[i]);
return tmp;
}
public override Vector expectation(double t0, Vector x0, double dt)
{
Vector tmp = new Vector(size());
for (int i = 0; i < size(); ++i)
tmp[i] = processes_[i].expectation(t0, x0[i], dt);
return tmp;
}
public override Matrix diffusion(double t, Vector x)
{
Matrix tmp = sqrtCorrelation_;
for (int i = 0; i < size(); ++i)
{
double sigma = processes_[i].diffusion(t, x[i]);
for (int j = 0; j < tmp.columns(); j++)
{
tmp[i, j] *= sigma;
}
}
return tmp;
}
public override Matrix covariance(double t0, Vector x0, double dt)
{
Matrix tmp = stdDeviation(t0, x0, dt);
return tmp * Matrix.transpose(tmp);
}
public override Matrix stdDeviation(double t0, Vector x0, double dt)
{
Matrix tmp = sqrtCorrelation_;
for (int i = 0; i < size(); ++i)
{
double sigma = processes_[i].stdDeviation(t0, x0[i], dt);
for (int j = 0; j < tmp.columns(); j++)
{
tmp[i, j] *= sigma;
}
}
return tmp;
}
public override Vector apply(Vector x0, Vector dx)
{
Vector tmp = new Vector(size());
for (int i = 0; i < size(); ++i)
tmp[i] = processes_[i].apply(x0[i], dx[i]);
return tmp;
}
public override Vector evolve(double t0, Vector x0, double dt, Vector dw)
{
Vector dz = sqrtCorrelation_ * dw;
Vector tmp = new Vector(size());
for (int i = 0; i < size(); ++i)
tmp[i] = processes_[i].evolve(t0, x0[i], dt, dz[i]);
return tmp;
}
public override double time(Date d) { return processes_[0].time(d); }
// inspectors
public StochasticProcess1D process(int i) { return processes_[i]; }
public Matrix correlation() { return sqrtCorrelation_ * Matrix.transpose(sqrtCorrelation_); }
}
}