BVB Source Codes

Algorithm-Implementations Show Average_test.java Source code

Return Download Algorithm-Implementations: download Average_test.java Source code - Download Algorithm-Implementations Source code - Type:.java
  1. package average;
  2.  
  3. import org.junit.Test;
  4. import java.util.stream.DoubleStream;
  5. import static org.junit.Assert.*;
  6.  
  7. /**
  8.  * @author Joshua Moody (joshimoo@hotmail.de)
  9.  */
  10. public class AverageTest {
  11.     private static final double DELTA = 0.01;
  12.  
  13.     @Test
  14.     public void testAverageInequality() throws Exception {
  15.         double[] numbers = new double[] { 1, 2, 3, 4, 5 };
  16.  
  17.         /**
  18.          * Mean Inequalities:
  19.          * Some means are in a constant relationship to one another.
  20.          * If we denote the arithmentic mean of x and y by A,
  21.          * their geometric mean by G,
  22.          * their harmonic mean by H,
  23.          * their root mean square (quadratic mean) by R,
  24.          * and their contraharmonic mean by C,
  25.          *
  26.          * then the following chain of inequalities is always true
  27.          * C >= R >= A >= G >= H
  28.          */
  29.  
  30.         double contraHarmonic = Average.contraHarmonicMean(numbers);
  31.         double quadratic = Average.quadraticMean(numbers);
  32.         double arithmetic = Average.arithmeticMean(numbers);
  33.         double geometric = Average.geometricMean(numbers);
  34.         double harmonic = Average.harmonicMean(numbers);
  35.         assertTrue("Your Average inequalities are not correct", (contraHarmonic >= quadratic) && (quadratic >= arithmetic) && (arithmetic >= geometric) && (geometric >= harmonic));
  36.     }
  37.  
  38.     @Test
  39.     public void testArithmeticMean() throws Exception {
  40.         double[] numbers = new double[] { 1, 2, 3, 4, 5 };
  41.         double expected = DoubleStream.of(numbers).average().getAsDouble();
  42.         assertEquals(expected, Average.arithmeticMean(numbers), DELTA);
  43.     }
  44.  
  45.     @Test
  46.     public void testWeightedMean() throws Exception {
  47.         double[] numbers = new double[] { 1, 2, 3, 4, 5 };
  48.         double[] weights = new double[] { 1, 1, 1, 1, 1 };
  49.         double expected = DoubleStream.of(numbers).average().getAsDouble();
  50.         assertEquals(expected, Average.weightedMean(numbers, weights), DELTA);
  51.     }
  52.  
  53.     @Test
  54.     public void testHarmonicMean() throws Exception {
  55.         // n/(1/x1 + 1/x2 + ... + 1/xn)
  56.         double[] numbers = new double[] { 1, 2, 3, 4, 5 };
  57.         double expected = numbers.length / DoubleStream.of(numbers).map(x -> 1.0 / x).sum();
  58.         assertEquals(expected, Average.harmonicMean(numbers), DELTA);
  59.     }
  60.  
  61.     @Test
  62.     public void testContraHarmonicMean() throws Exception {
  63.         double[] numbers = new double[] { 1, 2, 3, 4, 5 };
  64.         double expected = DoubleStream.of(numbers).map(x -> Math.pow(x, 2.0)).sum() / DoubleStream.of(numbers).sum();
  65.         assertEquals(expected, Average.contraHarmonicMean(numbers), DELTA);
  66.     }
  67.  
  68.     @Test
  69.     public void testGeometricMean() throws Exception {
  70.         // (x1*x2*...xn) ^ (1/n)
  71.         double[] numbers = new double[] { 1, 2, 3, 4, 5 };
  72.         double expected = Math.pow((numbers[0] * numbers[1] * numbers[2] * numbers[3] * numbers[4]), 1.0 / numbers.length);
  73.         assertEquals(expected, Average.geometricMean(numbers), DELTA);
  74.     }
  75.  
  76.     @Test
  77.     public void testQuadraticMean() throws Exception {
  78.         // sqrt((x1)^2+(x2)^2+(x3)^2+...+(xn)^2 /n)
  79.         double[] numbers = new double[] { 1, 2, 3, 4, 5 };
  80.         double expected = Math.sqrt(DoubleStream.of(numbers).map(x -> Math.pow(x, 2)).sum() / numbers.length);
  81.         assertEquals(expected, Average.quadraticMean(numbers), DELTA);
  82.     }
  83.  
  84.     @Test
  85.     public void testGeneralizedMean() throws Exception {
  86.         // y-root((x1)^y+(x2)^y+(x3)^y+...+(xn)^y / n)
  87.         double[] numbers = new double[] { 1, 2, 3, 4, 5 };
  88.         double power = 4;
  89.         double expected = Math.pow((DoubleStream.of(numbers).map(x -> Math.pow(x, 4)).sum() / numbers.length), 1.0 / power);
  90.         assertEquals(expected, Average.generalizedMean(numbers, power), DELTA);
  91.     }
  92.  
  93.     @Test
  94.     public void testMidrange() throws Exception {
  95.         double[] numbers = new double[] { 1, 2, 3, 4, 5 };
  96.         double expected = (numbers[0] + numbers[numbers.length - 1]) / 2.0;
  97.         assertEquals(expected, Average.midrange(numbers), DELTA);
  98.     }
  99.  
  100.     @Test
  101.     public void testMedianEven() throws Exception {
  102.         double[] numbers = new double[] { 1, 2, 3, 4, 5, 6};
  103.         double expected = (numbers[(numbers.length / 2) - 1] + numbers[numbers.length / 2]) / 2.0;
  104.         assertEquals(expected, Average.median(numbers), DELTA);
  105.     }
  106.  
  107.     @Test
  108.     public void testMedianOdd() throws Exception {
  109.         double[] numbers = new double[] { 1, 2, 3, 4, 5 };
  110.         double expected = numbers[((int) Math.floor(numbers.length / 2))];
  111.         assertEquals(expected, Average.median(numbers), DELTA);
  112.     }
  113.  
  114.     @Test
  115.     public void testMode() throws Exception {
  116.         // frequency based average
  117.         double[] numbers = new double[] { 1, 2, 3, 3, 3, 4, 5 };
  118.         double expected = 3;
  119.         assertEquals(expected, Average.mode(numbers), DELTA);
  120.     }
  121. }
