File: LaplacianGridKernelRegression.cpp

package info (click to toggle)
stopt 5.12%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 8,860 kB
  • sloc: cpp: 70,456; python: 5,950; makefile: 72; sh: 57
file content (454 lines) | stat: -rw-r--r-- 14,731 bytes parent folder | download | duplicates (3)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// Copyright (C) 2020 EDF
// Copyright (C) 2020 CSIRO Data61
// All Rights Reserved
// This code is published under the GNU Lesser General Public License (GNU LGPL)
#include <Eigen/SVD>
#include <Eigen/Cholesky>
#include "StOpt/core/utils/constant.h"
#include "StOpt/core/grids/RegularSpaceGrid.h"
#include "StOpt/core/grids/LinearInterpolator.h"
#include "StOpt/regression/LaplacianGridKernelRegression.h"
#include "StOpt/regression/fastLaplacianKDE.h"

using namespace std ;
using namespace Eigen ;

namespace StOpt
{

LaplacianGridKernelRegression::LaplacianGridKernelRegression(const bool &p_bZeroDate,
        const ArrayXXd  &p_particles,
        const ArrayXd   &p_h,
        const double   &p_coefNbGridPoint,
        const bool &p_bLinear
                                                            ):
    BaseRegression(p_bZeroDate, p_particles, false), m_h(p_h), m_coefNbGridPoint(p_coefNbGridPoint), m_z(p_h.size()), m_bLinear(p_bLinear)
{
    rectiConst();
}

LaplacianGridKernelRegression::LaplacianGridKernelRegression(const bool &p_bZeroDate,
        const ArrayXd   &p_h,
        const double   &p_coefNbGridPoint,
        const bool &p_bLinear,
        const std::vector< std::shared_ptr<Eigen::ArrayXd> >   &p_z):
    BaseRegression(p_bZeroDate, false), m_h(p_h), m_coefNbGridPoint(p_coefNbGridPoint), m_z(p_z), m_bLinear(p_bLinear)
{
}

LaplacianGridKernelRegression::LaplacianGridKernelRegression(const ArrayXd   &p_h,
        const double   &p_coefNbGridPoint,
        const bool &p_bLinear):
    BaseRegression(false), m_h(p_h), m_coefNbGridPoint(p_coefNbGridPoint), m_z(p_h.size()), m_bLinear(p_bLinear)
{
}


void LaplacianGridKernelRegression::updateSimulations(const bool &p_bZeroDate, const ArrayXXd &p_particles)
{
    BaseRegression::updateSimulationsBase(p_bZeroDate, p_particles);

    // calculate the grid associated
    rectiConst();
}


void   LaplacianGridKernelRegression::rectiConst()
{
    // number of points per direction
    int nbPt = static_cast<int>(pow(m_particles.cols() * m_coefNbGridPoint, 1. / m_particles.rows()));
    for (int id = 0; id < m_particles.rows() ; ++id)
    {
        ArrayXd zDim = ArrayXd::LinSpaced(nbPt, m_particles.row(id).minCoeff() + tiny, m_particles.row(id).maxCoeff() - tiny);
        m_z[id] = make_shared<ArrayXd>(zDim);
    }
    m_nbPtZ  = static_cast<int>(pow(nbPt, m_particles.rows()));
}

ArrayXXd  LaplacianGridKernelRegression::kernelCoeffCalcConst(const ArrayXXd &p_fToRegress) const
{
    // number of functions to calculate for regressions
    int nbFuncReg = 1;
    int nbFuncSecMem =  p_fToRegress.rows()   ;

    // store all weights for kernel
    ArrayXXd y(nbFuncReg + nbFuncSecMem, p_fToRegress.cols());
    for (int is = 0; is <  p_fToRegress.cols(); ++is)
    {
        int iloc = 0;
        // lower triangular matrix
        y(iloc++, is) = 1.;
        for (int ifunc = 0; ifunc < p_fToRegress.rows(); ++ifunc)
        {
            y(iloc++, is) = p_fToRegress(ifunc, is);
        }
    }

    // kernel coefficients on grid
    ArrayXXd coeffOnZ = FastSumRegressionTerms(m_particles,  m_z,  m_h,  y);

    // now calculate the regressed values on the grid
    ArrayXXd regOnZ(p_fToRegress.rows(), m_nbPtZ);

    for (int ipt = 0 ; ipt < m_nbPtZ; ++ipt)
    {
        for (int ifunc = 0 ; ifunc < p_fToRegress.rows(); ++ifunc)
        {
            regOnZ(ifunc, ipt) = coeffOnZ(ifunc + 1, ipt) / coeffOnZ(0, ipt);
        }
    }
    return regOnZ;
}

ArrayXXd  LaplacianGridKernelRegression::kernelCoeffCalcLinear(const ArrayXXd &p_fToRegress) const
{
    // dimension
    int nD = m_particles.rows();
    // number of functions to calculate for regressions
    int nbFuncReg = (nD + 1) * (nD + 2) / 2;
    int nbFuncSecMem = (nD + 1) * p_fToRegress.rows()   ;

    // store all weights for kernel
    ArrayXXd y(nbFuncReg + nbFuncSecMem, p_fToRegress.cols());
    for (int is = 0; is <  p_fToRegress.cols(); ++is)
    {
        int iloc = 0;
        // lower triangular matrix
        y(iloc++, is) = 1.;
        for (int id = 0; id < nD; ++id)
        {
            y(iloc++, is) = m_particles(id, is);
            for (int idd = 0; idd <= id; ++idd)
                y(iloc++, is) = m_particles(id, is) * m_particles(idd, is);
        }
        for (int ifunc = 0; ifunc < p_fToRegress.rows(); ++ifunc)
        {
            y(iloc++, is) = p_fToRegress(ifunc, is);
            for (int id = 0; id < nD ; ++id)
                y(iloc++, is) = p_fToRegress(ifunc, is) * m_particles(id, is);
        }
    }

    // kernel coefficients on grid
    ArrayXXd coeffOnZ = FastSumRegressionTerms(m_particles,  m_z,  m_h,  y);

    // now calculate the regressed values on the grid
    ArrayXXd regOnZ(p_fToRegress.rows(), m_nbPtZ);

    // for regressions
    MatrixXd  matA(1 + nD, 1 + nD);
    VectorXd  vecB(1 + nD);

    ArrayXi coord =  ArrayXi::Zero(nD);

    for (int ipt = 0 ; ipt < m_nbPtZ; ++ipt)
    {
        // create regression matrix
        int iloc = 0;
        for (int id = 0; id <= nD; ++id)
            for (int idd = 0; idd <= id; ++idd)
                matA(id, idd) = coeffOnZ(iloc++, ipt);
        for (int id = 0; id <= nD; ++id)
            for (int idd = id + 1; idd <= nD; ++idd)
                matA(id, idd) =  matA(idd, id);
        // second member and inverse
        // inverse
        LLT<MatrixXd>  lltA(matA);
        for (int ifunc = 0 ; ifunc < p_fToRegress.rows() ; ++ifunc)
        {
            for (int id = 0; id <= nD; ++id)
                vecB(id) = coeffOnZ(iloc++, ipt);
            VectorXd coeff = lltA.solve(vecB);
            regOnZ(ifunc, ipt) = coeff(0);
            for (int id  = 0; id < nD; ++id)
                regOnZ(ifunc, ipt)  += coeff(id + 1) * (*m_z[id])(coord(id));
        }
        // update coordinates
        for (int id = 0; id < nD; ++id)
        {
            if (coord(id) < m_z[id]->size() - 1)
            {
                coord(id) += 1;
                break;
            }
            else
            {
                coord(id) = 0;
            }
        }
    }
    return regOnZ;
}


ArrayXXd  LaplacianGridKernelRegression::interpolateAtSample(const ArrayXXd &p_regOnGrid) const
{

    int nD = m_z.size() ;
    // create a grid
    Eigen::ArrayXd lowValues(nD);
    for (int i = 0; i < nD; ++i)
        lowValues(i) = (*m_z[i])[0];
    Eigen::ArrayXd step(nD);
    for (int i = 0; i < nD; ++i)
        step(i) = ((*m_z[i])[m_z[i]->size() - 1] - (*m_z[i])[0]) / (m_z[i]->size() - 1);
    Eigen::ArrayXi  nbStep(nD);
    for (int i = 0; i < nD; ++i)
        nbStep(i) = m_z[i]->size() - 1 ;

    // regular
    RegularSpaceGrid regGrid(lowValues, step, nbStep);

    // for return
    ArrayXXd regressRet(p_regOnGrid.rows(), m_particles.cols());

    // transpose once for all
    ArrayXXd regTrans = p_regOnGrid.transpose();

    // now interpolate
    for (int isim = 0; isim < m_particles.cols(); ++isim)
    {
        Eigen::ArrayXd point = m_particles.col(isim);

        // create the interpolator
        LinearInterpolator  regLin(&regGrid, point);
        for (int j = 0; j < p_regOnGrid.rows(); ++j)
        {
            regressRet(j, isim) = regLin.apply(regTrans.col(j));
        }
    }
    return regressRet;
}


double   LaplacianGridKernelRegression::interpolateAtPoint(const ArrayXd   &p_coordinates,  const ArrayXd &p_regOnGrid) const
{

    int nD = m_z.size();
    // create a grid
    Eigen::ArrayXd lowValues(nD);
    for (int i = 0; i < nD; ++i)
        lowValues(i) = (*m_z[i])[0];
    Eigen::ArrayXd step(nD);
    for (int i = 0; i < nD; ++i)
        step(i) = ((*m_z[i])[m_z[i]->size() - 1] - (*m_z[i])[0]) / (m_z[i]->size() - 1);
    Eigen::ArrayXi  nbStep(nD);
    for (int i = 0; i < nD; ++i)
        nbStep(i) = m_z[i]->size() - 1 ;

    // regular
    RegularSpaceGrid regGrid(lowValues, step, nbStep);

    // create the interpolator
    LinearInterpolator  regLin(&regGrid, p_coordinates);

    return regLin.apply(p_regOnGrid);
}


ArrayXXd LaplacianGridKernelRegression::regressFunction(const ArrayXXd &p_fToRegress) const
{

    // calculate the coefficients on the grid
    ArrayXXd  coefOnGrid = ((m_bLinear == true) ? kernelCoeffCalcLinear(p_fToRegress) :  kernelCoeffCalcConst(p_fToRegress));
    // interpolate at sample points
    return interpolateAtSample(coefOnGrid);
}



ArrayXd  LaplacianGridKernelRegression::getCoordBasisFunction(const ArrayXd &p_fToRegress) const
{
    if (!BaseRegression::m_bZeroDate)
    {
        const ArrayXXd  fToRegress = Map<const ArrayXXd>(p_fToRegress.data(), 1, p_fToRegress.size()) ;
        ArrayXXd  regressed = ((m_bLinear) ? kernelCoeffCalcLinear(fToRegress) : kernelCoeffCalcConst(fToRegress));
        ArrayXd toReturn = Map<ArrayXd>(regressed.data(), regressed.size());
        return toReturn;
    }
    else
    {
        ArrayXd retAverage(1);
        retAverage(0) = p_fToRegress.mean();
        return retAverage;
    }
}

ArrayXXd  LaplacianGridKernelRegression::getCoordBasisFunctionMultiple(const ArrayXXd &p_fToRegress) const
{
    if (!BaseRegression::m_bZeroDate)
    {
        if (m_bLinear)
        {
            return kernelCoeffCalcLinear(p_fToRegress);
        }
        else
        {
            return kernelCoeffCalcConst(p_fToRegress);
        }
    }
    else
    {
        ArrayXXd retAverage(p_fToRegress.rows(), 1);
        for (int nsm = 0; nsm <  p_fToRegress.rows(); ++nsm)
            retAverage.row(nsm).setConstant(p_fToRegress.row(nsm).mean());
        return retAverage;
    }
}

ArrayXd  LaplacianGridKernelRegression::getAllSimulations(const ArrayXd &p_fToRegress) const
{
    if (!BaseRegression::m_bZeroDate)
    {
        const ArrayXXd  fToRegress = Map<const ArrayXXd>(p_fToRegress.data(), 1, p_fToRegress.size()) ;
        ArrayXXd  regressed = regressFunction(fToRegress);
        ArrayXd toReturn = Map<ArrayXd>(regressed.data(), regressed.size());
        return toReturn;
    }
    else
    {
        return ArrayXd::Constant(p_fToRegress.size(), p_fToRegress.mean());
    }
}

ArrayXXd  LaplacianGridKernelRegression::getAllSimulationsMultiple(const ArrayXXd &p_fToRegress) const
{
    if (!BaseRegression::m_bZeroDate)
    {
        return regressFunction(p_fToRegress);
    }
    else
    {
        ArrayXXd ret(p_fToRegress.rows(), p_fToRegress.cols());
        for (int ism = 0; ism < p_fToRegress.rows(); ++ism)
            ret.row(ism).setConstant(p_fToRegress.row(ism).mean());
        return ret;
    }
}


ArrayXd LaplacianGridKernelRegression::reconstruction(const ArrayXd   &p_basisCoefficients) const
{
    if (!BaseRegression::m_bZeroDate)
    {
        const ArrayXXd basisCoefficients  = Map<const ArrayXXd>(p_basisCoefficients.data(), 1, p_basisCoefficients.size()) ;
        // basis function are here regressed values but on the grid, so interpolate
        ArrayXXd retD = interpolateAtSample(basisCoefficients);
        return  Map<ArrayXd>(retD.data(), retD.size());
    }
    else
    {
        return ArrayXd::Constant(m_particles.cols(), p_basisCoefficients(0));
    }
}


ArrayXXd LaplacianGridKernelRegression::reconstructionMultiple(const ArrayXXd   &p_basisCoefficients) const
{
    if (!BaseRegression::m_bZeroDate)
    {
        // basis function are here regressed values but on the grid
        return interpolateAtSample(p_basisCoefficients);
    }
    else
    {
        ArrayXXd retValue(p_basisCoefficients.rows(), m_particles.cols());
        for (int nsm = 0; nsm < p_basisCoefficients.rows(); ++nsm)
            retValue.row(nsm).setConstant(p_basisCoefficients(nsm, 0));
        return retValue ;
    }
}

double  LaplacianGridKernelRegression::reconstructionASim(const int &p_isim, const ArrayXd   &p_basisCoefficients) const
{
    double ret ;
    if (!BaseRegression::m_bZeroDate)
    {
        ret =  interpolateAtPoint(m_particles.col(p_isim), p_basisCoefficients);
    }
    else
    {
        ret = p_basisCoefficients(0);
    }
    return ret ;
}

double LaplacianGridKernelRegression::getValue(const ArrayXd   &p_coordinates,
        const ArrayXd   &p_coordBasisFunction)  const
{
    double ret  ;
    if (!BaseRegression::m_bZeroDate)
    {
        return  interpolateAtPoint(p_coordinates, p_coordBasisFunction);
    }
    else
        ret =  p_coordBasisFunction(0);
    return ret ;
}


double LaplacianGridKernelRegression::getAValue(const ArrayXd &p_coordinates,  const ArrayXd &p_ptOfStock,
        const vector< shared_ptr<InterpolatorSpectral> > &p_interpFuncBasis) const
{
    if (!BaseRegression::m_bZeroDate)
    {
        // coordinates in Z grid
        ArrayXi coordMin(p_coordinates.size());
        for (int id = 0; id <  p_coordinates.size(); ++id)
        {
            if (p_coordinates(id) > (*m_z[id])(m_z[id]->size() - 1))
            {
                coordMin(id) = m_z[id]->size() - 2;
            }
            else
            {
                coordMin(id) =  m_z[id]->size() - 2;
                while ((*m_z[id])(coordMin(id)) > p_coordinates(id))
                {
                    coordMin(id) -= 1;
                    if (coordMin(id) == 0)
                        break;
                }
            }
        }
        // weight
        Eigen::ArrayXd weightPerDim(p_coordinates.size());
        for (int id = 0; id < p_coordinates.size(); ++id)
        {
            // weights have to be positive : so force in case is nearly 0 (rounding error)
            // they have to be below 1
            weightPerDim(id) = std::min(std::max(0., (p_coordinates(id) - (*m_z[id])(coordMin(id))) / ((*m_z[id])(coordMin(id) + 1) - (*m_z[id])(coordMin(id)))), 1.);
        }
        int nbWeigth = (0x01 << p_coordinates.size());
        double retInterp = 0.;
        ArrayXi iCoord(p_coordinates.size());
        // iterate on all vertex of the hypercube
        for (int j = 0 ; j < nbWeigth ; ++j)
        {
            unsigned int ires = j ;
            double weightLocal  = 1. ;
            for (int id = p_coordinates.size() - 1 ; id >= 0  ; --id)
            {
                unsigned int idec = (ires >> id) ;
                iCoord(id) = coordMin(id) + idec;
                weightLocal *= (1 - weightPerDim(id)) * (1 - idec) + weightPerDim(id) * idec ;
                ires -= (idec << id);
            }
            // global coordinate in grid
            int iCoorGlob = iCoord(0);
            int idec = m_z[0]->size();
            for (int id = 1; id < p_coordinates.size(); ++id)
            {
                iCoorGlob += iCoord(id) * idec ;
                idec *= m_z[id]->size();
            }
            retInterp += weightLocal * p_interpFuncBasis[iCoorGlob]->apply(p_ptOfStock);
        }
        return retInterp;
    }
    else
    {
        return p_interpFuncBasis[0]->apply(p_ptOfStock);
    }
}
}