1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-09-19 18:29:36 +00:00

moved small_kendall to statistics

This commit is contained in:
Zeno Rogue 2024-07-26 10:23:47 +02:00
parent 1511c715e4
commit 75465ca164
3 changed files with 44 additions and 40 deletions

View File

@ -182,7 +182,7 @@ void optimize_sag_loglik_logistic() {
void optimize_sag_loglik_match() { void optimize_sag_loglik_match() {
if(state &~ SS_WEIGHTED) return; if(state &~ SS_WEIGHTED) return;
lsq::leastsquare_solver<2> lsqs; stats::leastsquare_solver<2> lsqs;
for(auto& ei: sagedges) { for(auto& ei: sagedges) {
ld y = sagdist[sagid[ei.i]][sagid[ei.j]]; ld y = sagdist[sagid[ei.i]][sagid[ei.j]];

View File

@ -2,47 +2,11 @@
// Copyright (C) 2011-2022 Tehora and Zeno Rogue, see 'hyper.cpp' for details // Copyright (C) 2011-2022 Tehora and Zeno Rogue, see 'hyper.cpp' for details
#include "kohonen.h" #include "kohonen.h"
#include "../statistics.cpp"
namespace rogueviz { namespace rogueviz {
namespace measures { namespace measures {
double kendall(const vector<pair<int, int>>& allp) {
int maxo = 0, maxe = 0;
for(const auto& a: allp) maxo = max(maxo, a.first), maxe = max(maxe, a.second);
maxo++; maxe++;
if(maxo >= 256 || maxe >= 256) {
println(hlog, "more than 256!\n");
exit(1);
}
int cnt[256][256];
for(int a=0; a<maxo; a++)
for(int b=0; b<maxe; b++)
cnt[a][b] = 0;
for(const auto& a: allp) cnt[a.first][a.second]++;
// int i1 = 0, i2 = 0;
int K = isize(allp);
// allp.emplace_back(maxo, maxe);
vector<int> counts(maxe, 0);
vector<int> totals(maxe);
double tau = 0;
for(int i=0; i<maxo; i++) {
totals[0] = 0;
for(int ii=1; ii<maxe; ii++)
totals[0] -= counts[ii];
for(int ii=1; ii<maxe; ii++)
totals[ii] = totals[ii-1] + counts[ii] + counts[ii-1];
for(int b=0; b<maxe; b++) {
tau += totals[b] * 1. * cnt[i][b];
counts[b] += cnt[i][b];
}
}
ld par = (K * (K-1.) / 2);
return tau / par;
}
vector<pair<int, int>> recreate_topology(const vector<int>& mapp, const vector<pair<int, int> >& edges) { vector<pair<int, int>> recreate_topology(const vector<int>& mapp, const vector<pair<int, int> >& edges) {
auto cmapp = mapp; auto cmapp = mapp;
@ -117,7 +81,7 @@ ld evaluate_measure(manidata& emb, manidata& orig, vector<int>& mapp, vector<pai
for(int i=0; i<No; i++) if(mapp[i] != -1) for(int i=0; i<No; i++) if(mapp[i] != -1)
for(int j=0; j<i; j++) if(mapp[j] != -1) for(int j=0; j<i; j++) if(mapp[j] != -1)
allp.emplace_back(diso[i][j], dise[mapp[i]][mapp[j]]); allp.emplace_back(diso[i][j], dise[mapp[i]][mapp[j]]);
energy = kendall(allp); energy = stats::small_kendall<256, 256>(allp);
} }
else if(k == 3) { else if(k == 3) {
vector<bool> empty(Ne, true); vector<bool> empty(Ne, true);

View File

@ -1,9 +1,12 @@
#ifndef _STATISTICS_CPP_
#define _STATISTICS_CPP_
#include <vector> #include <vector>
#include <cstdio> #include <cstdio>
#include <cmath> #include <cmath>
#include <array> #include <array>
namespace lsq { namespace stats {
using namespace std; using namespace std;
@ -98,4 +101,41 @@ template<size_t N> struct leastsquare_solver {
} }
}; };
template<size_t dim1, size_t dim2> double small_kendall(const vector<pair<int, int>>& allp) {
int maxo = 0, maxe = 0;
for(const auto& a: allp) maxo = max(maxo, a.first), maxe = max(maxe, a.second);
maxo++; maxe++;
if(maxo >= dim1 || maxe >= dim2)
throw hr::hr_exception("small_kendall limit exceeded");
int cnt[dim1][dim2];
for(int a=0; a<maxo; a++)
for(int b=0; b<maxe; b++)
cnt[a][b] = 0;
for(const auto& a: allp) cnt[a.first][a.second]++;
// int i1 = 0, i2 = 0;
int K = hr::isize(allp);
// allp.emplace_back(maxo, maxe);
vector<int> counts(maxe, 0);
vector<int> totals(maxe);
double tau = 0;
for(int i=0; i<maxo; i++) {
totals[0] = 0;
for(int ii=1; ii<maxe; ii++)
totals[0] -= counts[ii];
for(int ii=1; ii<maxe; ii++)
totals[ii] = totals[ii-1] + counts[ii] + counts[ii-1];
for(int b=0; b<maxe; b++) {
tau += totals[b] * 1. * cnt[i][b];
counts[b] += cnt[i][b];
}
}
double par = (K * (K-1.) / 2);
return tau / par;
}
} }
#endif