How to resolve the algorithm Permutation test step by step in the Ada programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Permutation test step by step in the Ada programming language

Table of Contents

Problem Statement

A new medical treatment was tested on a population of

n + m

{\displaystyle n+m}

volunteers, with each volunteer randomly assigned either to a group of

n

{\displaystyle n}

treatment subjects, or to a group of

m

{\displaystyle m}

control subjects.
Members of the treatment group were given the treatment, and members of the control group were given a placebo. The effect of the treatment or placebo on each volunteer was measured and reported in this table. Write a program that performs a permutation test to judge whether the treatment had a significantly stronger effect than the placebo.

Extremely dissimilar values are evidence of an effect not entirely due to chance, but your program need not draw any conclusions. You may assume the experimental data are known at compile time if that's easier than loading them at run time. Test your solution on the data given above.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Permutation test step by step in the Ada programming language

Source code in the ada programming language

with Ada.Text_IO; with Iterate_Subsets;

procedure Permutation_Test is

   type Group_Type is array(Positive range <>) of Positive;

   Treat_Group: constant Group_Type := (85, 88, 75, 66, 25, 29, 83, 39, 97);
   Ctrl_Group:  constant Group_Type := (68, 41, 10, 49, 16, 65, 32, 92, 28, 98);

   package Iter is new Iterate_Subsets(Treat_Group'Length, Ctrl_Group'Length);

   Full_Group: constant Group_Type(1 .. Iter.All_Elements)
     := Treat_Group & Ctrl_Group;

   function Mean(S: Iter.Subset) return Float is
      Sum: Natural := 0;
   begin
      for I in S'Range loop
         Sum := Sum + Full_Group(S(I));
      end loop;
      return Float(Sum)/Float(S'Length);
   end Mean;

   package FIO is new Ada.Text_IO.Float_IO(Float);

   T_Avg: Float := Mean(Iter.First);
   S_Avg: Float;
   S:     Iter.Subset := Iter.First;
   Equal:  Positive := 1; -- Mean(Iter'First) = Mean(Iter'First)
   Higher: Natural  := 0;
   Lower:  Natural  := 0;

begin -- Permutation_Test;
   -- first, count the subsets with a higher, an equal or a lower mean
   loop
      Iter.Next(S);
      S_Avg := Mean(S);
      if S_Avg = T_Avg then
         Equal := Equal + 1;
      elsif S_Avg >= T_Avg then
         Higher := Higher + 1;
      else
         Lower := Lower + 1;
      end if;
      exit when Iter.Last(S);
   end loop;

   -- second, output the results
   declare
      use Ada.Text_IO;
      Sum: Float := Float(Higher + Equal + Lower);
   begin
      Put("Less or Equal: ");
      FIO.Put(100.0*Float(Lower+Equal) / Sum, Fore=>3, Aft=>1, Exp=>0);
      Put(Integer'Image(Lower+Equal));
      New_Line;
      Put("More:          ");
      FIO.Put(100.0*Float(Higher) / Sum,      Fore=>3, Aft=>1, Exp=>0);
      Put(Integer'Image(Higher));
      New_Line;
   end;
end Permutation_Test;


generic
   Subset_Size, More_Elements: Positive;
package Iterate_Subsets is

   All_Elements: Positive := Subset_Size + More_Elements;
   subtype Index is Integer range 1 .. All_Elements;
   type Subset is array (1..Subset_Size) of Index;

   -- iterate over all subsets of size Subset_Size
   -- from the set {1, 2, ..., All_Element}

   function First return Subset;
   procedure Next(S: in out Subset);
   function Last(S: Subset) return Boolean;

end Iterate_Subsets;


package body Iterate_Subsets is

   function First return Subset is
      S: Subset;
   begin
      for I in S'Range loop
         S(I) := I;
      end loop;
      return S;
   end First;

   procedure Next(S: in out Subset) is
      I: Natural := S'Last;
   begin
      if S(I) < Index'Last then
         S(I) := S(I) + 1;
      else
         while S(I-1)+1 = S(I) loop
            I := I - 1;
         end loop;
         S(I-1) := S(I-1) + 1;
         for J in I .. S'Last loop
            S(J) := S(J-1) + 1;
         end loop;
      end if;
      return;
   end Next;

   function Last(S: Subset) return Boolean is
   begin
      return S(S'First) = Index'Last-S'Length+1;
   end Last;

end Iterate_Subsets;


  

You may also check:How to resolve the algorithm Straddling checkerboard step by step in the 11l programming language
You may also check:How to resolve the algorithm Empty program step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Dot product step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Boolean values step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Magnanimous numbers step by step in the BBC BASIC programming language