How to resolve the algorithm Statistics/Basic step by step in the Ruby programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Statistics/Basic step by step in the Ruby programming language

Table of Contents

Problem Statement

Statistics is all about large groups of numbers.
When talking about a set of sampled data, most frequently used is their mean value and standard deviation (stddev).
If you have set of data xi where i =1,2,...n

When examining a large quantity of data, one often uses a histogram, which shows the counts of data samples falling into a prechosen set of intervals (or bins).
When plotted, often as bar graphs, it visually indicates how often each data value occurs. Task Using your language's random number routine, generate real numbers in the range of [0, 1]. It doesn't matter if you chose to use open or closed range.
Create 100 of such numbers (i.e. sample size 100) and calculate their mean and stddev.
Do so for sample size of 1,000 and 10,000, maybe even higher if you feel like.
Show a histogram of any of these sets.
Do you notice some patterns about the standard deviation? Extra Sometimes so much data need to be processed that it's impossible to keep all of them at once. Can you calculate the mean, stddev and histogram of a trillion numbers? (You don't really need to do a trillion numbers, just show how it can be done.) For a finite population with equal probabilities at all points, one can derive: Or, more verbosely. See also: Statistics/Normal distribution

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Statistics/Basic step by step in the Ruby programming language

The Ruby code snippet you provided is a function that generates statistics for a given number of random numbers.

The function takes one argument, n, which is the number of random numbers to generate.

The function initializes three variables:

  • sum to 0.0,
  • sum2 to 0.0,
  • and hist to an array of 10 zeros.

The function then iterates n times, generating a random number r each time.

The function adds r to sum and r**2 to sum2.

The function also increments the element of hist corresponding to the index (10*r).to_i.

After the loop, the function calculates the mean and standard deviation of the generated numbers.

The mean is calculated as sum / n.

The standard deviation is calculated as Math::sqrt((sum2 / n) - mean**2).

The function then prints the size, mean, and standard deviation of the generated numbers.

The function also prints a histogram of the generated numbers.

The histogram shows the number of times each value of (10*r).to_i occurred.

The function prints the histogram using the each_with_index method of the Array class.

The each_with_index method iterates over the elements of an array and passes both the element and the index of the element to a block of code.

In this case, the block of code calculates the percentage of times the element occurred and prints a string of equal signs that is 70 times the percentage of times the element occurred.

The function prints the histogram using the puts method of the IO class.

The puts method prints a string to the standard output.

The function calls the generate_statistics function with each of the numbers 100, 1000, and 10000 as arguments.

Source code in the ruby programming language

def generate_statistics(n)
  sum = sum2 = 0.0
  hist = Array.new(10, 0)
  n.times do
    r = rand
    sum += r
    sum2 += r**2
    hist[(10*r).to_i] += 1
  end
  mean = sum / n
  stddev = Math::sqrt((sum2 / n) - mean**2)
  
  puts "size: #{n}"
  puts "mean:   #{mean}"
  puts "stddev: #{stddev}"
  hist.each_with_index {|x,i| puts "%.1f:%s" % [0.1*i, "=" * (70*x/hist.max)]}
  puts
end

[100, 1000, 10000].each {|n| generate_statistics n}


  

You may also check:How to resolve the algorithm Musical scale step by step in the Lilypond programming language
You may also check:How to resolve the algorithm Colour bars/Display step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Kronecker product step by step in the Stata programming language
You may also check:How to resolve the algorithm Sierpinski triangle step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Handle a signal step by step in the Go programming language