File: BaseRegression.h

package info (click to toggle)
stopt 6.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,264 kB
  • sloc: cpp: 75,778; python: 6,012; makefile: 72; sh: 57
file content (221 lines) | stat: -rw-r--r-- 9,947 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
// Copyright (C) 2016, 2017 EDF
// All Rights Reserved
// This code is published under the GNU Lesser General Public License (GNU LGPL)
#ifndef BASEREGRESSION_H
#define BASEREGRESSION_H
#include <memory>
#include <vector>
#include <iostream>
#include <Eigen/Dense>
#include <Eigen/SVD>
#include "StOpt/core/grids/InterpolatorSpectral.h"

/** \file BaseRegression.h
 *  \brief Base class to define regressor for stochastic optimization by Monte Carlo
 *  \author Xavier Warin
 */
namespace StOpt
{
/// \class BaseRegression BaseRegression.h
/// Base class for regression
class BaseRegression
{
protected :

    bool m_bZeroDate ;                    ///< Is the regression date zero ?
    bool m_bRotationAndRescale ; ///< do we rescale particles and do a rotation with SVD on data
    Eigen::ArrayXd  m_meanX ; ///< store  scaled factor in each direction (average of particles values in each direction)
    Eigen::ArrayXd  m_etypX ; ///< store  scaled factor in each direction (standard deviation of particles in each direction)
    Eigen::MatrixXd m_svdMatrix ; ///< svd matrix transposed  used to transform particles
    Eigen::ArrayXd  m_sing      ; ///< singular values associated to SVD
    Eigen::ArrayXXd m_particles;  ///< Particles used to regress: first dimension  : dimension of the problem , second dimension : the  number of particles. These particles are rescaled and a rotation with SVD is achieved to avoid degeneracy in case of high correlations

    // rotation for data and rescaling
    void preProcessData();

public :

    /// \brief Default constructor
    BaseRegression();

    /// \brief Default destructor
    virtual ~BaseRegression() {}

    /// \brief Default constructor
    BaseRegression(const bool &p_bRotationAndRescale);

    /// \brief Constructor storing the particles
    /// \param  p_bZeroDate        first date is 0?
    /// \param  p_particles        particles used for the meshes.
    ///                            First dimension  : dimension of the problem,
    ///                            second dimension : the  number of particles
    /// \param  p_bRotationAndRescale do we rescale particle
    BaseRegression(const bool &p_bZeroDate, const Eigen::ArrayXXd &p_particles, const bool &p_bRotationAndRescale);

    /// \brief Constructor used in simulation, no rotation
    /// \param  p_bZeroDate    first date is 0?
    /// \param  p_bRotationAndRescale do we rescale particle
    BaseRegression(const bool &p_bZeroDate, const bool &p_bRotationAndRescale);


    /// \brief Last constructor used in simulation
    /// \param  p_bZeroDate    first date is 0?
    /// \param  p_meanX            scaled factor in each direction (average of particles values in each direction)
    /// \param  p_etypX            scaled factor in each direction (standard deviation of particles in each direction)
    /// \param  p_svdMatrix        svd matrix transposed  used to transform particles
    /// \param  p_bRotationAndRescale do we rescale particle

    BaseRegression(const bool &p_bZeroDate, const   Eigen::ArrayXd &p_meanX, const   Eigen::ArrayXd   &p_etypX, const   Eigen::MatrixXd   &p_svdMatrix, const bool &p_bRotationAndRescale);

    /// \brief Copy constructor
    /// \param p_object  object to copy
    BaseRegression(const BaseRegression &p_object);

    /// \brief update the particles used in regression  and construct the matrices
    /// \param  p_bZeroDate    first date is 0?
    /// \param  p_particles    particles used for the meshes.
    ///                        Firs dimension  : dimension of the problem,
    ///                        second dimension : the  number of particles
    void updateSimulationsBase(const bool &p_bZeroDate, const Eigen::ArrayXXd &p_particles);

    /// \brief Get some local accessors
    ///@{
    virtual inline Eigen::ArrayXXd  getParticles() const
    {
        return m_particles ;
    }

    /// \brief Get bRotationAndRescale
    virtual inline bool getBRotationAndRescale() const
    {
        return m_bRotationAndRescale ;
    }

    /// \brief Get average of simulation per dimension
    virtual inline Eigen::ArrayXd getMeanX() const
    {
        return m_meanX;
    }

    /// \brief get standard deviation per dimension
    virtual inline Eigen::ArrayXd getEtypX() const
    {
        return m_etypX;
    }

    /// \brief get back the SVD matrix used for rescaling particles
    virtual inline Eigen::MatrixXd getSvdMatrix() const
    {
        return m_svdMatrix;
    }

    /// \brief get back singular values
    virtual inline Eigen::ArrayXd getSing() const
    {
        return m_sing;
    }

    /// \brief Get dimension of the problem
    virtual inline int getDimension() const
    {
        return m_particles.rows();
    }