downloadAverage_test.java Source code - Download Algorithm-Implementations Source code
Related Source Codes/Software:
raty - 2017-04-22
RDVTabBarController - Highly customizable tabBar and tabBarController fo... 2017-04-22
material-icon-lib - Library containing over 1500 material vector icons... 2017-04-21
httpdiff - Perform the same request against two HTTP servers ... 2017-04-21
jquerytools - The missing UI library for the Web ... 2017-04-21
mcrouter - Mcrouter is a memcached protocol router for scalin... 2017-04-22
dynomite - A generic dynamo implementation for different k-v ... 2017-04-22
kityminder - Baidu brain figure 2017-04-22
llvm - Mirror of official llvm git repository located at ... 2017-04-22
RBBAnimation - Block-based animations made easy, comes with easin... 2017-04-22
ied - 2017-04-29
Nimble - A Matcher Framework for Swift and Objective-C 2017-04-29
MHVideoPhotoGallery - A Photo and Video Gallery 2017-04-29
shoulda-matchers - Collection of testing matchers extracted from Shou... 2017-04-29
Android-SlideExpandableListView - A better ExpandableListView, with animated expanda... 2017-04-29
AppSales-Mobile - App Sales allows iPhone and Mac App Store develope... 2017-04-29
react-templates - Light weight templates for react ... 2017-04-28
afterglow-theme - A minimal dark Theme for Sublime Text 2 and 3 2017-04-28
jwt-go - Golang implementation of JSON Web Tokens (JWT) 2017-04-28
DeerResume - Tool MarkDown online resume, online preview, edit,... 2017-04-28

 Back to top