Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add update_predictor_exp function and its pytest function #35

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/analyticsdf/analyticsdataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,28 @@ def update_response_poly_categorical(self, predictor_name: str = None, betas: di
self.response_vector = numeric_vals[predictor_name]
else:
self.response_vector += numeric_vals[predictor_name]

def update_predictor_exp(self, rate: float = None,
predictor_name: list = None):
"""Update a predictor with samples from an exponential distribution.

Args:
rate:
float, must be greater than 0.
Lambda(λ). The rate parameter of an exponential distribution.
predictor_name:
String, a predictor name in AnalyticsDataframe object.

Raises:
KeyError: If the column does not exists.

"""
## Check if all target predictors exist
_check_columns_exist(self.predictor_matrix, predictor_name)

with set_random_state(validate_random_state(self.seed)):
scale = 1/rate # scale(β) = 1/lambda(λ)
size = self.n
self.predictor_matrix[predictor_name] = np.random.exponential(scale, size)


25 changes: 25 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,28 @@ def test_update_response_poly_categorical():
ad.update_response_poly_categorical(predictor_name='X6', betas={'Red': -2000, 'Blue': -1700})
assert ad.predictor_matrix.loc[1, 'X6'] == 'Red'
assert ad.response_vector[1] < -1900

def test_predictor_exp():
"""Test function 'update_predictor_exp'
Test logic:
The mean of an exponential distribution is also the scale parameter
β = 1/λ
"""
## Initialize and use the function to update
ad = AnalyticsDataframe(10000, 3, ["xx1", "xx2", "xx3"], "yy", 666)
ad.update_predictor_exp(rate = 1.5,
predictor_name = "xx1")
pred_matrix = ad.predictor_matrix

## Set up groud truth for testing
round_decimals = 2
exp_mean = round(1/1.5, round_decimals)

## Test if the mean of the distribution is 1/λ
assert round(np.mean(pred_matrix["xx1"]), round_decimals) == exp_mean

## Test its error cases
# pred_exists error: predictor name doesn't exist
with pytest.raises(KeyError):
ad.update_predictor_exp(rate = 1.5,
predictor_name = "nonexistent")
Loading