How to resolve the algorithm Verify distribution uniformity/Chi-squared test step by step in the D programming language

Published on 12 May 2024 09:40 PM
#D

How to resolve the algorithm Verify distribution uniformity/Chi-squared test step by step in the D programming language

Table of Contents

Problem Statement

Write a function to determine whether a given set of frequency counts could plausibly have come from a uniform distribution by using the

χ

2

{\displaystyle \chi ^{2}}

test with a significance level of 5%.
The function should return a boolean that is true if and only if the distribution is one that a uniform distribution (with appropriate number of degrees of freedom) may be expected to produce. Note: normally a two-tailed test would be used for this kind of problem.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Verify distribution uniformity/Chi-squared test step by step in the D programming language

Source code in the d programming language

import std.stdio, std.algorithm, std.mathspecial;

real x2Dist(T)(in T[] data) pure nothrow @safe @nogc {
    immutable avg = data.sum / data.length;
    immutable sqs = reduce!((a, b) => a + (b - avg) ^^ 2)(0.0L, data);
    return sqs / avg;
}

real x2Prob(in real dof, in real distance) pure nothrow @safe @nogc {
    return gammaIncompleteCompl(dof / 2, distance / 2);
}

bool x2IsUniform(T)(in T[] data, in real significance=0.05L)
pure nothrow @safe @nogc {
    return x2Prob(data.length - 1.0L, x2Dist(data)) > significance;
}

void main() {
    immutable dataSets = [[199809, 200665, 199607, 200270, 199649],
                          [522573, 244456, 139979,  71531,  21461]];
    writefln(" %4s %12s  %12s %8s   %s",
             "dof", "distance", "probability", "Uniform?", "dataset");
    foreach (immutable ds; dataSets) {
        immutable dof = ds.length - 1;
        immutable dist = ds.x2Dist;
        immutable prob = x2Prob(dof, dist);
        writefln("%4d %12.3f  %12.8f    %5s    %6s",
                 dof, dist, prob, ds.x2IsUniform ? "YES" : "NO", ds);
    }
}


  

You may also check:How to resolve the algorithm A+B step by step in the Gema programming language
You may also check:How to resolve the algorithm 24 game step by step in the Erlang programming language
You may also check:How to resolve the algorithm Jump anywhere step by step in the Ruby programming language
You may also check:How to resolve the algorithm Sorting algorithms/Gnome sort step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the Fantom programming language