Skip to content

Commit bbeebb4

Browse files
authored
Merge pull request #326 from VibekeSkytt/AIC
Aic
2 parents 306babd + cdbc318 commit bbeebb4

File tree

7 files changed

+652
-2
lines changed

7 files changed

+652
-2
lines changed

lrsplines2D/app/PointCloud2LR.C

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ void print_help_text()
102102
std::cout << "-toldoc: Documentation on file format for tolerance domains. \n";
103103
std::cout << "-featuredoc: Show feature documentation \n";
104104
std::cout << "-refstrat: Print parameter information to define refinement strategy. \n";
105+
std::cout << "-AIC <filename>: Compute AIC at each iteration level and output to the given file. Time consuming. Default: No AIC computations \n";
105106
std::cout << "-h or --help : Write this text\n";
106107
}
107108

@@ -231,6 +232,8 @@ int main(int argc, char *argv[])
231232
double minsize = -1.0;
232233
int feature_out = -1;
233234
int verbose = 0;
235+
int AIC = 0;
236+
char *AIC_file = 0;
234237

235238
int initncoef = 10;
236239
int distribute_ncoef = 0;
@@ -514,7 +517,14 @@ int main(int argc, char *argv[])
514517
if (stat < 0)
515518
return 1;
516519
}
517-
520+
else if (arg == "-AIC")
521+
{
522+
AIC = 1;
523+
int stat = fetchCharParameter(argc, argv, ki, AIC_file,
524+
nmb_par, par_read);
525+
if (stat < 0)
526+
return 1;
527+
}
518528
}
519529

520530
// Read remaining parameters
@@ -848,6 +858,10 @@ int main(int argc, char *argv[])
848858
approx->addUpperConstraint(extent[5] + zfac);
849859
approx->setLocalConstraint(zfac);
850860
}
861+
862+
if (AIC)
863+
approx->setAIC(true);
864+
851865
#ifdef DEBUG
852866
std::cout << "Range: " << extent[1]-extent[0] << ", " << extent[3]-extent[2];
853867
std::cout << ", " << extent[5]-extent[4] << std::endl;
@@ -930,7 +944,25 @@ int main(int argc, char *argv[])
930944
regular_cloud.write(ofr);
931945
}
932946

933-
947+
if (AIC)
948+
{
949+
// Fetch Mahalanobis distance and log likelyhood
950+
vector<double> AICinfo;
951+
vector<int> ncoef;
952+
approx->getAICInfo(AICinfo, ncoef);
953+
std::ofstream ofAIC(AIC_file);
954+
ofAIC.precision(10);
955+
ofAIC << "For each iteration level the following is reported: " << std::endl;
956+
ofAIC << "level, number of coefficients, Mahalanobix distance with students t-distribution, \n";
957+
ofAIC << "AIC student t-distribution, Mahalanobix distance normal distribution, AIC normal distribution" << std::endl;
958+
for (size_t ki=0; ki<ncoef.size(); ++ki)
959+
{
960+
ofAIC << ki << "\t" << ncoef[ki];
961+
for (size_t kj=0; kj<4; ++kj)
962+
ofAIC << "\t" << AICinfo[4*ki+kj];
963+
ofAIC << std::endl;
964+
}
965+
}
934966

935967
std::ofstream sfout(surffile); // Surface output stream
936968

