Skip to content

Enable single-precision LifeCycleModel1#103

Draft
MichaelTiemann wants to merge 13 commits into
vfitoolkit:masterfrom
MichaelTiemann:singlefp
Draft

Enable single-precision LifeCycleModel1#103
MichaelTiemann wants to merge 13 commits into
vfitoolkit:masterfrom
MichaelTiemann:singlefp

Conversation

@MichaelTiemann

Copy link
Copy Markdown
Contributor

These changes are enough to test single-precision functionality for LifeCycleModel1 (CPU and GPU).

Work is likely needed for Grid Interpolation (and many other things), but it shows how not-terribly-invasive the changes are.

These changes are enough to test single-precision functionality for LifeCycleModel1 (CPU and GPU).

Work is likely needed for Grid Interpolation (and many other things), but it shows how not-terribly-invasive the changes are.
In PolicyInd2Val_FHorz.m we simplify our construction by not explicitly passing `1` as the step argument.  This is the default, and saves us a cast operation.  All the other parameters not cast are magically already the proper floating point precision for our purposes.

Note that we needed to change the defaults for `tolerance` in the computation of LifeCycleProfiles, as the default value is smaller than can be represented in single-precision floating point.
At some point in the future it would be cool to make Policy an `int32`, but definitely don't want to make it a `single` (which loses precision after millions of states in the state space).

Also, let AggVars be single precision if we are doing that.
@MichaelTiemann MichaelTiemann marked this pull request as ready for review June 6, 2026 01:55
@MichaelTiemann

Copy link
Copy Markdown
Contributor Author

These changes assume that users cannot be bothered by typing their parameters as single or double fp, so we let them do what they want, and we cast the parameters into the correct precision when we need them. This leads to relatively little need to do other casting.

Based on the models tested (LifeCycleModel 1, 8, 18, 35, and 40), it is obvious that Values can, indeed be 32 bit floating point. Policy, however, must either be int32 (which won't blow up when we do maths that contemplate briefly the addition of symmetric arrays of indexes with negative and positive values during grid interpolation), or double. Single floating point does not do reliable integer maths when indexes need more than 7 digits of precision--well short of 100M or 1B states.

Using LifeCycleModel40 I did test out using int32 indexes for Policies, but I convert that at the end because later Grid Interpolation wants to do floating point maths with grid probabilities. This model is large enough to show a 50% speedup single fp vs double fp. (13 seconds vs 19 seconds on my GPU.)

There's a bit of a conflict between optional index parameters and optional precision parameters in the Create...FromParams functions, but if you don't hate the solution, it cleans up everything else.

There are places in the code where we do need to do floating point operations on policy values (see around line 408 of `StationaryDist_FHorz_Case1`, but this can be handled in the moment and does not foreclose on maintaining `int32` Policies generally.
Applies these learnings:

If all we do is store an index matrix from `max` into Policy, we don't need to cast it to the index type.  However, if we need to do any maths, we do.

If we do any maths with division within a rem/ceil stanza, we need to take care that we are promoting numerator and denominator to double.  This is because in the integer world ceil(1/2) is zero, whereas ceil(1.0/2.0) is 1.  You'll know you got it wrong when you find lots of zeros in the Policy matrix.

Expanded to cover the Huggett&Ventura 2000 replication.
Need to understand why double is better than single for LifeCycleModel35semi!
@MichaelTiemann MichaelTiemann marked this pull request as draft June 14, 2026 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant