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 2 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
29 changes: 29 additions & 0 deletions src/analyticsdf/analyticsdataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,32 @@ 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,
size: tuple = None,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is tuple the ideal choice?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the rest of the functions all use list or np.array as the input.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback!
I was thinking about giving the option of defining output shape, e.g. multi-dimensional output. But I just realized that'd require a significant more complex function. We should indeed go with the simple version for now.
Also I think the size should be the number of observations by default. I will just get rid of this argument size for now.

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.
size:
int or tuple of ints, optional
Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn.
If size is None (default), a single value is returned if scale is a scalar.
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(λ)
self.predictor_matrix[predictor_name] = np.random.exponential(scale, size)


27 changes: 27 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,30 @@ 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,
size = ad.n,
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,
size = ad.n,
predictor_name = "nonexistent")
Loading