Calculate interquartile mean from Ruby array? - ruby

I have this array:
[288.563044, 329.835918, 578.622569, 712.359026, 866.614253, 890.066321, 1049.78037, 1070.29897, 2185.443662, 2492.245562, 4398.300227, 13953.264379]
How do I calculate the interquartile mean from this?
That Wikipedia link explains it best, but I basically need to remove the bottom and top 25% leaving only the middle 50%, of which I'll need to average the numbers.
But that's assuming the number of array items is divisible by 4. Here's how to calculate it when it's not divisible by four.
So how would I do that as well?

This is a partial solution for an array with a number of elements that is a multiple of 4. I'll put the full one when I figure it out.
arr = [288.563044, 329.835918, 578.622569, 712.359026, 866.614253, 890.066321, 1049.78037, 1070.29897, 2185.443662, 2492.245562, 4398.300227, 13953.264379].sort!
length = arr.size
mean = arr.sort[(length/4)..-(length/4+1)].inject(:+)/(length/2)
I think this is a better solution.
def interquartile_mean(array)
arr = array.sort
length = arr.size
quart = (length/4.0).floor
fraction = 1-((length/4.0)-quart)
new_arr = arr[quart..-(quart + 1)]
(fraction*(new_arr[0]+new_arr[-1]) + new_arr[1..-2].inject(:+))/(length/2.0)
end

The simple case array_size mod 4 = 0:
xs = [5, 8, 4, 38, 8, 6, 9, 7, 7, 3, 1, 6]
q = xs.size / 4
ys = xs.sort[q...3*q]
mean = ys.inject(0, :+) / ys.size.to_f
#=> 6.5
The general case (array_size >= 4):
xs = [1, 3, 5, 7, 9, 11, 13, 15, 17]
q = xs.size / 4.0
ys = xs.sort[q.ceil-1..(3*q).floor]
factor = q - (ys.size/2.0 - 1)
mean = (ys[1...-1].inject(0, :+) + (ys[0] + ys[-1]) * factor) / (2*q)
#=> 9.0
However, if you don't try to code it yourself this won't help much...

An improvement on tokland's answer that augments the Array class and fixes an edge case (method as written blows up with array size of 4).
class Array
def interquartile_mean
a = sort
l = size
quart = (l.to_f / 4).floor
t = a[quart..-(quart + 1)]
t.inject{ |s, e| s + e }.to_f / t.size
end
end

Related

Min, Max, Average, and Median of All Possible Sums (Ruby)