    /// \brief Get the number of simulations
    virtual inline int  getNbSimul()const
    {
        return m_particles.cols() ;
    }

    /// \brief get back particle by its number
    /// \param p_iPart   particle number
    /// \return the particle (if no particle, send back an empty array)
    virtual Eigen::ArrayXd getParticle(const int &p_iPart) const;

    /// \brief get the number of basis functions
    virtual  int getNumberOfFunction() const = 0 ;

    ///@}
    /// \brief Constructor storing the particles
    /// \brief update the particles used in regression  and construct the matrices
    /// \param  p_bZeroDate    first date is 0?
    /// \param  p_particles    particles used for the meshes.
    ///                        First dimension  : dimension of the problem,
    ///                        second dimension : the  number of particles
    virtual void updateSimulations(const bool &p_bZeroDate, const Eigen::ArrayXXd  &p_particles) = 0 ;

    /// \brief conditional expectation basis function coefficient calculation
    /// \param  p_fToRegress  function to regress associated to each simulation used in optimization
    /// \return regression coordinates on the basis  (size : number of meshes multiplied by the dimension plus one)
    /// @{
    virtual Eigen::ArrayXd getCoordBasisFunction(const Eigen::ArrayXd &p_fToRegress) const = 0;
    ///@}
    /// \brief conditional expectation basis function coefficient calculation for multiple functions to regress
    /// \param  p_fToRegress  function to regress associated to each simulation used in optimization (size : number of functions to regress \times the number of Monte Carlo simulations)
    /// \return regression coordinates on the basis  (size :  number of function to regress  \times number of meshes multiplied by the dimension plus one)
    /// @{
    virtual Eigen::ArrayXXd getCoordBasisFunctionMultiple(const Eigen::ArrayXXd &p_fToRegress) const = 0 ;
    ///@}

    /// \brief conditional expectation calculation
    /// \param  p_fToRegress  simulations  to regress used in optimization
    /// \return regressed value function
    /// @{
    virtual Eigen::ArrayXd getAllSimulations(const Eigen::ArrayXd &p_fToRegress) const = 0;
    virtual Eigen::ArrayXXd getAllSimulationsMultiple(const Eigen::ArrayXXd &p_fToRegress) const = 0;
    ///@}

    /// \brief Use basis functions to reconstruct the solution
    /// \param p_basisCoefficients basis coefficients
    ///@{
    virtual Eigen::ArrayXd reconstruction(const Eigen::ArrayXd   &p_basisCoefficients) const = 0 ;
    virtual Eigen::ArrayXXd reconstructionMultiple(const Eigen::ArrayXXd   &p_basisCoefficients) const = 0;
    /// @}

    /// \brief use basis function to reconstruct a given simulation
    /// \param p_isim               simulation number
    /// \param p_basisCoefficients  basis coefficients to reconstruct a given conditional expectation
    virtual double reconstructionASim(const int &p_isim, const Eigen::ArrayXd   &p_basisCoefficients) const = 0 ;

    /// \brief conditional expectation reconstruction
    /// \param  p_coordinates        coordinates to interpolate (uncertainty sample)
    /// \param  p_coordBasisFunction regression coordinates on the basis  (size: number of meshes multiplied by the dimension plus one)
    /// \return regressed value function reconstructed for each simulation
    virtual double getValue(const Eigen::ArrayXd   &p_coordinates,
                            const Eigen::ArrayXd   &p_coordBasisFunction) const = 0;

    /// \brief conditional expectation reconstruction for a lot of simulations
    /// \param  p_coordinates        coordinates to interpolate (uncertainty sample) size uncertainty dimension by number of samples
    /// \param  p_coordBasisFunction regression coordinates on the basis  (size: number of meshes multiplied by the dimension plus one)
    /// \return regressed value function reconstructed for each simulation
    Eigen::ArrayXd  getValues(const Eigen::ArrayXXd   &p_coordinates,
                              const Eigen::ArrayXd   &p_coordBasisFunction) const
    {
        Eigen::ArrayXd valRet(p_coordinates.cols());
        for (int is  = 0; is < p_coordinates.cols(); ++is)
            valRet(is) = getValue(p_coordinates.col(is), p_coordBasisFunction);
        return valRet;
    }

    /// \brief permits to reconstruct a function with basis functions coefficients values given on a grid
    /// \param  p_coordinates          coordinates  (uncertainty sample)
    /// \param  p_ptOfStock            grid point
    /// \param  p_interpFuncBasis      spectral interpolator to interpolate the basis functions  coefficients used in regression on the grid (given for each basis function)
    virtual double getAValue(const Eigen::ArrayXd &p_coordinates,  const Eigen::ArrayXd &p_ptOfStock,
                             const std::vector< std::shared_ptr<InterpolatorSpectral> > &p_interpFuncBasis) const = 0;

    /// \brief is the regression date zero
    inline bool getBZeroDate() const
    {
        return m_bZeroDate;
    }

    /// \brief Clone the regressor
    virtual std::shared_ptr<BaseRegression> clone() const = 0 ;


};

}

#endif