<commit_before>package dr.app.tracer.traces;

/**
 * A class that stores the correlation statistics for a trace
 *
 * @author <NAME>
 * @author <NAME>
 * @version $Id: TraceCorrelation.java,v 1.2 2006/11/29 14:53:53 rambaut Exp $
 */
public class TraceCorrelation extends TraceDistribution {

    public TraceCorrelation(double[] values, int stepSize) {
        super(values, stepSize);

        if (isValid) {
            analyseCorrelation(values, stepSize);
        }
    }

    public double getStdErrorOfMean() { return stdErrorOfMean; }
    public double getACT() { return ACT; }

    private final static int MAX_OFFSET = 1000;

    /**
     * Analyze trace
     */
    private void analyseCorrelation(double[] values, int stepSize) {

        int maxLag = MAX_OFFSET;
        int samples = values.length;
        if ((samples/3) < maxLag) {
            maxLag = (samples/3);
        }

        double[] gammaStat = new double[maxLag];
        double[] varGammaStat = new double[maxLag];
        double varStat = 0.0;
        double varVarStat = 0.0;
        double assVarCor = 1.0;
        double del1, del2;

        for (int lag=0; lag < maxLag; lag++) {
            for (int j = 0; j < samples-lag; j++) {
                del1=values[j] - mean;
                del2=values[j + lag] - mean;
                gammaStat[lag] += ( del1*del2 );
                varGammaStat[lag] += (del1*del1*del2*del2);
            }

            gammaStat[lag] /= ((double)(samples-lag));
            varGammaStat[lag] /= ((double) samples-lag);
            varGammaStat[lag] -= (gammaStat[0] * gammaStat[0]);

            if (lag==0) {
                varStat = gammaStat[0];
                varVarStat = varGammaStat[0];
                assVarCor = 1.0;
            }
            else if (lag%2==0)
            {
                // fancy stopping criterion :)
                if (gammaStat[lag-1] + gammaStat[lag] > 0) {
                    varStat    += 2.0*(gammaStat[lag-1] + gammaStat[lag]);
                    varVarStat += 2.0*(varGammaStat[lag-1] + varGammaStat[lag]);
                    assVarCor  += 2.0*((gammaStat[lag-1] * gammaStat[lag-1]) + (gammaStat[lag] * gammaStat[lag])) / (gammaStat[0] * gammaStat[0]);
                }
                // stop
                else
                    maxLag=lag;
            }
        }

        // standard error of mean
        stdErrorOfMean = Math.sqrt(varStat/samples);

        // auto correlation time
        ACT = stepSize * varStat / gammaStat[0];

        // effective sample size
        ESS = (stepSize * samples) / ACT;

        // standard deviation of autocorrelation time
        stdErrOfACT = (2.0* Math.sqrt(2.0*(2.0*(double) (maxLag+1))/samples)*(varStat/gammaStat[0])*stepSize);

        isValid = true;
    }<commit_msg>remove unused variables<commit_after>package dr.app.tracer.traces;

/**
 * A class that stores the correlation statistics for a trace
 *
 * @author <NAME>
 * @author <NAME>
 * @version $Id: TraceCorrelation.java,v 1.2 2006/11/29 14:53:53 rambaut Exp $
 */
public class TraceCorrelation extends TraceDistribution {

    public TraceCorrelation(double[] values, int stepSize) {
        super(values, stepSize);

        if (isValid) {
            analyseCorrelation(values, stepSize);
        }
    }

    public double getStdErrorOfMean() { return stdErrorOfMean; }
    public double getACT() { return ACT; }

    private final static int MAX_OFFSET = 1000;

    /**
     * Analyze trace
     */
    private void analyseCorrelation(double[] values, int stepSize) {

        int maxLag = MAX_OFFSET;
        int samples = values.length;
        if ((samples/3) < maxLag) {
            maxLag = (samples/3);
        }

        double[] gammaStat = new double[maxLag];
        double[] varGammaStat = new double[maxLag];
        double varStat = 0.0;
        //double varVarStat = 0.0;
        //double assVarCor = 1.0;
        double del1, del2;

        for (int lag=0; lag < maxLag; lag++) {
            for (int j = 0; j < samples-lag; j++) {
                del1=values[j] - mean;
                del2=values[j + lag] - mean;
                gammaStat[lag] += ( del1*del2 );
                varGammaStat[lag] += (del1*del1*del2*del2);
            }

            gammaStat[lag] /= ((double)(samples-lag));
            varGammaStat[lag] /= ((double) samples-lag);
            varGammaStat[lag] -= (gammaStat[0] * gammaStat[0]);

            if (lag==0) {
                varStat = gammaStat[0];
                //varVarStat = varGammaStat[0];
                //assVarCor = 1.0;
            }
            else if (lag%2==0)
            {
                // fancy stopping criterion :)
                if (gammaStat[lag-1] + gammaStat[lag] > 0) {
                    varStat    += 2.0*(gammaStat[lag-1] + gammaStat[lag]);
                   // varVarStat += 2.0*(varGammaStat[lag-1] + varGammaStat[lag]);
                   // assVarCor  += 2.0*((gammaStat[lag-1] * gammaStat[lag-1]) + (gammaStat[lag] * gammaStat[lag])) / (gammaStat[0] * gammaStat[0]);
                }
                // stop
                else
                    maxLag=lag;
            }
        }

        // standard error of mean
        stdErrorOfMean = Math.sqrt(varStat/samples);

        // auto correlation time
        ACT = stepSize * varStat / gammaStat[0];

        // effective sample size
        ESS = (stepSize * samples) / ACT;

        // standard deviation of autocorrelation time
        stdErrOfACT = (2.0* Math.sqrt(2.0*(2.0*(double) (maxLag+1))/samples)*(varStat/gammaStat[0])*stepSize);

        isValid = true;
    }
{'fraction_non_alphanumeric': 0.13097787294789437, 'fraction_numerical': 0.02569593147751606, 'mean_word_length': 1.7515954835542464, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, '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': '1361773', 'n_tokens_mistral': 1827, 'n_tokens_neox': 1651, 'n_words': 382}