I've been using a piece of Ruby code that I found here.
Here's the code:
a = [1, 4, 7, 13]
def add(ary, idx, sum)
(idx...ary.length).each do |i|
add(ary, i+1, sum + ary[i])
end
puts sum
end
add(a, 0, 0)
Thing is, I don't need it to spit out the results of adding all the sums. I need the min, max, median, and average of the sums.
How do I modify this code in order to get them? I'm a total beginner at Ruby. I've been using this code, and then transferring the results to Excel to get the values I want. But it feels like my methods could be more efficient.
Thank you for your help.
EDIT: Expected results - Currently the code spits this out on my screen:
25
12
18
5
21
8
14
1
24
11
17
4
20
7
13
0
I want it to spit out the min, average, median, and max instead:
0
12.5
12.5
25
a = [1, 4, 7, 13]
def all_sums(array)
combination_lengths = (0..array.length)
all_combinations = combination_lengths.flat_map do |c|
array.combination(c).to_a
end
all_combinations.map(&:sum)
end
def print_min_max_avg_med(array)
puts array.min
puts array.max
puts array.sum.to_f / array.length
sorted_arr = array.sort
puts sorted_arr[(array.length - 1) / 2] + sorted_arr[array.length / 2] / 2.0
end
print_min_max_avg_med(all_sums(a))
Ok, instead of outputting the values we can store them in an arrary and use that array for the values you need.
(edited after chewing out by Stefan Pochmann)
a = [1, 4, 7, 13]
def add(ary, idx, sum, results = nil)
unless results
results = []
first_run = true
end
(idx...ary.length).each do |i|
add(ary, i+1, sum + ary[i], results)
end
results << sum
if first_run
puts results.min
puts results.inject(&:+).to_f / results.size
puts (results.sort[((results.size - 1) / 2)] + results.sort[(results.size / 2)]) / 2.0
puts results.max
end
end
add(a, 0, 0)
Alright, after seeing the examples from Pochmann and Bronca, I put this together after googling for a better way to get the median.
a = [1, 4, 7, 13]
def all_sums(array)
combination_lengths = (0..array.length)
all_combinations = combination_lengths.flat_map do |c|
array.combination(c).to_a
end
all_combinations.map(&:sum)
end
def median(array)
sorted = array.sort
len = sorted.length
(sorted[(len - 1) / 2] + sorted[len / 2]) / 2.0
end
def print_min_max_avg_med(array)
puts array.min
puts array.empty? ? 0 : array.sum.to_f / array.length
puts median(array)
puts array.max
end
print_min_max_avg_med(all_sums(a))
I've run a few tests, and it seems to work for both odd and even arrays. Hope this is useful to the future somebody else stuck in my present position.
Thank you everyone who helped.
Min and Max
The min and max are easy.
def min_and_max_of_sums a
return [nil, nil] if a.empty?
negs, nonnegs = a.partition { |n| n < 0 }
[negs.any? ? negs.sum : nonnegs.min, nonnegs.any? ? nonnegs.sum : negs.max]
end
min_and_max_of_sums [1, 4, -5, 7, -8, 13]
#=> [-13, 25]
min_and_max_of_sums [1, 2, 3]
#=> [1, 6]
min_and_max_of_sums [-1, -2, -3]
#=> [-6, -1]
min_and_max_of_sums []
#=> [nil, nil]
Mean
Now consider the calculation of the mean.
If n is the size of the array a, there are 2n combinations of elements of a that contain between 0 and n elements.1 Moreover, there is a 1-1 mapping between each of those combinations and an n-vector of zeros and ones, where the ith element of the n-vector equals 1 if and only if the element ai is included in the combination. Note that there are 2n such n-vectors, one-half containing a 1 in the ith position. This means that one-half of the combinations contain the element ai. As i is arbitrary, it follows that each element of a appears in one-half of the combinations.
The mean of the sums of all elements of all combinations equals T/2n, where T is the sum of the sums of the elements of each combination. Each element ai appears in 2n/2 combinations, so its contribution to T equals (in Ruby terms)
a[i] * 2**(n)/2
As this hold for every element of a, the mean equals
a.sum * (2**(n)/2)/2**(n)
=> a.sum/2
Here's an example. For the array
a = [1, 4, 8]
the mean of the sums would be
a.sum/2
#=> 13/2 => 6.5
If we were to calculate the mean by its definition we would perform the following calculation (and of course get the same return value).
(0 + (1) + (4) + (8) + (1+4) + (1+8) + (4+8) + (1=4+8))/2**3
#=> (4*1 + 4*4 + 4*8)/8
#=> (1 + 4 + 8)/2
#=> 6.5
I will leave the calculating of the median to others.
1 Search for "Sums of the binomial coefficients" here.

How to 'reverse sum' in Ruby?

