Commit ec1b196c authored by niklas.baumgarten's avatar niklas.baumgarten
Browse files

updated measures and measure maps

parent 4465b14e
#ifndef EMPIRICMEASURELEVELMAPS_HPP
#define EMPIRICMEASURELEVELMAPS_HPP
#include "EmpiricMeasures.hpp"
#include "MonteCarlo.hpp"
#include <math.h>
#include <map>
// Todo: make second key template for degree to realize MultidegreeMonteCarlo
// Todo: also key should include proc information
template<typename T>
struct LevelMap : std::map<Level, T> {
std::vector<int> GetLevelVector() const {
std::vector<int> levelVector;
for (auto &&[level, entry]: *this)
levelVector.push_back(level.fine);
return levelVector;
}
std::vector<double> GetQVector() const {
std::vector<double> qVector;
for (auto &&[level, entry]: *this)
qVector.push_back(entry.Q);
return qVector;
}
std::vector<double> GetYVector() const {
std::vector<double> yVector;
for (auto &&[level, entry]: *this)
yVector.push_back(entry.Y);
return yVector;
}
std::vector<double> GetCostVector() const {
std::vector<double> costVector;
for (auto &&[level, entry]: *this)
costVector.push_back(entry.Cost);
return costVector;
}
std::vector<int> GetMVector() const {
std::vector<int> mVector;
for (auto &&[level, entry]: *this)
mVector.push_back(entry.M);
return mVector;
}
std::vector<int> GetdMVector() const {
std::vector<int> dMVector;
for (auto &&[level, entry]: *this)
dMVector.push_back(entry.dM);
return dMVector;
}
};
struct Exponents;
struct MonteCarloMap : LevelMap<MonteCarlo *> {
void UpdateSampleCounter(double epsilon);
void AppendLevel(double epsilon, Exponents exponents,
Level newLevel, MonteCarlo *newMc);
};
struct SampleCounterMap : LevelMap<SampleCounter> {
void Update(const MonteCarloMap &mcMap);
bool NoSamplesLeft();
};
struct AveragesMap : LevelMap<Averages> {
double Q = 0.0;
double Cost = 0.0;
AveragesMap() {};
AveragesMap(const MonteCarloMap &mcMap) {
Update(mcMap);
};
void Update(const MonteCarloMap &mcMap);
};
struct VariancesMap : LevelMap<Variances> {
VariancesMap() {};
VariancesMap(const MonteCarloMap &mcMap) {
Update(mcMap);
};
void Update(const MonteCarloMap &mcMap);
};
struct KurtosisMap : LevelMap<Kurtosis> {
KurtosisMap() {};
KurtosisMap(const MonteCarloMap &mcMap) {
Update(mcMap);
};
void Update(const MonteCarloMap &mcMap);
};
struct MultiLevelMonteCarloData {
SampleCounterMap ctrs;
AveragesMap avgs;
VariancesMap vars;
KurtosisMap kurtosis;
MultiLevelMonteCarloData() {};
MultiLevelMonteCarloData(const MonteCarloMap &mcMap) {
ExtractDataFrom(mcMap);
}
void ExtractDataFrom(const MonteCarloMap &mcMap);
};
#endif //EMPIRICMEASURELEVELMAPS_HPP
#include "EmpiricMeasures.hpp"
#include "utility/Assertion.hpp"
#include <math.h>
void SampleCounter::Update() {
......@@ -9,7 +8,7 @@ void SampleCounter::Update() {
}
void Sums::Update(SampleSolution &fineSolution, SampleSolution &coarseSolution) {
double diffQ = fineSolution.Q - coarseSolution.Q; // Todo
double diffQ = fineSolution.Q - coarseSolution.Q; // Todo might be unstable!
Cost += fineSolution.Cost;
Q1 += fineSolution.Q;
Q2 += fineSolution.Q * fineSolution.Q;
......@@ -29,6 +28,8 @@ void Averages::Update(Sums sums, int M) {
Y4 = sums.Y4 / M;
Q = abs(sums.Q1 / M);
Q2 = sums.Q2 / M;
Q3 = sums.Q3 / M;
Q4 = sums.Q4 / M;
}
void Variances::Update(Averages avgs) {
......@@ -37,8 +38,8 @@ void Variances::Update(Averages avgs) {
}
void Kurtosis::Update(Averages avgs, Variances vars) {
// Q = (avgs.Q4 - 4.0 * avgs.Q3 * avgs.Q + 6.0 * avgs.Q2 * avgs.Q * avgs.Q -
// 3.0 * avgs.Q * avgs.Q * avgs.Q * avgs.Q) / vars.Q / vars.Q;
Q = (avgs.Q4 - 4.0 * avgs.Q3 * avgs.Q + 6.0 * avgs.Q2 * avgs.Q * avgs.Q -
3.0 * avgs.Q * avgs.Q * avgs.Q * avgs.Q) / vars.Q / vars.Q;
Y = (avgs.Y4 - 4.0 * avgs.Y3 * avgs.Y + 6.0 * avgs.Y2 * avgs.Y * avgs.Y -
3.0 * avgs.Y * avgs.Y * avgs.Y * avgs.Y) / vars.Y / vars.Y;
if (Y > 100.0)
......
......@@ -32,6 +32,8 @@ struct Sums {
struct Averages {
double Q = 0.0;
double Q2 = 0.0;
double Q3 = 0.0;
double Q4 = 0.0;
double Y = 0.0;
double Y2 = 0.0;
double Y3 = 0.0;
......
#include "EmpiricMeasuresOnLevels.hpp"
void SampleCountersOnLevels::Update() {
}
void AveragesOnLevels::Update(const MonteCarloMap &mcMap) {
for (auto &mc: mcMap) {
Q[mc.first] = mc.second->avgs.Q;
Y[mc.first] = mc.second->avgs.Y;
Cost[mc.first] = mc.second->avgs.Cost;
}
}
void VariancesOnLevels::Update(const MonteCarloMap &mcMap) {
for (auto &mc: mcMap) {
Q[mc.first] = mc.second->vars.Q;
Y[mc.first] = mc.second->vars.Y;
}
}
void KurtosisOnLevels::Update(const MonteCarloMap &mcMap) {
for (auto &mc: mcMap) {
Q[mc.first] = mc.second->kurtosis.Q;
Y[mc.first] = mc.second->kurtosis.Y;
}
}
void Exponents::Update(const MonteCarloMap &mcMap) {
vector<double> levelList, avgsY, varsY, costs;
auto iter = mcMap.begin();
iter++;
for (; iter != mcMap.end(); iter++) {
levelList.push_back((double) iter->first);
avgsY.push_back(-log2(iter->second->avgs.Y));
varsY.push_back(-log2(iter->second->vars.Y));
costs.push_back(log2(iter->second->avgs.Cost));
}
std::pair<double, double> res = linear_fit(levelList, avgsY);
alpha = res.first;
res = linear_fit(levelList, varsY);
beta = res.first;
res = linear_fit(levelList, costs);
gamma = res.first;
}
#ifndef EMPIRICMEASURESONLEVELS_HPP
#define EMPIRICMEASURESONLEVELS_HPP
#include "EmpiricMeasures.hpp"
#include "MonteCarlo.hpp"
#include <map>
typedef std::map<int, MonteCarlo *> MonteCarloMap;
struct SampleCountersOnLevels {
std::map<int, int> M;
std::map<int, int> dM;
void Update();
};
struct AveragesOnLevels {
std::map<int, double> Q;
std::map<int, double> Y;
std::map<int, double> Cost;
void Update(const MonteCarloMap &mcMap);
};
struct VariancesOnLevels {
std::map<int, double> Q;
std::map<int, double> Y;
void Update(const MonteCarloMap &mcMap);
};
struct KurtosisOnLevels {
std::map<int, double> Q;
std::map<int, double> Y;
void Update(const MonteCarloMap &mcMap);
};
struct Exponents {
double alpha = 0.0;
double beta = 0.0;
double gamma = 0.0;
void Update(const MonteCarloMap &mcMap);
};
#endif //EMPIRICMEASURESONLEVELS_HPP
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment