-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Testing importance sampling in 3 dimensions. In 1 it works, in 3 it i…
…s not clear yet.
- Loading branch information
1 parent
ace2b7f
commit 556ff04
Showing
5 changed files
with
154 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include "../../src/newton-cotes/region.h" | ||
#include "../../src/multidimensional-range.h" | ||
#include <random> | ||
#include <string> | ||
#include <iomanip> | ||
#include <cmath> | ||
|
||
int main(int argc, char** argv) { | ||
std::size_t bins = 3, samples = 100; | ||
std::array<float,3> xmin{0,0,0}, xmax{1,1,1}; | ||
|
||
for (int i = 0;i<(argc-1);++i) { | ||
if (std::string(argv[i])=="-bins") bins = std::atoi(argv[++i]); | ||
else if (std::string(argv[i])=="-samples") samples = std::atoi(argv[++i]); | ||
else if (std::string(argv[i])=="-xmin") { | ||
xmin[0] = std::atof(argv[++i]); | ||
xmin[1] = std::atof(argv[++i]); | ||
xmin[2] = std::atof(argv[++i]); | ||
} | ||
else if (std::string(argv[i])=="-xmax") { | ||
xmax[0] = std::atof(argv[++i]); | ||
xmax[1] = std::atof(argv[++i]); | ||
xmax[2] = std::atof(argv[++i]); | ||
} | ||
} | ||
|
||
auto f = [] (std::array<float,3> x) { | ||
return std::sin(0.5*x[0]*M_PI) + std::cos(0.5*x[1]*M_PI) + x[2]; | ||
}; | ||
|
||
auto r = viltrum::region(f,viltrum::simpson, viltrum::range_primary<3>()); | ||
|
||
std::vector<float> probs(bins*bins*bins,0), hist(bins*bins*bins,0); | ||
|
||
std::array<float,3> dx; | ||
for (std::size_t c = 0; c<3; ++c) dx[c] = (xmax[c]-xmin[c])/bins; | ||
for (auto p : viltrum::multidimensional_range(std::array<size_t,3>{bins,bins,bins})) { | ||
std::array<float,3> x; | ||
for (std::size_t c = 0; c<3; ++c) x[c] = (float(p[c])+0.5)*dx[c] + xmin[c]; | ||
probs[p[0]+bins*p[1]+bins*bins*p[2]] = r.pdf_subrange(x,viltrum::range(xmin,xmax)); | ||
} | ||
|
||
std::random_device rd; | ||
std::mt19937 rng(rd()); | ||
std::uniform_real_distribution<float> dist(0,1); | ||
for (std::size_t i = 0; i<samples; ++i) { | ||
std::array<float,3> smp; | ||
for (std::size_t c = 0; c<3; ++c) smp[c]=dist(rng); | ||
auto x = r.sample_subrange(smp,viltrum::range(xmin,xmax)); | ||
std::array<std::size_t,3> p; | ||
for (std::size_t c = 0; c<3; ++c) p[c] = bins*(x[c]-xmin[c])/(xmax[c]-xmin[c]); | ||
hist[p[0]+bins*p[1]+bins*bins*p[2]] += float(bins*bins*bins)/float(samples); | ||
} | ||
|
||
float tot = 0; | ||
for (float x : probs) { tot+=x; std::cout<<std::setw(6)<<std::setprecision(3)<<x<<" "; } | ||
std::cout<<" [ "<<std::setw(6)<<std::setprecision(3)<<tot<< " ] "<<std::endl; | ||
tot = 0; | ||
for (float x : hist) { tot+=x; std::cout<<std::setw(6)<<std::setprecision(3)<<x<<" "; } | ||
std::cout<<" [ "<<std::setw(6)<<std::setprecision(3)<<tot<< " ] "<<std::endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include "../../src/newton-cotes/region.h" | ||
#include <random> | ||
#include <string> | ||
#include <iomanip> | ||
|
||
int main(int argc, char** argv) { | ||
float p0 = 1, p1 = 0.2, p2 = 1; | ||
std::size_t bins = 6, samples = 100; | ||
float xmin = 0.0, xmax = 1.0; | ||
|
||
for (int i = 0;i<(argc-1);++i) { | ||
if (std::string(argv[i])=="-bins") bins = std::atoi(argv[++i]); | ||
else if (std::string(argv[i])=="-samples") samples = std::atoi(argv[++i]); | ||
else if (std::string(argv[i])=="-p0") p0 = std::atof(argv[++i]); | ||
else if (std::string(argv[i])=="-p1") p1 = std::atof(argv[++i]); | ||
else if (std::string(argv[i])=="-p2") p2 = std::atof(argv[++i]); | ||
else if (std::string(argv[i])=="-xmin") xmin = std::atof(argv[++i]); | ||
else if (std::string(argv[i])=="-xmax") xmax = std::atof(argv[++i]); | ||
} | ||
|
||
auto f = [p0,p1,p2] (std::array<float,1> x) { | ||
return p0+(-3*p0+4*p1-p2)*x[0] + (2*p0-4*p1+2*p2)*x[0]*x[0]; | ||
}; | ||
|
||
auto r = viltrum::region(f,viltrum::simpson, viltrum::range(0.0f,1.0f)); | ||
|
||
std::vector<float> probs(bins,0), hist(bins,0); | ||
|
||
float dx = (xmax-xmin)/bins; | ||
for (std::size_t i = 0; i<bins; ++i) { | ||
std::array<float,1> x; | ||
x[0] = (float(i)+0.5)*dx + xmin; | ||
probs[i] = r.pdf_subrange(x,viltrum::range(xmin,xmax)); | ||
} | ||
|
||
std::random_device rd; | ||
std::mt19937 rng(rd()); | ||
std::uniform_real_distribution<float> dist(0,1); | ||
for (std::size_t i = 0; i<samples; ++i) { | ||
std::array<float,1> smp; | ||
smp[0]=dist(rng); | ||
auto x = r.sample_subrange(smp,viltrum::range(xmin,xmax)); | ||
std::size_t bin = bins*(x[0]-xmin)/(xmax-xmin); | ||
hist[bin] += float(bins)/float(samples); | ||
} | ||
|
||
float tot = 0; | ||
for (float x : probs) { tot+=x; std::cout<<std::setw(6)<<std::setprecision(3)<<x<<" "; } | ||
std::cout<<" [ "<<std::setw(6)<<std::setprecision(3)<<tot<< " ] "<<std::endl; | ||
tot = 0; | ||
for (float x : hist) { tot+=x; std::cout<<std::setw(6)<<std::setprecision(3)<<x<<" "; } | ||
std::cout<<" [ "<<std::setw(6)<<std::setprecision(3)<<tot<< " ] "<<std::endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters