-
Notifications
You must be signed in to change notification settings - Fork 77
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 a construct to distribute a total over of collection of coefficients #529
Comments
Here are some possible options in catala syntax: Distribution of a total over a collection with prorata.Pure prorata (with rounding errors) over a collection of money values
Pure prorata (with rounding errors) over a collection of structured values
Implementation over a collection of money valuesWe suppose the existence of
Implementation over a collection of structured values (with map2)We suppose the existence of a
Or with specific syntax:
Implementation over a collection of structured values (with specific construct)
Ok this is very concise but also both very specific (all the computed sums and (also this is assuming it's doable to integrate something like this in the |
Assuming we add tuples to the languageThis is similar to the
(note: adding this to the language will represent some work) In addition, we suppose a
|
Note that I used
|
Another completely random idea, which at first seems more convoluted but might in the end prove easier to grasp: lenses Assuming we already have the Lenses are just about combining these constructor/destructor functions for focusing inside a structure. Not entering into details here, the implementation could be complex, but it could look something like:
|
One more idea 😅 this could be made a tad lighter with a construction
(note that we could achieve a similar result by defining a |
Yet another not so unreasonable possibility: if we add maps (a.k.a. dictionaries) to the language, maybe it gives an angle for understanding the above that is easier to grasp ? The downside would be that we need ids under which the different members are registered, but the upside is that we still don't need to specify an ordering, and it becomes transparent that the correct scaled value is applied to the correct member. We could use simple integers denoting the indexes of the entries in the original collection as keys, to begin with. We now assume that we have
|
So we had a meeting today with the syntax committee (Sarah and Liane) to refine our decision among the last two competing proposals : Proposition 1 : integrated syntax
Proposition 2 : a pro-rata function returning a list of pairs
Decision !We prefer option 2. That involves having pairs in the language and a pattern destructuring available at least the map operation. Careful : the |
Update: there were some issues (implementation-wise) with the selected proposal, but I could come up with something reasonably close, for which I now have a working prototype. Hopefully that remains readable (the type definitions are the same as above):
There are two important elements here:
This remains to be evaluated by our Syntax Committee :) |
Louis' new version is validated by the syntax committee (meeting of Jan 30th, 2024). |
@AltGr I confirm the usefulness of your zip mapping syntax, I'm already using it in another place : https://gitlab.adullact.net/dgfip/ir-catala/-/blob/deficit_revenus_quotient/sources/cgi_revenus.catala_fr#L2070-2073 |
Now working OK through externals, see the example in the tests:
The part relevant to the syntax discussion above is in the 3rd file, where the piece missing is handled by the following syntax (same as proposed 3 comments up):
which is in fact a map2 allowing to construct the I think this issue can be closed (in favor of more general discussion on externals) ? |
Thanks @AltGr ! |
This is needed in the French IR computation when applying a prorata ; the difficulty is that there may be rounding errors, and we want to keep the sum of the resulting collection equal to the total: to achieve this, the usual approach is to adjust for the rounding errors in the last element of the list.
One way of handling this could be an operator of type
$$\text{total} \times \sum{\text{weights}} = \sum{\text{results}}$$
(weights: collection(decimal), total: money) -> collection(money)
, with a postcondition thatThe resulting collection would be computed as
$$\left[w_1 \times \text{total}; ...; w_{n-1} \times \text{total}; \text{total} \times \sum{\text{weights}} - \sum_{i =1}^{n-1}{w_i \times \text{total}}\right]$$
A typical application would first compute the weights as ratios of a given collection of quantities over their sum (so that$\sum{\text{weights}} = 1$ , and $\sum{\text{results}} = \text{total}$ )
This could be implemented as an external utility function, but if it's of general utility it could even be an overload for the operator
*
over typescollection(decimal)
andmoney
.Another completely different idea would be to add two much more general operations over collections, from which the above could be derived as pure Catala functions:
remove_last
: drops the last element of a collectionappend
: adds a given value at the end of a collectionTo compute the above, you would
The text was updated successfully, but these errors were encountered: