npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
Copulas.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_COPULAS_HH_
2 #define NPSTAT_COPULAS_HH_
3 
4 /*!
5 // \file Copulas.hh
6 //
7 // \brief Concrete copula distributions
8 //
9 // Author: I. Volobouev
10 //
11 // September 2010
12 */
13 
14 #include "npstat/nm/Matrix.hh"
15 
18 
19 namespace npstat {
20  // Forward declarations
21  class NMCombinationSequencer;
22 
23 
24  /** The Gaussian copula */
26  {
27  public:
28  explicit GaussianCopula(const Matrix<double>& covmat);
29  virtual inline ~GaussianCopula() {}
30 
31  double density(const double* x, unsigned dim) const;
32  void unitMap(const double* rnd, unsigned bufLen, double* x) const;
33  inline bool mappedByQuantiles() const {return false;}
34 
35  virtual inline GaussianCopula* clone() const
36  {return new GaussianCopula(*this);}
37  virtual inline gs::ClassId classId() const {return gs::ClassId(*this);}
38  virtual bool write(std::ostream& os) const;
39 
40  static inline const char* classname() {return "npstat::GaussianCopula";}
41  static inline unsigned version() {return 1;}
42  static GaussianCopula* read(const gs::ClassId& id, std::istream& in);
43 
44  protected:
45  virtual inline bool isEqual(const AbsDistributionND& ri) const
46  {
47  const GaussianCopula& o = static_cast<const GaussianCopula&>(ri);
48  return norm_ == o.norm_ && form_ == o.form_ &&
49  sqrCov_ == o.sqrCov_;
50  }
51 
52  private:
53  inline explicit GaussianCopula(unsigned nDim)
54  : AbsDistributionND(nDim) {}
55 
56  Matrix<double> form_;
57  Matrix<double> sqrCov_;
58  double norm_;
59  mutable std::vector<double> buf_;
60  };
61 
62 
63  /**
64  // The Farlie-Gumbel-Morgenstern copula. It is basically defined by its
65  // values at all vertices of the n-dimensional unit cube. All values
66  // inside the cube are obtained by multilinear interpolation. Naturally,
67  // the values at the vertices can not be arbitrary: they must satisfy
68  // certain constraints for the function to be a copula, so the
69  // representation in terms of independent parameters is a bit complicated.
70  // Then those parameters themselves must be selected in such a way that
71  // the function is non-negative in each of the 2^n vertices of the cube,
72  // so this representation is far from ideal. It is described in the book
73  // by R.B. Nelsen, "An introduction to Copulas", 2nd Ed. (2006), page 108.
74  // There should be a better way to do this...
75  */
77  {
78  public:
79  /**
80  // The order of parameters is the following: theta_1_2,
81  // theta_1_3, ..., theta_1_dim, theta_2_3, ..., theta_2_dim, ...
82  // theta_dim-1_dim, theta_1_2_3, ...
83  //
84  // There must be 2^dim - dim - 1 parameters total. Maximum
85  // dimensionality is 31. Invalid parameter set (in particular,
86  // a set resulting in possible negative values of the calculated
87  // copula) will result in std::invalid_argument exception thrown.
88  */
89  FGMCopula(unsigned dim, const double* params, unsigned nParams);
90  virtual inline ~FGMCopula() {}
91 
92  double density(const double* x, unsigned dim) const;
93  void unitMap(const double* rnd, unsigned bufLen, double* x) const;
94  inline bool mappedByQuantiles() const {return true;}
95 
96  virtual inline FGMCopula* clone() const {return new FGMCopula(*this);}
97  virtual inline gs::ClassId classId() const {return gs::ClassId(*this);}
98  virtual bool write(std::ostream& os) const;
99 
100  static inline const char* classname() {return "npstat::FGMCopula";}
101  static inline unsigned version() {return 1;}
102  static FGMCopula* read(const gs::ClassId& id, std::istream& in);
103 
104  protected:
105  virtual inline bool isEqual(const AbsDistributionND& ri) const
106  {
107  const FGMCopula& o = static_cast<const FGMCopula&>(ri);
108  return cornerValues_ == o.cornerValues_;
109  }
110 
111  private:
112  inline explicit FGMCopula(unsigned nDim) : AbsDistributionND(nDim) {}
113 
114  static double interpolate(const double* corners,
115  const double* x, unsigned dim);
116  static double density0(NMCombinationSequencer* sequencers,
117  const double* par, const double* x,
118  unsigned dim);
119 
120  std::vector<double> cornerValues_;
121  };
122 
123 
124  /** The Student's-t copula */
125  class TCopula : public AbsDistributionND
126  {
127  public:
128  TCopula(const Matrix<double>& covmat, double nDegreesOfFreedom);
129  virtual inline ~TCopula() {}
130 
131  double density(const double* x, unsigned dim) const;
132  void unitMap(const double* rnd, unsigned bufLen, double* x) const;
133  inline bool mappedByQuantiles() const {return false;}
134  inline double nDegreesOfFreedom() const {return nDoF_;}
135 
136  virtual inline TCopula* clone() const {return new TCopula(*this);}
137 
138  virtual inline gs::ClassId classId() const {return gs::ClassId(*this);}
139  virtual bool write(std::ostream& os) const;
140 
141  static inline const char* classname() {return "npstat::TCopula";}
142  static inline unsigned version() {return 1;}
143  static TCopula* read(const gs::ClassId& id, std::istream& in);
144 
145  protected:
146  virtual inline bool isEqual(const AbsDistributionND& ri) const
147  {
148  const TCopula& o = static_cast<const TCopula&>(ri);
149  return nDoF_ == o.nDoF_ && norm_ == o.norm_ && form_ == o.form_ &&
150  sqrCov_ == o.sqrCov_ && power_ == o.power_;
151  }
152 
153  private:
154  inline TCopula(unsigned nDim, double ndof)
155  : AbsDistributionND(nDim), t_(0.0, 1.0, ndof) {}
156 
157  Matrix<double> form_;
158  Matrix<double> sqrCov_;
159  double nDoF_;
160  double norm_;
161  double power_;
162  StudentsT1D t_;
163  mutable std::vector<double> buf_;
164  };
165 
166 
167  // Archimedean copulas -- to be implemented
168 }
169 
170 #endif // NPSTAT_COPULAS_HH_
Interface definition for multivariate continuous statistical distributions.
A number of useful 1-d continuous statistical distributions.
Template matrix class.
Definition: AbsDistributionND.hh:26
unsigned dim() const
Definition: AbsDistributionND.hh:51
Definition: Copulas.hh:77
FGMCopula(unsigned dim, const double *params, unsigned nParams)
virtual gs::ClassId classId() const
Definition: Copulas.hh:97
void unitMap(const double *rnd, unsigned bufLen, double *x) const
double density(const double *x, unsigned dim) const
bool mappedByQuantiles() const
Definition: Copulas.hh:94
virtual FGMCopula * clone() const
Definition: Copulas.hh:96
Definition: Copulas.hh:26
bool mappedByQuantiles() const
Definition: Copulas.hh:33
double density(const double *x, unsigned dim) const
virtual gs::ClassId classId() const
Definition: Copulas.hh:37
virtual GaussianCopula * clone() const
Definition: Copulas.hh:35
void unitMap(const double *rnd, unsigned bufLen, double *x) const
Definition: Copulas.hh:126
void unitMap(const double *rnd, unsigned bufLen, double *x) const
virtual gs::ClassId classId() const
Definition: Copulas.hh:138
double density(const double *x, unsigned dim) const
bool mappedByQuantiles() const
Definition: Copulas.hh:133
virtual TCopula * clone() const
Definition: Copulas.hh:136
Definition: AbsArrayProjector.hh:14