lrsplines2D/app/loglikelyhood.C

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (C) 1998, 2000-2007, 2010, 2011, 2012, 2013 SINTEF ICT,
3+
* Applied Mathematics, Norway.
4+
*
5+
* Contact information: E-mail: [email protected]
6+
* SINTEF ICT, Department of Applied Mathematics,
7+
* P.O. Box 124 Blindern,
8+
* 0314 Oslo, Norway.
9+
*
10+
* This file is part of GoTools.
11+
*
12+
* GoTools is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* GoTools is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public
23+
* License along with GoTools. If not, see
24+
* <http://www.gnu.org/licenses/>.
25+
*
26+
* In accordance with Section 7(b) of the GNU Affero General Public
27+
* License, a covered work must retain the producer line in every data
28+
* file that is created or manipulated using GoTools.
29+
*
30+
* Other Usage
31+
* You can be released from the requirements of the license by purchasing
32+
* a commercial license. Buying such a license is mandatory as soon as you
33+
* develop commercial activities involving the GoTools library without
34+
* disclosing the source code of your own applications.
35+
*
36+
* This file may be used in accordance with the terms contained in a
37+
* written agreement between you and SINTEF ICT.
38+
*/
39+
40+
#include "GoTools/geometry/GoTools.h"
41+
#include "GoTools/geometry/FileUtils.h"
42+
#include "GoTools/lrsplines2D/LogLikelyhood.h"
43+
#include <iostream>
44+
#include <fstream>
45+
46+
using std::vector;
47+
using namespace Go;
48+
49+
int main(int argc, char *argv[])
50+
{
51+
52+
if (argc != 2)
53+
{
54+
std::cout << "Parameters: residuals (x,y,z,r)" << std::endl;
55+
return 1;
56+
}
57+
58+
std::ifstream input(argv[1]);
59+
double Tny = 10.0; // Guess value
60+
61+
// Read point cloud with residuals
62+
vector<double> data;
63+
vector<double> extent(8); // Limits for points in all coordinates
64+
int nmb_pts = 0;
65+
FileUtils::readTxtPointFile(input, 4, data, nmb_pts, extent);
66+
67+
std::cout << "Number of points: " << nmb_pts << std::endl;
68+
// Extract residuals
69+
vector<double> residuals(nmb_pts);
70+
for (size_t ki=0; ki<nmb_pts; ++ki)
71+
residuals[ki] = data[4*ki+3];
72+
std::cout << "Min: " << extent[6] << ", max: " << extent[7] << std::endl;
73+
74+
double loglh2 = 0.0;
75+
double loglh = LogLikelyhood::compute(residuals, Tny, true, loglh2);
76+
printf("Loglikelyhood: %7.13f, %7.13f \n",loglh, loglh2);
77+
}

lrsplines2D/app/loglikelyhoodFixed.C

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (C) 1998, 2000-2007, 2010, 2011, 2012, 2013 SINTEF ICT,
3+
* Applied Mathematics, Norway.
4+
*
5+
* Contact information: E-mail: [email protected]
6+
* SINTEF ICT, Department of Applied Mathematics,
7+
* P.O. Box 124 Blindern,
8+
* 0314 Oslo, Norway.
9+
*
10+
* This file is part of GoTools.
11+
*
12+
* GoTools is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* GoTools is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public
23+
* License along with GoTools. If not, see
24+
* <http://www.gnu.org/licenses/>.
25+
*
26+
* In accordance with Section 7(b) of the GNU Affero General Public
27+
* License, a covered work must retain the producer line in every data
28+
* file that is created or manipulated using GoTools.
29+
*
30+
* Other Usage
31+
* You can be released from the requirements of the license by purchasing
32+
* a commercial license. Buying such a license is mandatory as soon as you
33+
* develop commercial activities involving the GoTools library without
34+
* disclosing the source code of your own applications.
35+
*
36+
* This file may be used in accordance with the terms contained in a
37+
* written agreement between you and SINTEF ICT.
38+
*/
39+
40+
#include "GoTools/geometry/GoTools.h"
41+
#include "GoTools/geometry/FileUtils.h"
42+
#include "GoTools/lrsplines2D/LogLikelyhood.h"
43+
#include <iostream>
44+
#include <fstream>
45+
46+
using std::vector;
47+
using namespace Go;
48+
49+
int main(int argc, char *argv[])
50+
{
51+
52+
if (argc != 3)
53+
{
54+
std::cout << "Parameters: residuals (x,y,z,r), degrees of freedom in T-distribution" << std::endl;
55+
return 1;
56+
}
57+
58+
std::ifstream input(argv[1]);
59+
double Tny = atof(argv[2]);
60+
61+
// Read point cloud with residuals
62+
vector<double> data;
63+
vector<double> extent(8); // Limits for points in all coordinates
64+
int nmb_pts = 0;
65+
FileUtils::readTxtPointFile(input, 4, data, nmb_pts, extent);
66+
67+
std::cout << "Number of points: " << nmb_pts << std::endl;
68+
// Extract residuals
69+
vector<double> residuals(nmb_pts);
70+
for (size_t ki=0; ki<nmb_pts; ++ki)
71+
residuals[ki] = data[4*ki+3];
72+
std::cout << "Min: " << extent[6] << ", max: " << extent[7] << std::endl;
73+
74+
double loglh2 = 0.0;
75+
double loglh = LogLikelyhood::compute(residuals, Tny, false, loglh2);
76+
printf("Loglikelyhood: %7.13f, %7.13f \n",loglh, loglh2);
77+
}

