<commit_before>/*
 * ARX: Powerful Data Anonymization
 * Copyright 2012 - 2016 <NAME>, <NAME> and contributors
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.deidentifier.arx.metric.v2;

import org.deidentifier.arx.ARXConfiguration;
import org.deidentifier.arx.DataDefinition;
import org.deidentifier.arx.framework.check.distribution.DistributionAggregateFunction;
import org.deidentifier.arx.framework.check.groupify.HashGroupify;
import org.deidentifier.arx.framework.check.groupify.HashGroupifyEntry;
import org.deidentifier.arx.framework.data.Data;
import org.deidentifier.arx.framework.data.DataManager;
import org.deidentifier.arx.framework.data.GeneralizationHierarchy;
import org.deidentifier.arx.framework.lattice.Transformation;
import org.deidentifier.arx.metric.InformationLossWithBound;
import org.deidentifier.arx.metric.MetricConfiguration;

/**
 * This class implements a the entropy-based information loss model proposed in:<br>
 * A Game Theoretic Framework for Analyzing Re-Identification Risk.
 * <NAME>, <NAME>, <NAME>, <NAME>,
 * <NAME>, <NAME>, <NAME>, <NAME>
 * PLOS|ONE. 2015.
 * 
 * 
 * @author <NAME>
 */
public class MetricSDNMEntropyBasedInformationLoss extends AbstractMetricSingleDimensional {

    /** SVUID*/
    private static final long serialVersionUID = -2443537745262162075L;

    /**
     * Implements the entropy-based IL model. Ignores record suppression. Returns the loss for exactly one record.
     * @param transformation
     * @param entry
     * @param shares
     * @param functions
     * @param microaggregationStartIndex
     * @param maxIL
     * @return
     */
    public static double getEntropyBasedInformationLoss(Transformation transformation, 
                                                        HashGroupifyEntry entry,
                                                        DomainShare[] shares,
                                                        DistributionAggregateFunction[] microaggregationFunctions,
                                                        int microaggregationStartIndex,
                                                        double maxIL) {

        // We transform the formula, to make evaluating it more efficient.
        //
        // With maxIL = long(size_1 * size_2 * ... * size_n) we define
        // IL = [-log( 1 / (share_1 * size_1) ) - log ( 1 / (share_2 * size_2) ) ... - log( 1 / (share_n * size_n) ) ] / maxIL
        //
        // Step 1:
        //
        // IL = [log(share_1 * size_1 ) + log (share_2 * size_2 ) ... + log( share_n * size_n) ] / maxIL
        //
        // Step 2:
        //
        // IL = [log(share_1 * share_2 * ... * share_n) + log(size_1 * size_2 * ... * size_n) ] / maxIL
        //
        // Step 3:
        // 
        // IL = [log(share_1 * share_2 * ... * share_n) + maxIL ] / maxIL
        //
        // Step 4:<commit_msg>Fix typo<commit_after>/*
 * ARX: Powerful Data Anonymization
 * Copyright 2012 - 2016 <NAME>, <NAME> and contributors
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.deidentifier.arx.metric.v2;

import org.deidentifier.arx.ARXConfiguration;
import org.deidentifier.arx.DataDefinition;
import org.deidentifier.arx.framework.check.distribution.DistributionAggregateFunction;
import org.deidentifier.arx.framework.check.groupify.HashGroupify;
import org.deidentifier.arx.framework.check.groupify.HashGroupifyEntry;
import org.deidentifier.arx.framework.data.Data;
import org.deidentifier.arx.framework.data.DataManager;
import org.deidentifier.arx.framework.data.GeneralizationHierarchy;
import org.deidentifier.arx.framework.lattice.Transformation;
import org.deidentifier.arx.metric.InformationLossWithBound;
import org.deidentifier.arx.metric.MetricConfiguration;

/**
 * This class implements a the entropy-based information loss model proposed in:<br>
 * A Game Theoretic Framework for Analyzing Re-Identification Risk.
 * <NAME>, <NAME>, <NAME>, <NAME>,
 * <NAME>, <NAME>, <NAME>, <NAME>
 * PLOS|ONE. 2015.
 * 
 * 
 * @author <NAME>
 */
public class MetricSDNMEntropyBasedInformationLoss extends AbstractMetricSingleDimensional {

    /** SVUID*/
    private static final long serialVersionUID = -2443537745262162075L;

    /**
     * Implements the entropy-based IL model. Ignores record suppression. Returns the loss for exactly one record.
     * @param transformation
     * @param entry
     * @param shares
     * @param functions
     * @param microaggregationStartIndex
     * @param maxIL
     * @return
     */
    public static double getEntropyBasedInformationLoss(Transformation transformation, 
                                                        HashGroupifyEntry entry,
                                                        DomainShare[] shares,
                                                        DistributionAggregateFunction[] microaggregationFunctions,
                                                        int microaggregationStartIndex,
                                                        double maxIL) {

        // We transform the formula, to make evaluating it more efficient.
        //
        // With maxIL = log(size_1 * size_2 * ... * size_n) we define
        // IL = [-log( 1 / (share_1 * size_1) ) - log ( 1 / (share_2 * size_2) ) ... - log( 1 / (share_n * size_n) ) ] / maxIL
        //
        // Step 1:
        //
        // IL = [log(share_1 * size_1 ) + log (share_2 * size_2 ) ... + log( share_n * size_n) ] / maxIL
        //
        // Step 2:
        //
        // IL = [log(share_1 * share_2 * ... * share_n) + log(size_1 * size_2 * ... * size_n) ] / maxIL
        //
        // Step 3:
        // 
        // IL = [log(share_1 * share_2 * ... * share_n) + maxIL ] / maxIL
        //
        // Step 4:
{'fraction_non_alphanumeric': 0.10132547864506627, 'fraction_numerical': 0.017378497790868926, 'mean_word_length': 2.243075453677173, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6544734', 'n_tokens_mistral': 1917, 'n_tokens_neox': 1808, 'n_words': 526}