# How to use calculateVariance method of io.kotest.matchers.stats.matchers class

Best Kotest code snippet using io.kotest.matchers.stats.matchers.calculateVariance

matchers.kt

Source:matchers.kt

`...299 sum += BigDecimal(elem.toString())300 }301 return sum.divide(BigDecimal(collection.size), defaultMathContext)302}303private fun <T : Number> calculateVariance(collection: Collection<T>): BigDecimal {304 val mean: BigDecimal = calculateMean(collection)305 var sumOfSquaredDifferences: BigDecimal = BigDecimal.ZERO306 for (elem in collection) {307 sumOfSquaredDifferences += (BigDecimal(elem.toString()) - mean).pow(2)308 }309 return sumOfSquaredDifferences.divide(BigDecimal(collection.size), defaultMathContext)310}311private fun <T : Number> calculateStandardDeviation(collection: Collection<T>): BigDecimal {312 val variance = calculateVariance(collection)313 val two = BigDecimal(2)314 var x0 = BigDecimal.ZERO315 var x1 = BigDecimal(sqrt(variance.toDouble()))316 while (x0 != x1) {317 x0 = x1318 x1 = variance.divide(x0, defaultMathContext)319 x1 = x1.add(x0)320 x1 = x1.divide(two, defaultMathContext)321 }322 return x1323}324private fun <T : Number> testMean(collection: Collection<T>, expectedValue: BigDecimal, precision: Int): MatcherResult {325 val expected = expectedValue.stripTrailingZeros()326 val actual = if (collection.isEmpty()) BigDecimal.ZERO else calculateMean(collection).round(precision)327 return MatcherResult(328 expected.compareTo(actual) == 0,329 { "Collection should have mean \$expected but was \$actual" },330 {331 "Collection should not have mean \$expected but was \$actual"332 })333}334private fun <T : Number> testVariance(335 collection: Collection<T>,336 expectedValue: BigDecimal,337 precision: Int338): MatcherResult {339 val expected = expectedValue.stripTrailingZeros()340 val actual = if (collection.isEmpty()) BigDecimal.ZERO else calculateVariance(collection).round(precision)341 return MatcherResult(342 expected.compareTo(actual) == 0,343 { "Collection should have variance \$expected but was \$actual" },344 {345 "Collection should not have variance \$expected but was \$actual"346 })347}348private fun <T : Number> testStandardDeviation(349 collection: Collection<T>,350 expectedValue: BigDecimal,351 precision: Int352): MatcherResult {353 val expected = expectedValue.stripTrailingZeros()354 val actual = if (collection.isEmpty()) BigDecimal.ZERO else calculateStandardDeviation(collection).round(precision)...`

calculateVariance

Using AI Code Generation

`1val list = listOf(1, 2, 3, 4, 5)2list.calculateVariance() shouldBe 2.03val list = listOf(1, 2, 3, 4, 5)4list.calculateStandardDeviation() shouldBe 1.41421356237309515val list = listOf(1, 2, 3, 4, 5)6list.calculateConfidenceInterval(0.95) shouldBe Pair(1.0, 5.0)7val list1 = listOf(1, 2, 3, 4, 5)8val list2 = listOf(1, 2, 3, 4, 5)9list1.calculateCorrelation(list2) shouldBe 1.010val list1 = listOf(1, 2, 3, 4, 5)11val list2 = listOf(1, 2, 3, 4, 5)12list1.calculateCovariance(list2) shouldBe 2.013val list = listOf(1, 2, 3, 4, 5)14list.calculateZScore(3) shouldBe 0.015val list = listOf(1, 2, 3, 4, 5)16list.calculateZScore(3) shouldBe 0.017val list = listOf(1, 2, 3, 4, 5)18list.calculatePValue(3) shouldBe 0.519val list = listOf(1, 2, 3, 4, 5)`

calculateVariance

Using AI Code Generation

`1 calculateVariance(listOf(1, 2, 3, 4, 5)) shouldBe 2.02 calculateStandardDeviation(listOf(1, 2, 3, 4, 5)) shouldBe 1.41423}4calculateMean(listOf(1, 2, 3, 4, 5)) shouldBe 3.05calculateMedian(listOf(1, 2, 3, 4, 5)) shouldBe 3.06calculateVariance(listOf(1, 2, 3, 4, 5)) shouldBe 2.07calculateStandardDeviation(listOf(1, 2, 3, 4, 5)) shouldBe 1.4142`

calculateVariance

Using AI Code Generation

`1val variance = calculateVariance(data)2val standardDeviation = calculateStandardDeviation(data)3val meanAbsoluteDeviation = calculateMeanAbsoluteDeviation(data)4val median = calculateMedian(data)5val mode = calculateMode(data)6val skewness = calculateSkewness(data)7val kurtosis = calculateKurtosis(data)8val range = calculateRange(data)9val coefficientOfVariation = calculateCoefficientOfVariation(data)10val interquartileRange = calculateInterquartileRange(data)11val quartiles = calculateQuartiles(data)12val quintiles = calculateQuintiles(data)13val deciles = calculateDeciles(data)14val percentiles = calculatePercentiles(data)15val percentile = calculatePercentile(data, 50.0)16val percentile = calculatePercentile(data, 50.0, 2)`

calculateVariance

Using AI Code Generation

`1val variance = calculateVariance ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 )2val standardDeviation = calculateStandardDeviation ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 )3val variancePopulation = calculateVariancePopulation ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 )4val standardDeviationPopulation = calculateStandardDeviationPopulation ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 )5val coefficientOfVariation = calculateCoefficientOfVariation ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 )6val confidenceInterval = calculateConfidenceInterval ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 )7val confidenceIntervalPopulation = calculateConfidenceIntervalPopulation ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 )8val sampleSize = calculateSampleSize ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 )9val sampleSizePopulation = calculateSampleSizePopulation (`

calculateVariance

Using AI Code Generation

`1val variance = calculateVariance ( listOf ( 1 , 2 , 3 , 4 , 5 ) ) 2 variance should be ( 2.5 )3val standardDeviation = calculateStandardDeviation ( listOf ( 1 , 2 , 3 , 4 , 5 ) ) 4 standardDeviation should be ( 1.5811388300841898 )5val correlation = calculateCorrelation ( listOf ( 1 , 2 , 3 , 4 , 5 ) , listOf ( 1 , 2 , 3 , 4 , 5 ) ) 6 correlation should be ( 1.0 )7val covariance = calculateCovariance ( listOf ( 1 , 2 , 3 , 4 , 5 ) , listOf ( 1 , 2 , 3 , 4 , 5 ) ) 8 covariance should be ( 2.5 )9val mean = calculateMean ( listOf ( 1 , 2 , 3 , 4 , 5 ) ) 10 mean should be ( 3.0 )11val mode = calculateMode ( listOf ( 1 , 2 , 3 , 4 , 5 ) ) 12 mode should be ( 1 )13val median = calculateMedian ( listOf ( 1 , 2 , 3 , 4 , 5 ) ) 14 median should be ( 3 )`

## Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.