-
Notifications
You must be signed in to change notification settings - Fork 2
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
losses: add likelihood loss and test, improve base loss #38
Conversation
|
||
|
||
class LikelihoodLoss(BaseLoss): | ||
"""Class for a likelihood loss computation using a Gaussian kernel function.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK for the first version using a Gaussian kernel function; but one might want to use a different density estimation approach?
(I would convert this as an issue, after merging the PR, if considered relevant)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, of course this can be useful, we can expand the class as necessary in the future
|
||
|
||
class LikelihoodLoss(BaseLoss): | ||
"""Class for a likelihood loss computation using a Gaussian kernel function.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we might want to add some references here if considered relevant (perhaps Grazzini et al 2017? Although we might not be interested in the posterior distribution)
Codecov Report
📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more Additional details and impacted files@@ Coverage Diff @@
## main #38 +/- ##
==========================================
- Coverage 97.17% 97.08% -0.10%
==========================================
Files 29 30 +1
Lines 1452 1508 +56
==========================================
+ Hits 1411 1464 +53
- Misses 41 44 +3
|
d9cb33d
to
759041f
Compare
return h | ||
|
||
@staticmethod | ||
def _get_bandwidth_scott(N: int, D: int) -> float: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not used; maybe we can have a configuration in the loss (either scott or silverman)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Will do
tests/test_losses/test_likelihood.py
Outdated
|
||
from black_it.loss_functions.likelihood import LikelihoodLoss | ||
|
||
np.random.seed(3) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a bit dangerous as the line is executed at module level, but it is not guaranteed the seeds, across tests, are set in the same order because of changes in the order in how the test modules are imported. I suggest using a different approach; how is it done in other tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok I see each test calls np.random.seed
in the body of each test. I argue against this practice as np.random.seed
does a global side-effect and might cause dependency among tests. This should be refactored in using a local random number generator (np.default_rng(seed)
) whenever possible.
For this PR, I suggest moving this call within the body of each test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I set the seed within each test. In any case, the tests should work across different seeds
black_it/loss_functions/base.py
Outdated
for i in range(num_coords): | ||
filter_ = filters[i] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for i in range(num_coords): | |
filter_ = filters[i] | |
for i, filter_ in enumerate(filters): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, much better!
black_it/loss_functions/base.py
Outdated
|
||
@staticmethod | ||
def _filter_data( | ||
num_coords: int, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If suggestion below is accepted, num_coords
is not needed here and above at line 68:
num_coords: int, |
black_it/loss_functions/base.py
Outdated
weights = self._check_coordinate_weights(num_coords) | ||
filters = self._check_coordinate_filters(num_coords) | ||
|
||
filtered_data = self._filter_data(num_coords, filters, sim_data_ensemble) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See below
filtered_data = self._filter_data(num_coords, filters, sim_data_ensemble) | |
filtered_data = self._filter_data(filters, sim_data_ensemble) |
@@ -0,0 +1,74 @@ | |||
# Black-box ABM Calibration Kit (Black-it) | |||
# Copyright (C) 2021-2022 Banca d'Italia |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we are in 2023, so we should change the year range of the licenses accordingly in all the copyright headers; but that's a task for another day...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I will change it for this module only in the meanwhile
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the copyright check will fail :( , however, you could add the year 2023
among the options in the regex of the script scripts/check_copyright.py
.
In particular, at line 36, from:
# Copyright \(C\) 2021-2022 Banca d'Italia
To:
# Copyright \(C\) 2021-(2022|2023) Banca d'Italia
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks 🙏🏼
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work 🚀
Thank you @marcofavoritobi 🙏🏼 |
Proposed changes
I add a loss based on the computation of the likelihood function, I improve the modularity of the base loss class and I add a Jupyter notebook where the new loss is used to compute likelihood and posterior for the BH4 model.
Types of changes
What types of changes does your code introduce?
Put an
x
in the boxes that applyChecklist
Put an
x
in the boxes that apply.develop
branch (left side). Also you should start your branch off ourdevelop
.