Skip to content

Commit

Permalink
Implemented sample_variance to aggregate rules (#53)
Browse files Browse the repository at this point in the history
Added the "Sample Variance" rule to the set of aggregate rules. Changes
reflect in the schema files (JSON, PHP, YAML), with a new PHP class for
the rule and updates to existing tests. The README's rules count has
been updated as well.
  • Loading branch information
SmetDenis committed Mar 17, 2024
1 parent 6886ba2 commit 7715277
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 12 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Stable Version](https://poser.pugx.org/jbzoo/csv-blueprint/version)](https://packagist.org/packages/jbzoo/csv-blueprint/) [![Total Downloads](https://poser.pugx.org/jbzoo/csv-blueprint/downloads)](https://packagist.org/packages/jbzoo/csv-blueprint/stats) [![Docker Pulls](https://img.shields.io/docker/pulls/jbzoo/csv-blueprint.svg)](https://hub.docker.com/r/jbzoo/csv-blueprint) [![Dependents](https://poser.pugx.org/jbzoo/csv-blueprint/dependents)](https://packagist.org/packages/jbzoo/csv-blueprint/dependents?order_by=downloads) [![GitHub License](https://img.shields.io/github/license/jbzoo/csv-blueprint)](https://github.com/JBZoo/Csv-Blueprint/blob/master/LICENSE)

<!-- rules-counter -->
![Static Badge](https://img.shields.io/badge/Rules-84-green?label=Total%20Number%20of%20Rules&labelColor=blue&color=gray) ![Static Badge](https://img.shields.io/badge/Rules-55-green?label=Cell%20Rules&labelColor=blue&color=gray) ![Static Badge](https://img.shields.io/badge/Rules-29-green?label=Aggregate%20Rules&labelColor=blue&color=gray)
![Static Badge](https://img.shields.io/badge/Rules-88-green?label=Total%20Number%20of%20Rules&labelColor=blue&color=gray) ![Static Badge](https://img.shields.io/badge/Rules-55-green?label=Cell%20Rules&labelColor=blue&color=gray) ![Static Badge](https://img.shields.io/badge/Rules-33-green?label=Aggregate%20Rules&labelColor=blue&color=gray)
<!-- /rules-counter -->

## Introduction
Expand Down Expand Up @@ -277,12 +277,19 @@ columns:

# Population variance - Use when all possible observations of the system are present.
# If used with a subset of data (sample variance), it will be a biased variance.
# It's n degrees of freedom.
# n degrees of freedom, where n is the number of observations.
population_variance: 5.123
population_variance_not: 4.123
population_variance_min: 1.123
population_variance_max: 10.123

# Unbiased sample variance Use when only a subset of all possible observations of the system are present.
# n - 1 degrees of freedom, where n is the number of observations.
sample_variance: 5.123
sample_variance_not: 4.123
sample_variance_min: 1.123
sample_variance_max: 10.123

- name: "another_column"

- name: "third_column"
Expand Down
7 changes: 6 additions & 1 deletion schema-examples/full.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@
"population_variance" : 5.123,
"population_variance_not" : 4.123,
"population_variance_min" : 1.123,
"population_variance_max" : 10.123
"population_variance_max" : 10.123,

"sample_variance" : 5.123,
"sample_variance_not" : 4.123,
"sample_variance_min" : 1.123,
"sample_variance_max" : 10.123
}
},

Expand Down
5 changes: 5 additions & 0 deletions schema-examples/full.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@
'population_variance_not' => 4.123,
'population_variance_min' => 1.123,
'population_variance_max' => 10.123,

'sample_variance' => 5.123,
'sample_variance_not' => 4.123,
'sample_variance_min' => 1.123,
'sample_variance_max' => 10.123,
],
],

Expand Down
9 changes: 8 additions & 1 deletion schema-examples/full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,19 @@ columns:

# Population variance - Use when all possible observations of the system are present.
# If used with a subset of data (sample variance), it will be a biased variance.
# It's n degrees of freedom.
# n degrees of freedom, where n is the number of observations.
population_variance: 5.123
population_variance_not: 4.123
population_variance_min: 1.123
population_variance_max: 10.123

# Unbiased sample variance Use when only a subset of all possible observations of the system are present.
# n - 1 degrees of freedom, where n is the number of observations.
sample_variance: 5.123
sample_variance_not: 4.123
sample_variance_min: 1.123
sample_variance_max: 10.123

- name: "another_column"

- name: "third_column"
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Aggregate/ComboPopulationVariance.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class ComboPopulationVariance extends AbstarctAggregateRuleCombo
protected const HELP_TOP = [
'Population variance - Use when all possible observations of the system are present.',
'If used with a subset of data (sample variance), it will be a biased variance.',
'It\'s n degrees of freedom',
'n degrees of freedom, where n is the number of observations.',
];

protected function getActualAggregate(array $colValues): ?float
Expand Down
34 changes: 34 additions & 0 deletions src/Rules/Aggregate/ComboSampleVariance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* JBZoo Toolbox - Csv-Blueprint.
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @see https://github.com/JBZoo/Csv-Blueprint
*/

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Rules\Aggregate;

use MathPHP\Statistics\Descriptive;

final class ComboSampleVariance extends AbstarctAggregateRuleCombo
{
protected const NAME = 'population variance';

protected const HELP_TOP = [
'Unbiased sample variance Use when only a subset of all possible observations of the system are present.',
'n - 1 degrees of freedom, where n is the number of observations.',
];

protected function getActualAggregate(array $colValues): ?float
{
return Descriptive::sampleVariance(self::stringsToFloat($colValues));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,16 @@

use function JBZoo\PHPUnit\isSame;

class ComboVarianceTest extends AbstractAggregateRuleCombo
class ComboPopulationVarianceTest extends AbstractAggregateRuleCombo
{
protected string $ruleClass = ComboPopulationVariance::class;

public function testEqual(): void
{
$rule = $this->create(10, Combo::EQ);

isSame(
'The population variance in the column is "9.3333333333333", which is not equal than the expected "10"',
$rule->test([13, 18, 13, 14, 13, 16, 14, 21, 10]),
);
$rule = $this->create(0.1875, Combo::EQ);
isSame('', $rule->test(['1', '2', '1', '1']));

$rule = $this->create(10, Combo::EQ);
isSame(
'The population variance in the column is "0.1875", which is not equal than the expected "10"',
$rule->test(['1', '2', '1', '1']),
Expand Down
40 changes: 40 additions & 0 deletions tests/Rules/Aggregate/ComboSampleVarianceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* JBZoo Toolbox - Csv-Blueprint.
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @see https://github.com/JBZoo/Csv-Blueprint
*/

declare(strict_types=1);

namespace JBZoo\PHPUnit\Rules\Aggregate;

use JBZoo\CsvBlueprint\Rules\AbstarctRule as Combo;
use JBZoo\CsvBlueprint\Rules\Aggregate\ComboSampleVariance;
use JBZoo\PHPUnit\Rules\AbstractAggregateRuleCombo;

use function JBZoo\PHPUnit\isSame;

class ComboSampleVarianceTest extends AbstractAggregateRuleCombo
{
protected string $ruleClass = ComboSampleVariance::class;

public function testEqual(): void
{
$rule = $this->create(10.5, Combo::EQ);
isSame('', $rule->test([13, 18, 13, 14, 13, 16, 14, 21, 10]));

$rule = $this->create(10, Combo::EQ);
isSame(
'The population variance in the column is "10.5", which is not equal than the expected "10"',
$rule->test([13, 18, 13, 14, 13, 16, 14, 21, 10]),
);
}
}

0 comments on commit 7715277

Please sign in to comment.