Skip to content

Add IRR to cashloan and migrate functions to lib_financial.h#1223

Merged
cpaulgilman merged 15 commits into
developfrom
sam-638-irr-cashloan
Oct 22, 2024
Merged

Add IRR to cashloan and migrate functions to lib_financial.h#1223
cpaulgilman merged 15 commits into
developfrom
sam-638-irr-cashloan

Conversation

@cpaulgilman

@cpaulgilman cpaulgilman commented Oct 18, 2024

Copy link
Copy Markdown
Collaborator

Fix NatLabRockies/SAM#638

1. Add IRR output to cashloan

  • IRR output available for Residential and Commercial financial models.
  • Add nominal discount rate output to help with identifying nominal discount rate that results in NPV = 0 (useful for parametrics on real discount rate)
  • Note that default Residential and Commercial cases use 100% debt, so IRR is NaN for default configurations because initial investment is zero
  • Not modifying SAM UI to add IRR to Metrics table on Summary tab, so it is only available in Data Tables because IRR metric is a bit ambiguous for projects that do not earn revenue

2. Move financial model functions to lib_financial for all financial models

Task SO CS TPO HD MP EPF LPF SLB TPO H CashLoan
Compare functions to SO before moving NA NA NA
Check depreciation_sched_custom() NA NA
Delete using namespace libfin from cmod
Delete libfin functions from cmod
Convert npv()to libfin::
Convert irr()to libfin::
Convert min()to libfin:: NA
Convert max()to libfin:: NA
Convert ppmt()to libfin:: NA NA
Convert payback() to libfin:: NA NA NA NA NA NA NA *

* Note that Third Party Ownership - Host model has payback cashflows but does not use the payback() function to calculate payback period.

Question

That there are special min() and max() functions defined in lib_financial. I changed all calls to min() and max() in the financial cmods to libfin::min() and libfin::max() to avoid conflict, but should any of those be std::min() or std::max() instead?

@sjanzou sjanzou left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

For Mac and Linux GitHub Actions to pass, please add the following to the lib_financial.cpp file for DBL_MAX to be defined:

DBL_MAX is defined in float.h and was previously included at the top of each financial compute module as
#ifndef WIN32
#include <float.h>
#endif

Comment thread shared/lib_financial.cpp
}

double scale_factor = irr_scale_factor(cf_vector, count);
double residual = DBL_MAX;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

DBL_MAX was not defined and is causing the Linux and MacOS tests to fail.

DBL_MAX is defined in float.h and was previously included at the top of each financial compute module as
#ifndef WIN32
#include <float.h>
#endif

@cpaulgilman

Copy link
Copy Markdown
Collaborator Author

For Mac and Linux GitHub Actions to pass, please add the following to the lib_financial.cpp file for DBL_MAX to be defined:

DBL_MAX is defined in float.h and was previously included at the top of each financial compute module as #ifndef WIN32 #include <float.h> #endif

@sjanzou Thanks for finding the cause of those tests failing.

@cpaulgilman cpaulgilman requested a review from sjanzou October 21, 2024 17:56

@brtietz brtietz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Refactoring looks good! One request for a clarifying comment. Thanks!

Comment thread shared/lib_financial.cpp Outdated
#endif

double libfin::min(double a, double b)
{ // handle NaN

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you or @sjanzou expand upon this comment to include why 0 is the correct answer when one of the inputs is NaN? My hunch based on reading the code is to make the cashflow readable, but it would be good to document that if true so future developers know whether to use this or std::min.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@brtietz , yes - to make the cashflow readable and that std::min and max have screwy results based on if a NaN is the first element in the comparison - see https://stackoverflow.com/questions/72296440/how-to-prevent-stdmin-and-max-to-return-nan-if-the-first-element-of-the-array

@sjanzou sjanzou left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Working for me and all platforms and GitHub Actions...

Thanks for taking the plunge into modularization!

@cpaulgilman cpaulgilman merged commit ab890dd into develop Oct 22, 2024
@cpaulgilman cpaulgilman deleted the sam-638-irr-cashloan branch October 22, 2024 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Requests to calculate IRR for residential/commercial financial model

3 participants