<commit_before>#include "div_l2.hpp"

#include <algorithm>
#include <cmath>
#include <numeric>
#include <string>
#include <vector>

#include "fix_terms.hpp"
#include "gamma.hpp"

namespace NPDivs {

using namespace std;

DivL2::DivL2(double ub_) : DivFunc(ub_) {}

string DivL2::name() const {
    return "L2 divergence";
}


class pow_mult {
    double ex;
    double mult;
public:
    pow_mult(double e, double m) : ex(e), mult(m) {}
    double operator()(double x) {
        return pow(x, ex) * mult;
    }
};

template <typename T>
inline T mean(const vector<T> &v) {
    return accumulate(v.begin(), v.end(), (T) 0) / v.size();
}


double DivL2::operator()(const vector<float> &rho_x,
                         const vector<float> &nu_x,
                         const vector<float> &rho_y,
                         const vector<float> &nu_y,
                         int dim,
                         int k) const {
    /* Estimates L2 divergence \sqrt \int (p-q)^2 between distribution X and Y,
     * based on kth-nearest-neighbor statistics.
     */
    const double c = (k-1) / pow(M_PI, .5 * dim) / gamma(dim/2.0 + 1);

    int N = rho_x.size();
    int M = rho_y.size();

    // break up the calculation according to
    // \sqrt \int (p - q)^2 = \sqrt( \int p^2 - \int qp - \int pq + \int q^2 )
    vector<double> pp, qp, pq, qq;
    pp.resize(N); qp.resize(N);
    pq.resize(M); qq.resize(M);<commit_msg>bug in L2 estimator: divide instead of multiply<commit_after>#include "div_l2.hpp"

#include <algorithm>
#include <cmath>
#include <numeric>
#include <string>
#include <vector>

#include "fix_terms.hpp"
#include "gamma.hpp"

namespace NPDivs {

using namespace std;

DivL2::DivL2(double ub_) : DivFunc(ub_) {}

string DivL2::name() const {
    return "L2 divergence";
}


class pow_mult {
    double ex;
    double mult;
public:
    pow_mult(double e, double m) : ex(e), mult(m) {}
    double operator()(double x) {
        return pow(x, ex) * mult;
    }
};

template <typename T>
inline T mean(const vector<T> &v) {
    return accumulate(v.begin(), v.end(), (T) 0) / v.size();
}


double DivL2::operator()(const vector<float> &rho_x,
                         const vector<float> &nu_x,
                         const vector<float> &rho_y,
                         const vector<float> &nu_y,
                         int dim,
                         int k) const {
    /* Estimates L2 divergence \sqrt \int (p-q)^2 between distribution X and Y,
     * based on kth-nearest-neighbor statistics.
     */
    const double c = (k-1) / pow(M_PI, .5 * dim) * gamma(dim/2.0 + 1);

    int N = rho_x.size();
    int M = rho_y.size();

    // break up the calculation according to
    // \sqrt \int (p - q)^2 = \sqrt( \int p^2 - \int qp - \int pq + \int q^2 )
    vector<double> pp, qp, pq, qq;
    pp.resize(N); qp.resize(N);
    pq.resize(M); qq.resize(M);
{'fraction_non_alphanumeric': 0.150278940027894, 'fraction_numerical': 0.012203626220362623, 'mean_word_length': 2.278857142857143, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3971787', 'n_tokens_mistral': 1052, 'n_tokens_neox': 1013, 'n_words': 286}