### Ruby Statistics Hello-World Example Source: https://github.com/estebanz01/ruby-statistics/blob/master/README.md A basic 'Hello, World!' example showcasing how to instantiate Poisson and StandardNormal distributions using different namespace conventions after requiring the gem. ```ruby require 'ruby-statistics' poisson = Distribution::Poisson.new(l) # Using Distribution alias. normal = RubyStatistics::Distribution::StandardNormal.new # Using all namespaces. ``` -------------------------------- ### Install Ruby Statistics Gem Source: https://github.com/estebanz01/ruby-statistics/blob/master/README.md Instructions for adding the ruby-statistics gem to your project's Gemfile or installing it directly using the gem command. ```ruby gem 'ruby-statistics' ``` ```bash $ bundle ``` ```bash $ gem install ruby-statistics ``` -------------------------------- ### Beta Distribution Calculations in Ruby Source: https://context7.com/estebanz01/ruby-statistics/llms.txt Illustrates the usage of the Beta distribution in Ruby, suitable for modeling random variables between 0 and 1, often used in Bayesian analysis. Examples include initializing the distribution with alpha and beta parameters, calculating probability density and cumulative probability, and retrieving distribution properties like mean and mode. It also provides an example of its application in Bayesian conversion rate estimation. ```ruby require 'ruby-statistics' # Beta distribution with alpha=2, beta=5 beta = RubyStatistics::Distribution::Beta.new(2, 5) # Probability density density = beta.density_function(0.3) # => 2.058 # Cumulative probability cumulative = beta.cumulative_function(0.3) # => 0.5798300000000001 # Distribution properties puts "Mean: #{beta.mean}" # => 0.2857142857142857 puts "Mode: #{beta.mode}" # => 0.2 # Example: Bayesian conversion rate estimation # Prior: alpha=10 successes, beta=30 failures conversion_rate = RubyStatistics::Distribution::Beta.new(10, 30) prob_less_than_30_percent = conversion_rate.cumulative_function(0.3) expected_rate = conversion_rate.mean # => 0.25 ``` -------------------------------- ### T-Test (Student's t-test) in Ruby Source: https://context7.com/estebanz01/ruby-statistics/llms.txt Provides examples of performing Student's t-tests in Ruby for hypothesis testing. It demonstrates how to perform a one-sample t-test to compare a sample mean to a population mean, and a two-sample t-test for comparing two independent groups. It also includes an example of a paired t-test for related samples, showing how to interpret the results including t-score, p-value, and confidence. ```ruby require 'ruby-statistics' # One-sample t-test: compare sample mean to population mean sample_data = [98.2, 97.8, 99.1, 98.5, 97.9, 98.7, 99.3, 98.1] comparison_mean = 98.6 alpha = 0.05 result = RubyStatistics::StatisticalTest::TTest.perform( alpha, :two_tail, comparison_mean, sample_data ) puts "T-Score: #{result[:t_score]}" # => -0.734... puts "P-Value: #{result[:p_value]}" # => 0.487... puts "Accept Null?: #{result[:null]}" # => true puts "Reject Null?: #{result[:alternative]}" # => false puts "Confidence: #{result[:confidence_level]}" # => 0.95 # Two-sample t-test: compare two independent groups group_a = [23, 25, 27, 29, 31, 33, 35] group_b = [18, 20, 22, 24, 26, 28, 30] result_two_sample = RubyStatistics::StatisticalTest::TTest.perform( 0.05, :two_tail, group_a, group_b ) # Paired t-test: compare related samples (before/after) before = [110, 120, 130, 140, 150] after = [105, 115, 125, 138, 145] paired_result = RubyStatistics::StatisticalTest::TTest.paired_test( 0.05, :two_tail, before, after ) puts "Paired T-Score: #{paired_result[:t_score]}" puts "Paired P-Value: #{paired_result[:p_value]}" puts "Significant Difference?: #{paired_result[:alternative]}" ``` -------------------------------- ### Uniform Distribution Calculations in Ruby Source: https://context7.com/estebanz01/ruby-statistics/llms.txt Demonstrates how to use the Uniform distribution from the Ruby Statistics library. It covers creating a uniform distribution object, calculating probability density, cumulative probability, and key properties like mean, median, and variance. It also shows an example of generating random numbers within a specified range. ```ruby require 'ruby-statistics' # Uniform distribution between 0 and 10 uniform = RubyStatistics::Distribution::Uniform.new(0, 10) # Probability density (constant within interval) density_5 = uniform.density_function(5) # => 0.1 density_outside = uniform.density_function(15) # => 0 # Cumulative probability cumulative_2_5 = uniform.cumulative_function(2.5) # => 0.25 cumulative_7 = uniform.cumulative_function(7) # => 0.7 # Distribution properties puts "Mean: #{uniform.mean}" # => 5.0 puts "Median: #{uniform.median}" # => 5.0 puts "Variance: #{uniform.variance}" # => 8.333333333333334 # Example: Random number generator between -1 and 1 rng = RubyStatistics::Distribution::Uniform.new(-1, 1) prob_positive = rng.cumulative_function(1) - rng.cumulative_function(0) # => 0.5 ``` -------------------------------- ### Binomial Distribution Calculations in Ruby Source: https://context7.com/estebanz01/ruby-statistics/llms.txt Illustrates the use of the Binomial distribution in Ruby to calculate probabilities for a fixed number of independent trials with binary outcomes. Examples include calculating the probability of exactly k successes and the cumulative probability, along with accessing mean, variance, and mode. ```ruby require 'ruby-statistics' # 20 coin flips with 50% success probability binomial = RubyStatistics::Distribution::Binomial.new(20, 0.5) # Probability of exactly k successes prob_10 = binomial.probability_mass_function(10) # => 0.17619705200195312 prob_15 = binomial.probability_mass_function(15) # => 0.014785766601562498 # Cumulative probability P(X <= k) cumulative_10 = binomial.cumulative_function(10) # => 0.5881309509277344 # Distribution properties puts "Mean: #{binomial.mean}" # => 10.0 puts "Variance: #{binomial.variance}" # => 5.0 puts "Mode: #{binomial.mode}" # => [10, 9] (can be two values) # Example: Quality control - 5% defect rate, sample of 100 items qc_sample = RubyStatistics::Distribution::Binomial.new(100, 0.05) prob_exactly_5_defects = qc_sample.probability_mass_function(5) prob_10_or_fewer_defects = qc_sample.cumulative_function(10) mean_defects = qc_sample.mean # => 5.0 ``` -------------------------------- ### Chi-Squared Distribution Calculations in Ruby Source: https://context7.com/estebanz01/ruby-statistics/llms.txt Shows how to work with the Chi-Squared distribution in Ruby, a common distribution in hypothesis testing. The examples cover creating a Chi-Squared distribution object with specified degrees of freedom, calculating probability density and cumulative probability, and accessing distribution properties like mean, mode, and variance. A special case for 2 degrees of freedom is also demonstrated. ```ruby require 'ruby-statistics' # Chi-squared distribution with 5 degrees of freedom chi_squared = RubyStatistics::Distribution::ChiSquared.new(5) # Probability density density = chi_squared.density_function(3) # => 0.1540904090916254 # Cumulative probability cumulative = chi_squared.cumulative_function(11.07) # => 0.9499999999999999 # Distribution properties puts "Mean: #{chi_squared.mean}" # => 5 puts "Mode: #{chi_squared.mode}" # => 3 puts "Variance: #{chi_squared.variance}" # => 10 # Special case: 2 degrees of freedom chi_2 = RubyStatistics::Distribution::ChiSquared.new(2) cumulative_2 = chi_2.cumulative_function(5.99) # => 0.9499336... ``` -------------------------------- ### Calculate Skewness of Gamma Distribution in Ruby Source: https://github.com/estebanz01/ruby-statistics/wiki/Gamma-Distribution Calculates the skewness of the Gamma distribution, which is solely dependent on the shape parameter. This example demonstrates that skewness is the same regardless of whether scale or rate is used for initialization. ```ruby irb(main):016> rate, scale = Distribution::Gamma.new(shape: 4), Distribution::Gamma.new(shape: 4, scale: 2) => [#, #] irb(main):031> rate.skewness == scale.skewness => true irb(main):032> rate.skewness => 1.0 irb(main):033> scale.skewness => 1.0 ``` -------------------------------- ### Poisson Distribution Calculations in Ruby Source: https://context7.com/estebanz01/ruby-statistics/llms.txt Provides examples of using the Poisson distribution in Ruby for modeling event occurrences. It covers calculating the probability mass function for exact occurrences and the cumulative function for probabilities up to a certain number of events, along with accessing mean and variance. ```ruby require 'ruby-statistics' # Create Poisson distribution with lambda (expected occurrences) = 5 poisson = RubyStatistics::Distribution::Poisson.new(5) # Probability of exactly k occurrences prob_3 = poisson.probability_mass_function(3) # => 0.14037389581428018 (P(X = 3)) prob_5 = poisson.probability_mass_function(5) # => 0.17546736976785082 (P(X = 5), the mode) # Cumulative probability P(X <= k) cumulative_3 = poisson.cumulative_function(3) # => 0.73498721171394 (P(X > 3)) cumulative_10 = poisson.cumulative_function(10) # => 0.013695096054738 (P(X > 10)) # Distribution properties puts "Mean: #{poisson.mean}" # => 5 puts "Variance: #{poisson.variance}" # => 5 # Example: Website receives average 12 visits per hour website_traffic = RubyStatistics::Distribution::Poisson.new(12) prob_exactly_15 = website_traffic.probability_mass_function(15) prob_more_than_20 = website_traffic.cumulative_function(20) ``` -------------------------------- ### Calculate Beta Distribution Mean (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/Beta-Distribution Returns the mean of the beta distribution. The mean is the expected value of the distribution. This method provides a quick way to get the average value of the distribution. ```ruby [40] pry(main)> beta_distribution => # [41] pry(main)> beta_distribution.mean => 0.5 ``` -------------------------------- ### Chi-Squared Goodness of Fit Test in Ruby Source: https://context7.com/estebanz01/ruby-statistics/llms.txt Demonstrates the Chi-Squared Goodness of Fit test in Ruby to assess if observed frequencies match expected frequencies for categorical data. Examples include testing the fairness of a die and testing with different expected frequencies per category. The code shows how to perform the test, interpret results (p-value, null hypothesis acceptance/rejection), and also how to directly calculate the chi-squared statistic and degrees of freedom. ```ruby require 'ruby-statistics' # Test if a die is fair observed = [15, 18, 12, 20, 14, 21] # Observed rolls expected = 16.67 # Expected for fair die (100/6 rolls per face) alpha = 0.05 result = RubyStatistics::StatisticalTest::ChiSquaredTest.goodness_of_fit( alpha, expected, observed ) puts "P-Value: #{result[:p_value]}" # => 0.628... puts "Accept Null?: #{result[:null]}" # => true puts "Reject Null?: #{result[:alternative]}" # => false puts "Chi-Squared Probability: #{result[:probability]}" # Test with different expected frequencies per category observed_grades = [30, 45, 60, 40, 25] expected_grades = [28, 42, 65, 38, 27] grade_result = RubyStatistics::StatisticalTest::ChiSquaredTest.goodness_of_fit( 0.05, expected_grades, observed_grades ) # Calculate chi-squared statistic directly chi_stat, df = RubyStatistics::StatisticalTest::ChiSquaredTest.chi_statistic( expected_grades, observed_grades ) puts "Chi-Squared Statistic: #{chi_stat}" puts "Degrees of Freedom: #{df}" ``` -------------------------------- ### Get Chi-Squared Mean (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/Chi-squared-Distribution Retrieves the mean of the Chi-Squared distribution, which is equivalent to the degrees of freedom specified at initialization. ```ruby chi_sq = Distribution::ChiSquared.new(3) mean = chi_sq.mean puts mean ``` -------------------------------- ### Calculate Density Function for Gamma Distribution in Ruby Source: https://github.com/estebanz01/ruby-statistics/wiki/Gamma-Distribution Computes the probability density function for a specified value based on the initialized shape and scale or rate parameters. This example illustrates its use with both parameterizations. ```ruby irb(main):016> rate, scale = Distribution::Gamma.new(shape: 4), Distribution::Gamma.new(shape: 4, scale: 2) => [#, #] irb(main):019> rate.density_function(1) => 0.0005070317598121126 irb(main):020> scale.density_function(1) => 0.006318027705339931 ``` -------------------------------- ### Calculate Mean of Gamma Distribution in Ruby Source: https://github.com/estebanz01/ruby-statistics/wiki/Gamma-Distribution Computes the mean of the Gamma distribution, which depends on the shape and scale or rate parameters. The example demonstrates this for both initialization methods. ```ruby irb(main):016> rate, scale = Distribution::Gamma.new(shape: 4), Distribution::Gamma.new(shape: 4, scale: 2) => [#, #] irb(main):021> rate.mean => 16.0 irb(main):022> scale.mean => 8 ``` -------------------------------- ### Calculate Cumulative Function for Gamma Distribution in Ruby Source: https://github.com/estebanz01/ruby-statistics/wiki/Gamma-Distribution Calculates the cumulative distribution function P(x <= X) for a given value X. This example shows calculations for both scale and rate parameterizations of the Gamma distribution. ```ruby irb(main):016> rate, scale = Distribution::Gamma.new(shape: 4), Distribution::Gamma.new(shape: 4, scale: 2) => [#, #] irb(main):017> rate.cumulative_function(1) => 0.00013336965051406234 irb(main):018> scale.cumulative_function(1) => 0.0017516225562908235 ``` -------------------------------- ### Get Mode of Normal Distribution (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/Normal-distribution Returns the mode of a Normal distribution, which is an alias for the mean accessor. Requires an instantiated Normal distribution object. ```ruby normal_dist.mode ``` -------------------------------- ### Initialize Empirical Distribution in Ruby Source: https://github.com/estebanz01/ruby-statistics/wiki/Empirical-Distribution-(ECDF) Demonstrates how to initialize an Empirical Distribution object by passing an Enumerable collection of samples. This object is used for statistical comparisons like the Kolmogorov-Smirnov Test. ```ruby Distribution::Empirical => Statistics::Distribution::Empirical Distribution::Empirical.new(samples: [1, 2, 3, 4, 5, 6, 7]) => # ``` -------------------------------- ### Get Chi-Squared Variance (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/Chi-squared-Distribution Calculates the variance of the Chi-Squared distribution, which is twice the degrees of freedom specified during initialization. ```ruby chi_sq = Distribution::ChiSquared.new(3) variance = chi_sq.variance puts variance ``` -------------------------------- ### Initialize Gamma Distribution with Scale or Rate in Ruby Source: https://github.com/estebanz01/ruby-statistics/wiki/Gamma-Distribution Demonstrates initializing the Gamma distribution using either the 'scale' or 'rate' parameter. The 'rate' parameter is defined as 1.0 / scale. The library supports both parameterizations. ```ruby irb(main):005> rate, scale = Distribution::Gamma.new(shape: 2), Distribution::Gamma.new(shape: 2, scale: 2) => [#, #] irb(main):006> rate => # irb(main):007> scale => # irb(main):008> scale.as_rate? => false irb(main):009> rate.as_rate? => true ``` -------------------------------- ### Get Mode of T-Student Distribution (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/T-student-distribution Retrieves the mode of the T-student distribution, which is always zero. This method does not require any arguments. ```ruby t_student.mode ``` -------------------------------- ### Get Chi-Squared Mode (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/Chi-squared-Distribution Calculates the mode of the Chi-Squared distribution. The mode is the maximum value between (degrees_of_freedom - 2) and zero. ```ruby chi_sq = Distribution::ChiSquared.new(3) mode = chi_sq.mode puts mode ``` -------------------------------- ### Get Variance of Normal Distribution (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/Normal-distribution Retrieves the variance of a Normal distribution, which is the square of the standard deviation. Requires an instantiated Normal distribution object. ```ruby normal_dist.variance ``` -------------------------------- ### Normal Distribution Calculations in Ruby Source: https://context7.com/estebanz01/ruby-statistics/llms.txt Demonstrates how to create and use a Normal distribution object in Ruby for calculating probability density, cumulative probabilities, and generating random samples. It also shows how to access distribution properties like mean and standard deviation. ```ruby require 'ruby-statistics' # Create a normal distribution with mean=100, std=15 normal = RubyStatistics::Distribution::Normal.new(100, 15) # Calculate probability density at a point density = normal.density_function(110) # => 0.017603266338214976 # Calculate cumulative probability P(X <= 110) cumulative = normal.cumulative_function(110) # => 0.7475074624530771 # Generate random samples single_value = normal.random # => 98.45236... (varies each run) multiple_values = normal.random(elements: 5, seed: 12345) # => [113.789..., 95.234..., 102.456..., 88.901..., 106.123...] # Access distribution properties puts "Mean: #{normal.mean}" # => 100 puts "Mode: #{normal.mode}" # => 100 puts "Variance: #{normal.variance}" # => 225 puts "Std Dev: #{normal.standard_deviation}" # => 15 ``` -------------------------------- ### Calculate Variance of Gamma Distribution in Ruby Source: https://github.com/estebanz01/ruby-statistics/wiki/Gamma-Distribution Computes the variance of the Gamma distribution. The calculation depends on the shape and scale or rate parameters. Examples are provided for both parameterizations. ```ruby irb(main):016> rate, scale = Distribution::Gamma.new(shape: 4), Distribution::Gamma.new(shape: 4, scale: 2) => [#, #] irb(main):028> rate.variance => 64.0 irb(main):029> scale.variance => 16.0 ``` -------------------------------- ### Array Statistical Extensions in Ruby Source: https://context7.com/estebanz01/ruby-statistics/llms.txt Extends Ruby's Enumerable module to provide direct calculations for mean, variance, and standard deviation on arrays. These methods simplify basic statistical analysis of datasets. Requires the 'ruby-statistics' gem. ```ruby require 'ruby-statistics' # Enumerable extensions work on any array data = [12, 15, 18, 21, 24, 27, 30, 33, 36] # Mean (average) mean = data.mean # => 24.0 # Sample variance variance = data.variance # => 72.0 # Sample standard deviation std_dev = data.standard_deviation # => 8.48528137423857 # Use in statistical tests sample_a = [45, 48, 52, 55, 58, 61, 64] sample_b = [38, 42, 46, 50, 54, 58, 62] puts "Group A Mean: #{sample_a.mean}" # => 54.714... puts "Group A Std Dev: #{sample_a.standard_deviation}" # => 6.89... puts "Group B Mean: #{sample_b.mean}" # => 50.0 puts "Group B Std Dev: #{sample_b.standard_deviation}" # => 8.48... # Combine with statistical tests t_test_result = RubyStatistics::StatisticalTest::TTest.perform( 0.05, :two_tail, sample_a, sample_b ) ``` -------------------------------- ### Get Mean of T-Student Distribution (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/T-student-distribution Returns the mean of the T-student distribution, which is zero if the degrees of freedom are greater than one. Returns nil if the degrees of freedom are less than or equal to one. ```ruby t_student.mean ``` ```ruby Distribution::TStudent.new(1).mean ``` -------------------------------- ### Calculate Mean of Uniform Distribution (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/Continuous-Uniform-distribution Returns the expected mean value for the uniform distribution, calculated based on the left and right bounds provided during initialization. ```ruby uniform_dist = Distribution::Uniform.new(3,6) uniform_dist.mean # => 4.5 ``` -------------------------------- ### Mathematical Functions in Ruby Source: https://context7.com/estebanz01/ruby-statistics/llms.txt Provides extended mathematical functions including factorial, combinations, permutations, Beta and Gamma functions (including incomplete and normalized versions), and numerical integration using Simpson's rule. These functions are useful for various statistical and mathematical computations. Requires the 'ruby-statistics' gem. ```ruby require 'ruby-statistics' # Factorial factorial_5 = Math.factorial(5) # => 120 factorial_10 = Math.factorial(10) # => 3628800.0 # Combination (n choose r) combinations = Math.combination(10, 3) # => 120 (number of ways to choose 3 from 10) # Permutation (n permute k) permutations = Math.permutation(10, 3) # => 720 (number of ordered arrangements) # Beta function beta_val = Math.beta_function(2, 3) # => 0.08333333333333333 # Incomplete beta function incomplete_beta = Math.incomplete_beta_function(0.5, 2, 3) # => 0.6875 # Lower incomplete gamma function gamma_lower = Math.lower_incomplete_gamma_function(3, 2) # => 0.646647... # Normalized lower incomplete gamma function gamma_norm = Math.normalised_lower_incomplete_gamma_function(3, 2) # => 0.323323... # Simpson's rule numerical integration integral = Math.simpson_rule(0, Math::PI, 1000) { |x| Math.sin(x) } # => 2.0 (approximately) # Example: Calculate probability manually n, k, p = 10, 3, 0.5 binomial_prob = Math.combination(n, k) * (p ** k) * ((1 - p) ** (n - k)) # => 0.1171875 ``` -------------------------------- ### Get Variance of T-Student Distribution (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/T-student-distribution Calculates the variance of the T-student distribution. Returns Float::Infinity if 1 < degrees_of_freedom <= 2, performs calculation if degrees_of_freedom > 2, and returns nil if degrees_of_freedom <= 1. ```ruby t_student.variance # degrees of freedom: 3 ``` ```ruby Distribution::TStudent.new(1).variance # Degrees of freedom: 1 ``` ```ruby Distribution::TStudent.new(2).variance # Degrees of freedom: 2 ``` -------------------------------- ### Calculate Chi-Squared Probability Density (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/Chi-squared-Distribution Calculates the probability density for a specified value based on the degrees of freedom set during initialization. Useful for understanding the distribution's shape. ```ruby chi_sq = Distribution::ChiSquared.new(3) density_values = (0..8).map { |number| chi_sq.density_function(number) } puts density_values ``` -------------------------------- ### Require Ruby Statistics Gem Source: https://github.com/estebanz01/ruby-statistics/blob/master/README.md Demonstrates different ways to require the ruby-statistics gem to load its functionality. You can load the entire gem, a specific namespace, or an individual class. ```ruby require 'ruby-statistics' ``` ```ruby require 'ruby-statistics/distribution' ``` ```ruby require 'ruby-statistics/distribution/normal' ``` -------------------------------- ### Get Alpha Values from Chi Squared Distribution Table (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/Chi-Squared-Distribution-Table Retrieves all defined alpha values from the Chi Squared distribution table. These represent common significance levels used in statistical hypothesis testing. ```ruby RubyStatistics::Distribution::Tables::ChiSquared.alpha_values ``` -------------------------------- ### Get Chi Squared Alpha Column Data (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/Chi-Squared-Distribution-Table Fetches critical values and their corresponding degrees of freedom for a specific alpha value from the Chi Squared distribution table. Raises a RuntimeError if the alpha value is not found. ```ruby RubyStatistics::Distribution::Tables::ChiSquared.alpha_column(0.01) ``` -------------------------------- ### Calculate Probability Density for Uniform Distribution (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/Continuous-Uniform-distribution Calculates the probability density for a specified value within the interval defined at initialization. Returns 0 if the value falls outside the interval. ```ruby uniform_dist = Distribution::Uniform.new(3,6) uniform_dist.density_function(4) # interval: [3, 6] # => 0.3333333333333333 uniform_dist.density_function(7) # interval: [3, 6] # => 0 ``` -------------------------------- ### Standard Normal Distribution and Inverse Calculations in Ruby Source: https://context7.com/estebanz01/ruby-statistics/llms.txt Shows how to utilize the Standard Normal distribution for z-score related calculations, including density and cumulative probabilities. It also demonstrates the use of the Inverse Standard Normal distribution for finding z-scores from probabilities. ```ruby require 'ruby-statistics' # Standard normal distribution z = RubyStatistics::Distribution::StandardNormal.new # Calculate probability density for z-score density = z.density_function(1.96) # => 0.05844094153513403 # Calculate cumulative probability (area under curve) cumulative = z.cumulative_function(1.96) # => 0.9750021048517796 # Generate random standard normal values samples = z.random(elements: 1000, seed: 42) # Inverse standard normal (quantile function) inverse = RubyStatistics::Distribution::InverseStandardNormal.new # Get z-score for a given probability z_score = inverse.cumulative_function(0.975) # => 1.959963984540054 (approximately 1.96) z_score_lower = inverse.cumulative_function(0.025) # => -1.959963984540054 ``` -------------------------------- ### Calculate Probability Density Function for Normal Distribution (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/Normal-distribution Calculates the probability density for a specified value in a Normal distribution. This is done by mapping over a range of numbers and applying the density_function method. It requires initializing a Normal distribution object. ```ruby normal_dist = Distribution::Normal.new(0, 0.447) result = (-5..5).map do |number| normal_dist.density_function(number) end ``` -------------------------------- ### Ruby Geometric Distribution Cumulative Function Calculation Source: https://github.com/estebanz01/ruby-statistics/wiki/Geometric-Distribution Calculates the cumulative probability P(x <= K) for a given K in a Geometric Distribution. The function is undefined for k <= 0 when p < 1, and for k < 0 when p <= 1. Examples show usage with and without the `always_success: true` option. ```ruby Distribution::Geometric.new(0.5) # always_success: false => # Distribution::Geometric.new(0.5).cumulative_function(0) => nil Distribution::Geometric.new(0.5).cumulative_function(4) => 0.9375 Distribution::Geometric.new(0.5).cumulative_function(2) => 0.75 Distribution::Geometric.new(0.5, always_success: true) # always_success: true => # Distribution::Geometric.new(0.5, always_success: true).cumulative_function(0) => 0.5 Distribution::Geometric.new(0.5, always_success: true).cumulative_function(4) => 0.96875 Distribution::Geometric.new(0.5, always_success: true).cumulative_function(2) => 0.875 ``` -------------------------------- ### Calculate Cumulative Function in Ruby Source: https://github.com/estebanz01/ruby-statistics/wiki/Empirical-Distribution-(ECDF) Shows how to use the `cumulative_function` method on an initialized Empirical Distribution object. This method calculates the probability P(x <= X) for a given value x. ```ruby empirical = Distribution::Empirical.new(samples: [1, 2, 3, 4, 5, 6, 7]) => # berdasarkan empirical.cumulative_function(x: 3) => 0.42857142857142855 ``` -------------------------------- ### Calculate Beta Distribution Probability Density Function (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/Beta-Distribution Calculates the probability density for a given value. If the value is outside the range [0, 1], the density defaults to zero. This method is useful for understanding the distribution's shape. ```ruby [3] pry(main)> beta_distribution = RubyStatistics::Distribution::Beta.new(2, 2) => # [4] pry(main)> results = [0, 0.2, 0.4, 0.6, 0.8, 1].map do |number| [4] pry(main)* beta_distribution.density_function(number) [4] pry(main)* end => [0.0, 0.9600000000000002, 1.44, 1.44, 0.9599999999999999, 0.0] ``` -------------------------------- ### Calculate Bernoulli Skewness in Ruby Source: https://github.com/estebanz01/ruby-statistics/wiki/Bernoulli-Distribution Calculates the skewness for the Bernoulli distribution given a probability p. ```ruby Distribution::Bernoulli.skewness(0.75) ``` -------------------------------- ### Calculate Variance of Uniform Distribution (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/Continuous-Uniform-distribution Returns the expected variance value for the uniform distribution, calculated based on the left and right bounds provided during initialization. ```ruby uniform_dist = Distribution::Uniform.new(3,6) uniform_dist.variance # => 0.75 ``` -------------------------------- ### Rank Data with Spearman's Method in Ruby Source: https://github.com/estebanz01/ruby-statistics/wiki/Spearman's-Rank-Coefficient Calculates the rank for a given dataset using Spearman's method. It can return only the ranks as an array of floats or a hash containing detailed ranking information including tie handling. This method is crucial for preparing data for correlation tests. ```Ruby RubyStatistics::SpearmanRankCoefficient.rank(data: [10, 30, 34, 340, 35]) RubyStatistics::SpearmanRankCoefficient.rank(data: [10, 30, 34, 340, 35], return_ranks_only: false) RubyStatistics::SpearmanRankCoefficient.rank(data: [10, 30, 34, 340, 35, 35, 10]) RubyStatistics::SpearmanRankCoefficient.rank(data: [10, 30, 34, 340, 35, 35, 10], return_ranks_only: false) ``` -------------------------------- ### Generate Random Samples from Normal Distribution (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/Normal-distribution Generates random numbers or samples following a Normal distribution. Can specify the number of elements and a seed for the random number generator. Defaults to one element if not specified. ```ruby normal = Distribution::Normal.new(2,3) normal.random normal.random(elements: 3) normal.random(seed: Random.new_seed) ``` -------------------------------- ### Rank Data for Mann-Whitney U Test in Ruby Source: https://github.com/estebanz01/ruby-statistics/wiki/Mann-Whitney-U-test The `rank` method processes a list of numerical elements to determine their ranks, accounting for ties. It returns a hash where keys are the unique elements and values are hashes containing the count of each element and its calculated rank, considering ties by averaging their positions. This method is essential for preparing data for the Mann-Whitney U test. ```ruby pry(main)> test = StatisticalTest::WilcoxonRankSumTest.new => # pry(main)> test.rank([1,1,2,3,4,5,6,7,7,8]) => {1=>{:counter=>2, :rank=>3}, 2=>{:counter=>1, :rank=>3}, 3=>{:counter=>1, :rank=>4}, 4=>{:counter=>1, :rank=>5}, 5=>{:counter=>1, :rank=>6}, 6=>{:counter=>1, :rank=>7}, 7=>{:counter=>2, :rank=>17}, 8=>{:counter=>1, :rank=>10}} ``` -------------------------------- ### Calculate Combinations in Ruby Source: https://github.com/estebanz01/ruby-statistics/wiki/Helper-functions Calculates the number of combinations (nCr) for a given set size N and a subset size R. This function computes 'N choose R'. ```ruby Math.combination(4, 2) # => 6.0 Math.combination(4, 4) # => 1.0 ``` -------------------------------- ### Numerical Integration with Simpson's Rule in Ruby Source: https://github.com/estebanz01/ruby-statistics/wiki/Helper-functions Implements Simpson's rule to approximate the definite integral of a function over a range [a, b], divided into an even number of N intervals. The function to integrate is provided as a block. ```ruby Math.simpson_rule(1, 10, 10_000) do |x| x**2 end # => 333.0302975699995 ``` -------------------------------- ### Calculate Probability Density Function for F Distribution (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/F-Distribution Computes the probability density for a specified value according to the F distribution. The density is undefined if either degree of freedom is zero. This method is demonstrated within a map block to generate densities for a range of numbers. ```ruby f_dist = Distribution::F.new(5, 2) results = (0..5).map do |number| f_dist.density_function(number) end ``` -------------------------------- ### Calculate Weibull Probability Density Function (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/Weibull-Distribution Calculates the probability density at a specified X. Returns 0 if X is negative. The function is undefined if the scale or shape parameters are less than or equal to 0. Requires scale and shape parameters during initialization. ```Ruby invalid_density_scale = Distribution::Weibull.new(2, 0).density_function(3) invalid_density_shape = Distribution::Weibull.new(0, 2).density_function(3) zero_density = Distribution::Weibull.new(2, 2).density_function(-3) valid_density = Distribution::Weibull.new(2, 2).density_function(3) ``` -------------------------------- ### Calculate Permutations in Ruby Source: https://github.com/estebanz01/ruby-statistics/wiki/Helper-functions Calculates the number of permutations (nPr) for a given set size N and an arrangement size R. This function computes 'N permute R'. ```ruby Math.permutation(4, 2) # => 12.0 Math.permutation(4, 4) # => 24.0 ``` -------------------------------- ### Calculate Cumulative Probability for Uniform Distribution (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/Continuous-Uniform-distribution Calculates P(x <= X) for a given value X based on the left and right bounds specified during initialization. Returns 0 if X is less than or equal to the left bound, and 1 if X is greater than or equal to the right bound. ```ruby Distribution::Uniform.new(3,4).cumulative_function(2) # => 0 Distribution::Uniform.new(3,4).cumulative_function(3.5) # => 0.5 Distribution::Uniform.new(3,4).cumulative_function(5) # => 1 ``` -------------------------------- ### Calculate Chi-Squared Cumulative Probability (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/Chi-squared-Distribution Calculates the cumulative probability P(x <= X) for a given X using the lower incomplete gamma function. Requires initializing the ChiSquared distribution with degrees of freedom. ```ruby distribution = Distribution::ChiSquared.new(4) probability = distribution.cumulative_function(2) puts probability ``` -------------------------------- ### F-Test and One-Way ANOVA in Ruby Source: https://context7.com/estebanz01/ruby-statistics/llms.txt Performs the F-test for equality of variances between two groups and calculates the One-Way ANOVA for multiple groups. It also shows how to directly compute the F-score and its degrees of freedom. Requires the 'ruby-statistics' gem. ```ruby require 'ruby-statistics' # F-Test for equality of variances (two groups) group1 = [12, 14, 16, 18, 20, 22] group2 = [10, 15, 20, 25, 30, 35] alpha = 0.05 f_result = RubyStatistics::StatisticalTest::FTest.one_way_anova( alpha, group1, group2 ) puts "P-Value: #{f_result[:p_value]}" puts "Equal Variances?: #{f_result[:null]}" puts "Different Variances?: #{f_result[:alternative]}" # One-Way ANOVA for multiple groups treatment_a = [23, 25, 27, 29, 31] treatment_b = [20, 22, 24, 26, 28] treatment_c = [18, 20, 22, 24, 26] anova_result = RubyStatistics::StatisticalTest::FTest.one_way_anova( 0.05, treatment_a, treatment_b, treatment_c ) puts "ANOVA P-Value: #{anova_result[:p_value]}" puts "Significant Difference?: #{anova_result[:alternative]}" puts "Probability: #{anova_result[:probability]}" puts "Confidence Level: #{anova_result[:confidence_level]}" # Calculate F-score directly f_score, df1, df2 = RubyStatistics::StatisticalTest::FTest.anova_f_score( treatment_a, treatment_b, treatment_c ) puts "F-Score: #{f_score}" puts "DF1: #{df1}, DF2: #{df2}" ``` -------------------------------- ### Ruby Geometric Distribution Skewness Calculation Source: https://github.com/estebanz01/ruby-statistics/wiki/Geometric-Distribution Calculates the skewness of the geometric distribution. The skewness value is the same for both strategies of probability of success. ```ruby Distribution::Geometric.new(0.75, always_success: false).skewness # always_success: false => 2.5 Distribution::Geometric.new(0.75, always_success: true).skewness # always_success: false => 2.5 ``` -------------------------------- ### Calculate Bernoulli Kurtosis in Ruby Source: https://github.com/estebanz01/ruby-statistics/wiki/Bernoulli-Distribution Calculates the kurtosis for the Bernoulli distribution given a probability p. ```ruby Distribution::Bernoulli.kurtosis(0.75) ``` -------------------------------- ### Calculate Cumulative Probability for F Distribution (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/F-Distribution Calculates the cumulative probability P(x <= X) for a given value X using the incomplete beta function. Requires initializing the F distribution with two degrees of freedom. ```ruby Distribution::F.new(3, 4).cumulative_function(2) ``` -------------------------------- ### Perform Mann-Whitney U Test in Ruby Source: https://github.com/estebanz01/ruby-statistics/wiki/Mann-Whitney-U-test The `perform` method executes the Mann-Whitney U test, comparing two groups of data. It requires an alpha value for significance level and a tail parameter (`:one_tail` or `:two_tail`). The method utilizes a Z statistic approximation for a standard normal distribution and returns a hash containing the U statistic, Z statistic, probability, p-value, alpha, confidence level, and boolean indicators for the null and alternative hypotheses. This method is crucial for hypothesis testing regarding the distribution of two independent samples. ```ruby pry(main)> group_one => [0.12819915256260872, 0.24345459073897613, 0.27517650565714014, 0.8522185144081152, 0.05471111219486524] pry(main)> group_two => [0.3272414061985621, 0.2989306116723194, 0.642664937717922] # Alpha of 0.05 and one tailored test pry(main)> test.perform(alpha = 0.05, :one_tail, group_one, group_two) => {:probability=>0.9101437525605001, :u=>3.0, :z=>-1.3416407864998738, :p_value=>0.08985624743949994, :alpha=>0.05, :null=>true, :alternative=>false, :confidence_level=>0.95} # Alpha of 0.01 and one tailored test pry(main)> test.perform(alpha = 0.01, :one_tail, group_one, group_two) => {:probability=>0.9101437525605001, :u=>3.0, :z=>-1.3416407864998738, :p_value=>0.08985624743949994, :alpha=>0.01, :null=>true, :alternative=>false, :confidence_level=>0.99} # Alpha of 0.01 and two tailored test pry(main)> test.perform(alpha = 0.01, :two_tail, group_one, group_two) => {:probability=>0.9101437525605001, :u=>3.0, :z=>-1.3416407864998738, :p_value=>0.17971249487899987, :alpha=>0.01, :null=>true, :alternative=>false, :confidence_level=>0.99} ``` -------------------------------- ### Calculate Cumulative Probability for Normal Distribution (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/Normal-distribution Calculates the cumulative probability P(x <= X) for a given value X in a Normal distribution. Requires instantiating the Normal distribution with mean and standard deviation. ```ruby Distribution::Normal.new(3, 4).cumulative_function(9) ``` -------------------------------- ### Calculate Skewness of Negative Binomial Distribution in Ruby Source: https://github.com/estebanz01/ruby-statistics/wiki/Negative-Binomial-Distribution Calculates the skewness of the Negative Binomial distribution. Requires the 'statistics' gem. ```ruby require 'statistics' # Example usage: # Initialize with number of failures and probability per trial dist = Distribution::NegativeBinomial.new(3, 0.5) # Calculate the skewness skewness_value = dist.skewness puts "Skewness of the distribution: #{skewness_value}" # Output: 1.2247448713915892 ``` -------------------------------- ### Calculate Beta Distribution Mode (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/Beta-Distribution Returns the mode of the beta distribution. The mode represents the peak of the distribution. Note that the mode is undefined when alpha <= 1 and beta <= 1. ```ruby [35] pry(main)> beta_distribution => # [36] pry(main)> beta_distribution.mode => 0.5 [37] pry(main)> RubyStatistics::Distribution::Beta.new(0.5, 0.5).mode => nil ``` -------------------------------- ### Calculate Weibull Cumulative Function (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/Weibull-Distribution Computes the cumulative distribution function P(x <= X) for a given X. If X is less than 0, the probability is zero. Requires scale and shape parameters during initialization. ```Ruby distribution = Distribution::Weibull.new(2, 3) probability_at_neg_1 = distribution.cumulative_function(-1) probability_at_5 = distribution.cumulative_function(5) ``` -------------------------------- ### Handle Identical Samples in Paired T-test (Ruby) Source: https://github.com/estebanz01/ruby-statistics/wiki/Student's-T-test Illustrates the StandardError raised when performing a paired T-test if both input samples are identical. This indicates that no difference can be calculated between the paired observations. ```ruby StatisticalTest::TTest.paired_test(alpha = 0.01, :two_tail, [1.0, 2.0, 3.0], [1.0, 2.0, 3.0]) ```