I have no clue how to call this in correct math-terms. Consider a method which takes two digits:
def num_of_sum(total, group_count)
end
where total is an integer and group_count is an integer.
How would I get a 'nicely' grouped Array of integers of group_count-length which sum up till total.
My spec would look like:
describe "number to sum of" do
it "grabs all numbers" do
expect(num_of_sum(10, 2)).to eq([5,5])
expect(num_of_sum(10, 3)).to eq([3,3,4])
expect(num_of_sum(20, 3)).to eq([6,7,7])
expect(num_of_sum(100, 3)).to eq([33,33,34])
expect(num_of_sum(100, 2)).to eq([50,50])
end
end
I tried this, which works:
def num_of_sum(total, in_groups_of)
result = []
section_count ||= (total.to_f / in_groups_of.to_f).round
while(total > 0)
total -= section_count
if (total - section_count) < 0 && (total + section_count).even?
section_count += total
total -= total
end
result << section_count
end
result
end
But, for instance, this spec doesn't work:
expect(num_of_sum(67,5)).to eq([13,13,13,14,14])
I need the array to contain numbers that are as close to each other as possible. But the array is limited to the length of the group_count.
Does someone know what the mathemetical name for this is, so I can search a bit more accurately?
The mathematical term for this is an integer partition
A more direct approach to this is to observe that if you do integer division (round down) of the total by the number of groups, then your sum would be short by total mod number_of_groups, so you just need to distribute that amount across the array:
def even_partition(total, number_of_groups)
quotient, remainder = total.divmod(number_of_groups)
(number_of_groups-remainder).times.collect {quotient} +
remainder.times.collect { quotient + 1}
end
def n_parts(num, groupcount)
div, mod = num.divmod(groupcount)
Array.new(groupcount-mod, div) + Array.new(mod, div+1)
end
n_parts(100,3) => [33, 33, 34]
Docs to Array.new and Fixnum.divmod
A naive implementation is like this:
Let's take example of (20, 3). You want three numbers as a result.
20 / 3 # => 6
This is your "base" value. Create an array of three sixes, [6, 6, 6]. That'll get you 18. Now you have to distribute remaining 2 as equally as possible. For example, enumerate array elements and increment each one by 1, until you have no value to distribute. Result is [7, 7, 6]. Good enough, I think.
Possible (working) implementation:
def breakdown(total, group_count)
avg_value, extra = total.divmod(group_count)
result = Array.new(group_count, avg_value)
extra.times do |i|
result[i] += 1
end
result
end
breakdown(10, 2) == [5, 5] # => true
breakdown(10, 3) == [4, 3, 3] # => true
breakdown(20, 3) # => [7, 7, 6]
I have no clue how it’s called, but here is a solution:
def num_of_sum sum, count
result = [i = sum / count] * count # prepare an array e.g. [3,3,3] for 10,3
result[sum - i * count..-1] + # these should be left intact
result[0...sum - i * count].map { |i| i + 1 } # these are ++’ed
end
Hope it helps.
Another way:
def floors_then_ceils(n, groups)
floor, ceils = n.divmod(groups)
groups.times.map { |i| (i < groups-ceils) ? floor : floor + 1 }
end
floors_then_ceils(10, 3)
#=> [3, 3, 4]
floors_then_ceils(9, 3)
#=> [3, 3, 3]
Alternatively, groups.times.map... could be replaced with:
Array.new(groups-ceils, floor).concat(Array.new(ceils, floor+1))

Splitting an array into two subarrays with minimal absolute difference

For example, consider array A:
[3, 1, 2, 4, 3]
I can split this array in four places:
P = 1, difference = |3 − 10| = 7
P = 2, difference = |4 − 9| = 5
P = 3, difference = |6 − 7| = 1
P = 4, difference = |10 − 3| = 7
The minimal difference is 1.
Each element of array is an integer within the range [−1,000..1,000].
Below is the code I wrote. My code is correct for this exercise, and is correct for the given input but incorrect in some other cases. I do not understand why.
def minimaldifference(a)
sumArr = []
sumArr = sumfromstarting(a)
sum = sumArr.last
sumRev = sumFromReverse(a, sum)
size = sumArr.size-2
min = 0
v = 0
for i in 0..size
if(i==0)
min = (sumArr[i] - sumRev[i]).abs
else
v = (sumArr[i] - sumRev[i]).abs
if( v < min)
min = v
end
end
end
min
end
def sumfromstarting(a)
sumArr = []
sum = 0
a.each do |i|
sum += i
sumArr.push(sum)
end
sumArr
end
def sumFromReverse(a, sum)
sumArr = []
rsum = 0
a.each do |i|
rsum += i
sumArr.push(sum - i)
end
sumArr
end
a = [3, 1, 2, 4, 3]
puts minimaldifference(a)
This solution requires that all elements of the array be non-negative, which I understand is the case.
Consider
a = [4,1,3,1,2,4,3,1,5]
The possible partitions of this array are as follows:
Partition Totals Diff Abs Diff
[4], [1,3,1,2,4,3,1,5] [4, 20]
[4,1], [3,1,2,4,3,1,5] [4+1, 20-1] =>[5, 19] -14 14
[4,1,3], [1,2,4,3,1,5] [5+3, 19-3] =>[8, 16] -8 8
[4,1,3,1], [2,4,3,1,5] [8+1, 16-1] =>[9, 15] -6 6
[4,1,3,1,2], [4,3,1,5] [9+2, 15-2] =>[11, 13] -2 2
[4,1,3,1,2,4], [3,1,5] [11+4, 13-4]=>[15, 9] 6 6
[4,1,3,1,2,4,3], [1,5] [15+3, 9-3] =>[18, 6] 12 12
[4,1,3,1,2,4,3,1], [5] [18+1, 6-1] =>[19, 5] 14 14
In each row of this table "Diff" equals the difference between the totals for the two sets. Because all elements of a are non-negative, these values are monotonically non-decreasing. The absolute differences are therefore non-increasing then non-decreasing. It follows that to obtain the minimum absolute difference of totals we simply step through this array, stopping when the absolute difference would increase if the next element were moved from the "right" array to the "left" array.
We can implement this in code as follows.
def minimize_difference(arr)
raise ArgumentError, "array must contain at least two elements" if arr.size < 2
left, *right = arr
last = left - right.reduce(:+)
left = [left]
while right.any?
test = last + 2*right.first
break if test.abs > last.abs
last = test
left << right.shift
end
[left, right, last.abs]
end
minimize_difference [3, 1, 2, 4, 3]
#=> [[3, 1, 2], [4, 3], 1]

