How to resolve the algorithm Fivenum step by step in the Julia programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Fivenum step by step in the Julia programming language

Table of Contents

Problem Statement

Many big data or scientific programs use boxplots to show distributions of data.   In addition, sometimes saving large arrays for boxplots can be impractical and use extreme amounts of RAM.   It can be useful to save large arrays as arrays with five numbers to save memory. For example, the   R   programming language implements Tukey's five-number summary as the fivenum function.

Given an array of numbers, compute the five-number summary.

While these five numbers can be used to draw a boxplot,   statistical packages will typically need extra data. Moreover, while there is a consensus about the "box" of the boxplot,   there are variations among statistical packages for the whiskers.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Fivenum step by step in the Julia programming language

The provided Julia code defines two functions: mediansorted and fivenum. Here's how these functions work:

  1. mediansorted Function:

    • It takes three parameters: an abstract vector x, an index i (representing the starting index), and an index l (representing the ending index).
    • It calculates the length of the sub-array as len = l - i + 1.
    • It checks if len > zero(len); if not, it throws an ArgumentError indicating that the array slice cannot be empty.
    • It computes the middle index mid = i + len ÷ 2.
    • Depending on whether the length len is odd or even, it calculates the median as follows:
      • If len is odd, it uses x[mid] as the median.
      • If len is even, it computes the median as (x[mid-1] + x[mid]) / 2.
  2. fivenum Function:

    • It takes one parameter: x, an abstract vector of type T where T is constrained to be a floating-point type (AbstractFloat).
    • It creates an empty vector r of type Vector{T} with a size of 5. This vector will store the five-number summary statistics.
    • It sorts the input vector x in ascending order and stores the sorted vector in xs.
    • It calculates the middle index mid of the sorted vector xs as length(xs) ÷ 2.
    • It calculates lowerend, which is set to mid if the length of xs is odd and mid - 1 if it is even.
    • It computes the five-number summary statistics:
      • r[1] is set to the minimum value of xs (xs[1]).
      • r[2] is set to the median of the lower half of xs (mediansorted(xs, 1, lowerend)).
      • r[3] is set to the median of the entire sorted vector xs (mediansorted(xs, 1, endof(xs))).
      • r[4] is set to the median of the upper half of xs (mediansorted(xs, mid, endof(xs))).
      • r[end] is set to the maximum value of xs (xs[end]).
    • Finally, it returns the vector r containing the five-number summary: minimum, first quartile (Q1), median (Q2), third quartile (Q3), and maximum.

In the provided examples, the code calculates and prints the five-number summary for three different input vectors:

  1. [15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0]
  2. [36.0, 40.0, 7.0, 39.0, 41.0, 15.0]
  3. A vector of 20 random floating-point numbers

For each input vector, the code prints the original vector and its corresponding five-number summary.

Source code in the julia programming language

function mediansorted(x::AbstractVector{T}, i::Integer, l::Integer)::T where T
    len = l - i + 1
    len > zero(len) || throw(ArgumentError("Array slice cannot be empty."))
    mid = i + len ÷ 2
    return isodd(len) ? x[mid] : (x[mid-1] + x[mid]) / 2
end

function fivenum(x::AbstractVector{T}) where T<:AbstractFloat
    r = Vector{T}(5)
    xs = sort(x)
    mid::Int = length(xs) ÷ 2
    lowerend::Int = isodd(length(xs)) ? mid : mid - 1
    r[1] = xs[1]
    r[2] = mediansorted(xs, 1, lowerend)
    r[3] = mediansorted(xs, 1, endof(xs))
    r[4] = mediansorted(xs, mid, endof(xs))
    r[end] = xs[end]
    return r
end

for v in ([15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
          [36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
          [0.14082834,  0.09748790,  1.73131507,  0.87636009, -1.95059594,  0.73438555,
          -0.03035726,  1.46675970, -0.74621349, -0.72588772,  0.63905160,  0.61501527,
          -0.98983780, -1.00447874, -0.62759469,  0.66206163,  1.04312009, -0.10305385,
           0.75775634,  0.32566578])
    println("# ", v, "\n -> ", fivenum(v))
end


  

You may also check:How to resolve the algorithm Hello world/Web server step by step in the Haskell programming language
You may also check:How to resolve the algorithm Rock-paper-scissors step by step in the Elixir programming language
You may also check:How to resolve the algorithm AVL tree step by step in the Java programming language
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the SenseTalk programming language
You may also check:How to resolve the algorithm Compound data type step by step in the Go programming language