How to resolve the algorithm Fivenum step by step in the Julia programming language
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:
-
mediansorted
Function:- It takes three parameters: an abstract vector
x
, an indexi
(representing the starting index), and an indexl
(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 anArgumentError
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 usesx[mid]
as the median. - If
len
is even, it computes the median as(x[mid-1] + x[mid]) / 2
.
- If
- It takes three parameters: an abstract vector
-
fivenum
Function:- It takes one parameter:
x
, an abstract vector of typeT
whereT
is constrained to be a floating-point type (AbstractFloat
). - It creates an empty vector
r
of typeVector{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 inxs
. - It calculates the middle index
mid
of the sorted vectorxs
aslength(xs) ÷ 2
. - It calculates
lowerend
, which is set tomid
if the length ofxs
is odd andmid - 1
if it is even. - It computes the five-number summary statistics:
r[1]
is set to the minimum value ofxs
(xs[1]
).r[2]
is set to the median of the lower half ofxs
(mediansorted(xs, 1, lowerend)
).r[3]
is set to the median of the entire sorted vectorxs
(mediansorted(xs, 1, endof(xs))
).r[4]
is set to the median of the upper half ofxs
(mediansorted(xs, mid, endof(xs))
).r[end]
is set to the maximum value ofxs
(xs[end]
).
- Finally, it returns the vector
r
containing the five-number summary: minimum, first quartile (Q1), median (Q2), third quartile (Q3), and maximum.
- It takes one parameter:
In the provided examples, the code calculates and prints the five-number summary for three different input vectors:
[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]
- 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