Skip to content

Commit d698fe6

Browse files
committed
VineCopulaFactory: Select native
1 parent c092943 commit d698fe6

File tree

6 files changed

+60
-10
lines changed

6 files changed

+60
-10
lines changed

lib/src/VineCopula.cxx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
*/
2121
#include "otvine/VineCopula.hxx"
2222
#include <openturns/PersistentObjectFactory.hxx>
23+
#include <openturns/ClaytonCopula.hxx>
24+
#include <openturns/GumbelCopula.hxx>
25+
#include <openturns/FrankCopula.hxx>
2326

2427
#include <vinecopulib.hpp>
2528

@@ -137,6 +140,28 @@ String VineCopula::__repr__() const
137140
return oss;
138141
}
139142

143+
Distribution VineCopula::asNative() const
144+
{
145+
if (p_vinecop_->get_dim() != 2)
146+
throw NotYetImplementedException(HERE) << "dim is not 2";
147+
std::vector<std::vector<vinecopulib::Bicop> > pairs = p_vinecop_->get_all_pair_copulas();
148+
if (pairs.size() != 1 && pairs[0].size() != 1)
149+
throw NotYetImplementedException(HERE) << "tree contains more than one node";
150+
vinecopulib::Bicop bicop = pairs[0][0];
151+
int rotation = bicop.get_rotation();
152+
if (rotation != 0)
153+
throw NotYetImplementedException(HERE) << "rotation is not 0";
154+
const Scalar theta = bicop.get_parameters()(0, 0);
155+
if (bicop.get_family_name() == "Clayton")
156+
return ClaytonCopula(theta);
157+
else if (bicop.get_family_name() == "Frank")
158+
return FrankCopula(theta);
159+
else if (bicop.get_family_name() == "Gumbel")
160+
return GumbelCopula(theta);
161+
else
162+
throw NotYetImplementedException(HERE) << "unknown family: " << bicop.get_family_name();
163+
}
164+
140165
/* Method save() stores the object through the StorageManager */
141166
void VineCopula::save(Advocate & adv) const
142167
{

lib/src/VineCopulaFactory.cxx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ VineCopulaFactory * VineCopulaFactory::clone() const
4747
}
4848

4949
/* example of a func that return a point squared. */
50-
Distribution VineCopulaFactory::build(const Sample & sample) const
50+
VineCopula VineCopulaFactory::buildAsNative(const Sample & sample) const
5151
{
5252
const UnsignedInteger dimension = sample.getDimension();
5353
const UnsignedInteger size = sample.getSize();
@@ -56,10 +56,23 @@ Distribution VineCopulaFactory::build(const Sample & sample) const
5656
for (UnsignedInteger j = 0; j < dimension; ++ j)
5757
data(i, j) = sample(i, j);
5858
Pointer <vinecopulib::Vinecop> p_vinecop = new vinecopulib::Vinecop(dimension);
59-
p_vinecop->select(data);
59+
std::vector<vinecopulib::BicopFamily> family_set;
60+
#if 1
61+
family_set = {
62+
vinecopulib::BicopFamily::clayton,
63+
vinecopulib::BicopFamily::gumbel,
64+
vinecopulib::BicopFamily::frank};
65+
#endif
66+
vinecopulib::FitControlsVinecop controls(family_set);
67+
p_vinecop->select(data, controls);
6068
return VineCopula(p_vinecop);
6169
}
6270

71+
Distribution VineCopulaFactory::build(const Sample & sample) const
72+
{
73+
return buildAsNative(sample);
74+
}
75+
6376
/* String converter */
6477
String VineCopulaFactory::__repr__() const
6578
{

lib/src/otvine/VineCopula.hxx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ public:
5555
protected:
5656
OT::Bool equals(const DistributionImplementation & other) const override;
5757
public:
58-
58+
59+
/** Convert to native OT distribution if possible */
60+
OT::Distribution asNative() const;
61+
5962
/** String converter */
6063
OT::String __repr__() const override;
6164

lib/src/otvine/VineCopulaFactory.hxx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public:
4848

4949
/** example of a func that return a point squared. **/
5050
OT::Distribution build(const OT::Sample & sample) const;
51+
VineCopula buildAsNative(const OT::Sample & sample) const;
5152

5253
/** String converter */
5354
OT::String __repr__() const override;

python/src/VineCopula.i

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// SWIG file VineCopulaFactory.i
1+
// SWIG file VineCopula.i
22

33
%{
4-
#include "otvine/VineCopulaFactory.hxx"
4+
#include "otvine/VineCopula.hxx"
55
%}
66

7-
%include VineCopulaFactory_doc.i
7+
%include VineCopula_doc.i
88

9-
%copyctor OTVINE::VineCopulaFactory;
9+
%copyctor OTVINE::VineCopula;
1010

11-
%include otvine/VineCopulaFactory.hxx
11+
%include otvine/VineCopula.hxx

python/test/t_VineCopulaFactory_std.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,13 @@
1616
realization = distribution.getRealization()
1717
print(realization)
1818

19-
sample = distribution.getSample(1000)
20-
print(sample.computeMean())
19+
bic = ot.FittingTest.BIC(sample, distribution)
20+
print("bic=", bic)
21+
22+
sample2 = distribution.getSample(1000)
23+
print(sample2.computeMean())
24+
25+
factory2 = ot.ClaytonCopulaFactory()
26+
print(factory2.build(sample))
27+
28+
print(factory.buildAsNative(sample).asNative())

0 commit comments

Comments
 (0)