Skip to content

Commit

Permalink
Merge 20fff5c into 842b5c0
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestoarbitrio committed Jun 19, 2020
2 parents 842b5c0 + 20fff5c commit 2f69211
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/cr/cube/cubepart.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,16 @@ def shape(self):
"""
return (self.row_count,)

@lazyproperty
def standard_deviation(self):
""" -> np.ndarray, percentages standard deviation"""
return np.sqrt(self._variance)

@lazyproperty
def standard_error(self):
""" -> np.ndarray, percentages standard error"""
return np.sqrt(self._variance / self.rows_margin)

@lazyproperty
def table_base(self):
"""1D, single-element ndarray (like [3770])."""
Expand Down Expand Up @@ -1060,7 +1070,6 @@ def table_margin(self):
return np.array([row.table_margin for row in self._stripe.rows])

return self._stripe.table_margin_unpruned
# return self._stripe.table_margin

@lazyproperty
def table_margin_unpruned(self):
Expand Down Expand Up @@ -1167,6 +1176,14 @@ def _stripe(self):
def _table_proportions_as_array(self):
return np.array([row.table_proportions for row in self._stripe.rows])

@lazyproperty
def _variance(self):
"""Returns the variance for cell percentages
`variance = p * (1-p)`
"""
p = self._table_proportions_as_array
return p * (1 - p)


class _Nub(CubePartition):
"""0D slice."""
Expand Down
36 changes: 36 additions & 0 deletions tests/integration/test_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,42 @@ def test_proportions_text(self):
(0.1666667, 0.1666667, 0.1666667, 0.1666667, 0.1666667, 0.1666667),
)

def test_std_dev_err_univariate_cat_axis_none(self):
strand = Cube(CR.UNIVARIATE_CATEGORICAL).partitions[0]
np.testing.assert_almost_equal(
strand.standard_deviation, [0.47140452, 0.47140452]
)
np.testing.assert_almost_equal(strand.standard_error, [0.1490712, 0.21081851])

def test_std_dev_err_numeric(self):
strand = Cube(CR.VOTER_REGISTRATION).partitions[0]
np.testing.assert_almost_equal(
strand.standard_deviation, [0.31902194, 0.30655342, 0.09949874]
)
np.testing.assert_almost_equal(
strand.standard_error, [0.01072381, 0.02991655, 0.03146427]
)

def test_std_dev_err_datetime(self):
strand = Cube(CR.SIMPLE_DATETIME).partitions[0]
np.testing.assert_almost_equal(
strand.standard_deviation, [0.4330127, 0.4330127, 0.4330127, 0.4330127]
)
np.testing.assert_almost_equal(
strand.standard_error, [0.4330127, 0.4330127, 0.4330127, 0.4330127]
)

def test_std_dev_err_text(self):
strand = Cube(CR.SIMPLE_TEXT).partitions[0]
np.testing.assert_almost_equal(
strand.standard_deviation,
[0.372678, 0.372678, 0.372678, 0.372678, 0.372678, 0.372678],
)
np.testing.assert_almost_equal(
strand.standard_error,
[0.372678, 0.372678, 0.372678, 0.372678, 0.372678, 0.372678],
)

def test_proportions_cat_x_cat_axis_none(self):
slice_ = Cube(CR.CAT_X_CAT).partitions[0]
expected = np.array([[0.3333333, 0.1333333], [0.3333333, 0.2000000]])
Expand Down
32 changes: 32 additions & 0 deletions tests/integration/test_cubepart.py
Original file line number Diff line number Diff line change
Expand Up @@ -1451,6 +1451,38 @@ def it_provides_values_for_cat_with_means_and_insertions(self):
)
assert strand.title == "Untitled"

def it_provides_std_dev_err_univ_mr_with_hs(self):
strand = Cube(CR.UNIV_MR_WITH_HS["slides"][0]["cube"]).partitions[0]

np.testing.assert_almost_equal(
strand.standard_deviation,
[
0.46426724,
0.3584419,
0.2351762,
0.32431855,
0.2891897,
0.24800318,
0.15104855,
0.49700725,
0.14466968,
],
)
np.testing.assert_almost_equal(
strand.standard_error,
[
0.00453318,
0.00504326,
0.00531142,
0.00513733,
0.00521646,
0.0052914,
0.00541038,
0.00407718,
0.00541584,
],
)

def it_places_insertions_on_a_reordered_dimension_in_the_right_position(self):
"""Subtotal anchors follow re-ordered rows.
Expand Down
11 changes: 11 additions & 0 deletions tests/integration/test_multiple_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ def test_proportions_simple_mr():
np.testing.assert_almost_equal(table_proportions, (0.6, 0.6666667, 0.0))


def test_std_dev_err_simple_mr():
strand = Cube(CR.SIMPLE_MR).partitions[0]

np.testing.assert_almost_equal(
strand.standard_deviation, [0.4898979, 0.4714045, 0.0]
)
np.testing.assert_almost_equal(
strand.standard_error, [0.2828427, 0.2357023, np.nan]
)


def test_1D_mr_with_means():
strand = Cube(CR.MR_MEAN_FILT_WGTD).partitions[0]
np.testing.assert_almost_equal(
Expand Down

0 comments on commit 2f69211

Please sign in to comment.