<commit_before><commit_msg>Add transinformation.hpp that provide calculation of entropy and transinformation.<commit_after>#ifndef BNI_EVALUATION_TRANSINFORMATION_HPP
#define BNI_EVALUATION_TRANSINFORMATION_HPP

#include <bayesian/graph.hpp>
#include <bayesian/sampler.hpp>

namespace bn {
namespace evaluation {
    
struct entropy {
    // sampling瓾e[upāCvariables̓entropyvZ
    // sampling: load_sampleςsampler
    // variables: ׂm[h̃Xg
    double operator() (sampler const& sampling, std::vector<vertex_type> const& variables) const
    {
        // f[^猵Ȋmo(tmł͂Ȃ)
        // CPTem[hsZĎZo邱Ƃ\Cǂ낤
        std::unordered_map<condition_t, std::size_t> table;
        for(auto const& sample : sampling.table())
        {
            condition_t cond;
            for(auto const& variable : variables)
                cond[variable] = sample.first.at(variable);

            // LΉZCȂ΍쐬
            auto it = table.find(cond);
            if(it == table.end()) table.emplace(cond, sample.second);
            else                  it->second += sample.second;
        }
        
        // ׂĂ̊mɒ2̃Ggs[vZs
        double entropy = 0.0;
        for(auto const& pattern : table)
        {
            auto const probability = static_cast<double>(pattern.second) / sampling.sampling_size();
            entropy -= probability * std::log2(probability);
        }
        return entropy;
    }
    
    // sampling瓾e[upāCvariables̓entropyvZ
    // sampling: load_sampleςsampler
    // variable: ׂm[h
    double operator() (sampler const& sampling, vertex_type const& variable) const
    {
        return (*this)(sampling, {variable});
    }
};

struct mutual_information {
    // sampling瓾e[upāCxẙԂ̑ݏʂvZ
    // sampling: load_sampleςsampler
    // x, y: ׂm[h
    double operator() (sampler const& sampling, vertex_type const& x, vertex_type const& y) const
    {
        entropy ent;
        return ent(sampling, x) + ent(sampling, y) - ent(sampling, {x, y});
    }
    
    // sampling瓾e[upāCxẙԂ̑ݏʂvZ(xyP̂entropyvZς̏ꍇ)
    // sampling: load_sampleςsampler
    // x, y: ׂm[h
    // x_ent, y_ent: ׂm[h̃Ggs[
    template<class T>
    double operator() (sampler const& sampling, vertex_type const& x, T const x_ent, vertex_type const& y, T const y_ent)
    {
        entropy ent;
        return x_ent + y_ent - ent(sampling, {x, y});
    }
    
    // sampling瓾e[upāCxẙԂ̑ݏʂvZ(xyxyentropyvZς̏ꍇ)
    // x_ent, y_ent: ׂm[h̃Ggs[
    // xy_ent: ׂm[h2̓Ggs[
    template<class T>
    double operator() (T const x_ent, T const y_ent, T const xy_ent)
    {
        return x_ent + y_ent - xy_ent;
    }
};

} // namespace evaluation
} // namespace bn

#endif // #ifndef BNI_EVALUATION_TRANSINFORMATION_HPP

{'fraction_non_alphanumeric': 0.1216065451840833, 'fraction_numerical': 0.001859427296392711, 'mean_word_length': 2.498049414824447, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 11, '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': '6331719', 'n_tokens_mistral': 992, 'n_tokens_neox': 976, 'n_words': 195}