Rounding a number to nearest fractional eighth or third in Ruby

I want to round any given number to an eighth or a third in Ruby, whichever is closest.
I'm hoping for output like 1/8 or 2/3.
I've tried the following:
scalar_in_eighths = (scalar * 8.0).round / 8.0
scalar_in_thirds = (scalar * 3.0).round / 3.0
thirds_difference = (scalar - scalar_in_thirds).abs
eighths_difference = (scalar - scalar_in_eighths).abs
compute_in_thirds = thirds_difference < eighths_difference
if compute_in_thirds
less_than_eighth = false
rounded_scalar = scalar_in_thirds
else
less_than_eighth = false
rounded_scalar = scalar_in_eighths
end
quotient, modulus = rounded_scalar.to_s.split '.'
quotient = quotient.to_f
modulus = ".#{modulus}".to_f
This works well for eights, but for numbers like 1.32 it breaks down.
Doing modulus.numerator and modulus.denominator for the fractional components will yield numbers like 6004799503160661 and 18014398509481984.
Is there a better way to solve this?
Here's one way you could write it.
Code
def closest_fraction(f,*denominators)
n, frac = denominators.map { |n| [n, round_to_fraction(f,n)] }
.min_by { |_,g| (f-g).abs }
[(n*frac).round, n, frac]
end
def round_to_fraction(f,n)
(f*n).round/n.to_f
end
Examples
closest_fraction(2.33, 3, 8)
#=> [7, 3, 2.3333333333333335]
closest_fraction(2.12, 3, 8)
#=> [17, 8, 2.125]
closest_fraction(2.46, 2, 3, 5)
#=> [5, 2, 2.5]
closest_fraction(2.76, 2, 3, 5, 7, 11, 13, 17)
#=> [47, 17, 2.764705882352941]

Ruby: Multiply all elements of an array

Let's say I have an array A = [1, 2, 3, 4, 5]
how can I multiply all elements with ruby and get the result? 1*2*3*4*5 = 120
and what if there is an element 0 ? How can I ignore this element?
This is the textbook case for inject (also called reduce)
[1, 2, 3, 4, 5].inject(:*)
As suggested below, to avoid a zero,
[1, 2, 3, 4, 5].reject(&:zero?).inject(:*)
There is also another way to calculate this factorial!
Should you want to, you can define whatever your last number is as n.
In this case, n=5.
From there, it would go something like this:
(1..num).inject(:*)
This will give you 120. Also, .reduce() works the same way.
Well, this is a dummy way but it works :)
A = [1, 2, 3, 4, 5]
result = 1
A.each do |i|
if i!= 0
result = result*i
else
result
end
end
puts result
If you want to understand your code later on, use this: Assume A = 5, I used n instead of A
n = 5
n.times {|x| unless x == 0; n = n * x; ++x; end}
p n
To carry it forward, you would:
A = [1,2,3,4,5]
arb = A.first
a = A.count
a.times {|x| arb = arb * A[x]; ++x}
p arb

Resources