-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Proposed Numerical Integration Atom #2735
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
base: master
Are you sure you want to change the base?
Conversation
integration atom that can handle both 1D and ND cases
|
|
cvxpy/atoms/numerical_integration.py
Outdated
| from cvxpy.atoms.affine.sum import sum as cvx_sum | ||
|
|
||
|
|
||
| def numerical_integration(f_callable, w, ranges, g_list=None, num_points=100, |
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.
all function arguments need types
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.
should be good now!
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.
sorry about that!
cvxpy/atoms/numerical_integration.py
Outdated
| from cvxpy.atoms.affine.sum import sum as cvx_sum | ||
|
|
||
|
|
||
| def numerical_integration(f_callable, w, ranges, g_list=None, num_points=100, |
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.
should be good now!
|
Hey @shokonaasti - did you see the comments on Discord? Copied below for visibility. My sense is maybe we should change it to cp.integrate to be consistent with scipy numerical integration. Any thoughts on the other issues raised? Hey thanks a lot for your PR.. I had a few comments open for discussion (hence why I didn't give a code review)
|
|
I made the changes to to adhere to scientific notation as well as passing the cvxpy expression directly. Sorry for the delay and let me know if there are any questions. |
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.
I think I'd prefer to have one file intergrate.py that contains both atoms than the subfolder. Any reason to not to do it this way? Are you planning to add a lot more integration methods in future PRs?
Also, just out of lay-curiosity can Forward Euler or Backward Euler be simulated with these atoms?
| from cvxpy.atoms.stats import mean, std, var | ||
| from cvxpy.atoms.ptp import ptp | ||
|
|
||
| from . import integrate |
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.
| from . import integrate | |
| from cvxpy.atoms.integrate import trapz, simpson |
| from cvxpy.atoms.ptp import ptp | ||
|
|
||
| from . import integrate | ||
| from cvxpy.atoms import integrate |
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.
| from cvxpy.atoms import integrate |
| @@ -0,0 +1,19 @@ | |||
| """ | |||
| Copyright, the CVXPY Ashok Viswanathan | |||
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.
| Copyright, the CVXPY Ashok Viswanathan | |
| Copyright, the CVXPY Project |
Please sign the CLA.
| @@ -0,0 +1,97 @@ | |||
| """ | |||
| Copyright, the CVXPY Ashok Viswanathan | |||
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.
| Copyright, the CVXPY Ashok Viswanathan | |
| Copyright, the CVXPY Project |
| from .trapz import trapz | ||
| from .simpson import simpson |
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.
| from .trapz import trapz | |
| from .simpson import simpson | |
| from cvxpy.atoms.integrate.trapz import trapz | |
| from cvxpy.atoms.integrate.simpson import simpson |
| x: Optional[np.ndarray] = None, | ||
| dx: float = 1.0, | ||
| axis: int = -1, | ||
| even: str = "avg" |
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.
Which strategy does scipy implement? I notice that scipy.integrate.simpson doesn't have an even mode specification.
| axis: int = -1 | ||
| ) -> Expression: | ||
| y_ndim = len(y.shape) | ||
| axis = axis % y_ndim |
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.
Same question as with simpson.
| dxs = np.diff(x) | ||
| dxs_shape = [1] * y_ndim | ||
| dxs_shape[axis] = len(dxs) | ||
| dxs_broadcast = dxs.reshape(dxs_shape) |
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.
Should we use np.broadcast_to instead of .reshape
| center = cvx_sum(y[slicer(1, n - 1)]) | ||
| return dx * (edge + center) | ||
|
|
||
| # def trapz( |
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 generally don't allow commented out code to be merged.
| # The optimized result should be smaller than the naive result, | ||
| # where X of the naive result is I. | ||
| self.assertTrue(prob.value < naiveRes) | ||
| def test_trapz_atom(self) -> None: |
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.
| def test_trapz_atom(self) -> None: | |
| def test_trapz_atom(self) -> None: |
|
@shokonaasti are you able to sign the CLA, so someone else can finish this PR? Will you be able to finish the PR soon? |
Description
Please include a short summary of the change.
Issue link (if applicable):
This PR creates the numerical_integration atom using discretization methods (trapezoidal, Simpson’s rule, and Monte Carlo) that numerically integrates a convex function of some parameters over a given domain. And is implemented as a wrapper around the existing sum atom. Unit test is included in the test_atoms.py file.
Type of change
Contribution checklist