lrsplines2D/include/GoTools/lrsplines2D/LRSurfApprox.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,19 @@ class LRSurfApprox
507507
write_feature_ = false;
508508
}
509509

510+
void setAIC(bool AIC)
511+
{
512+
compute_AIC_ = AIC;
513+
}
514+
515+
void getAICInfo(std::vector<double>& AIC_info, std::vector<int>& ncoef)
516+
{
517+
AIC_info.clear();
518+
ncoef.clear();
519+
AIC_info.insert(AIC_info.end(), AIC_.begin(), AIC_.end());
520+
ncoef.insert(ncoef.end(), ncoef_.begin(), ncoef_.end());
521+
}
522+
510523
private:
511524
shared_ptr<LRSplineSurface> srf_;
512525
shared_ptr<Eval1D3DSurf> evalsrf_;
@@ -593,6 +606,11 @@ class LRSurfApprox
593606
bool write_feature_;
594607
int ncell_;
595608

609+
// AIC
610+
bool compute_AIC_;
611+
std::vector<double> AIC_;
612+
std::vector<int> ncoef_;
613+
596614
void initDefaultParams();
597615

598616
/// Define free and fixed coefficients
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (C) 1998, 2000-2007, 2010, 2011, 2012, 2013 SINTEF ICT,
3+
* Applied Mathematics, Norway.
4+
*
5+
* Contact information: E-mail: [email protected]
6+
* SINTEF ICT, Department of Applied Mathematics,
7+
* P.O. Box 124 Blindern,
8+
* 0314 Oslo, Norway.
9+
*
10+
* This file is part of GoTools.
11+
*
12+
* GoTools is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* GoTools is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public
23+
* License along with GoTools. If not, see
24+
* <http://www.gnu.org/licenses/>.
25+
*
26+
* In accordance with Section 7(b) of the GNU Affero General Public
27+
* License, a covered work must retain the producer line in every data
28+
* file that is created or manipulated using GoTools.
29+
*
30+
* Other Usage
31+
* You can be released from the requirements of the license by purchasing
32+
* a commercial license. Buying such a license is mandatory as soon as you
33+
* develop commercial activities involving the GoTools library without
34+
* disclosing the source code of your own applications.
35+
*
36+
* This file may be used in accordance with the terms contained in a
37+
* written agreement between you and SINTEF ICT.
38+
*/
39+
40+
#ifndef LOGLIKELYHOOD_H
41+
#define LOGLIKELYHOOD_H
42+
43+
#include <vector>
44+
#include <iostream>
45+
46+
namespace LogLikelyhood
47+
{
48+
double compute(const std::vector<double>& residual, double indegT,
49+
bool iterateT, double& llh2);
50+
51+
void getMeanAndVariance(const std::vector<double>& residual,
52+
double& mean, double& variance);
53+
54+
void getMeanAndVarianceIterated(const std::vector<double>& residual,
55+
double& degreeT,
56+
double& mean, double& variance,
57+
bool iterateT);
58+
59+
void iterateDegreeT(const std::vector<double>& residual,
60+
double mean, double variance,
61+
double& degreeT);
62+
};
63+
64+
#endif

0 commit comments

Comments
 (0)