/* Mon Feb 25 11:01:05 UTC 2008
 * Computes how financially unstable a routinely lending Government is
 * over time, and when it becomes bankrupt.
 *
 * (C)Jos Boersema This program is released under the GNU GPL License.
 *
 * See http://www.gnu.org You are free to use, copy & distribute, as
 * long as you don't deny others that right. There is no warranty
 * of any kind on this program. It is provided as is, in the hope it
 * will be useful.
 *
 * Compilation on GNU/Linux: gcc debt.c
 * Usage: see prog without args for help message.
 */

/*                     User options */
#define TERMINATE_AT_BANKRUPT /* comment out to stop after benkruptcy */
#define CURRENCY '$'          /* displayed currency symbol */
/*                     End eser options */

#include <stdio.h>
#include <stdlib.h>
#define YES 1
#define NO 0

int main ( int argc , char * * argv )
{
	double rent ; /* input interest rate */
	double startdebt ; /* input start debt */
	double debt = 0 ; /* adjusted to each year */
	double income ; /* input total income budget */
	short do_income = NO ; /* boolean yes/no income argument given */
	unsigned int endyear ; /* input end year */
	unsigned int year = 0 ; /* number of years (or other time unit) */

	/* help */
	if ( 2 >= argc ) {
		if ( 2 <= argc && 0 == strcmp ( "--version" , argv [ 1 ] ) ) {
			printf ( "debt: version 1.0 2008 (C) JHBoersema License GPL (no "
					"warranty).\n" ) ;
			return 0 ;
		}
		printf ( "Usage: debt INTERESTRATE LOANVALUE MAXTERMS [INCOME]\n" ) ;
		printf ( " Info: run program without arguments.\n" ) ;
		printf ( "Compute debt instability and bankruptcy for roll-over debt "
					"pyramid racket.\n" ) ;
		if ( 2 <= argc && 0 == strcmp ( "--help" , argv [ 1 ] ) ) {
			return 0 ;
		}
		printf ( "\nThis program calculates debt values, assuming old debt "
    "is being payed down\nwith new debt, which creates an increasingly "
		"unstable pyramid-game. Quantities\nto give are the starting debt "
		"LOANVALUE for the first term (year) "
		"the interest\nrate on the debt INTERESTRATE, and the number of "
		"itterations you " 
		"want it\ncalculated for MAXTERMS. Optional INCOME is used to mark the"
		"points where the\npyramid becomes more instable.\n\n" ) ;
		printf ( "more (algorithm): <enter>\n" ) ;
		getchar ( ) ;
		printf ( "\nAlgorithm: the idea is to compute for a Government that "
		"has a shortage\nof funds in a year, for which it does not raise taxes "
		"or decrease spending,\nbut takes a loan. Next year the Government pays "
		"back the loans, and loans\nagain for the same lack of funds, and also "
		"takes loans to pay back\nthe loans and the interest. It never raises "
		"taxes or decreases spending.\nThat system causes bankruptcy of the "
		"Government and financial chaos. This\nprogram calculates when the "
		"pyramid scheme of a routinely lending Government\ncollapses.\n\n" ) ;
		printf ( "more (example): <enter>\n" ) ;
		getchar ( ) ;
		printf ( 
		"Year 1 Income from taxation: 100 billion\n"
		"  Spending: 120 billion\n"
		"       -------- +\n"
		"  Finance gap: 100 b - 120 b = 20 billion\n"
		"  -> Selling bonds and loans at 10%% over 1 years for sum: 20 billion\n"
		"\n"
		"Year 2 Income from taxation: 100 billion\n"
		"  Spending: 120 billion\n"
		" +Spending to pay back loans: 20 billion \n"
		" +Spending to pay back interest on those loans at 10%% = 2 billion\n"
		"       -------- +\n"
		"  Finance gap (over-spending + lending): 100 b - 120 b - 20 b - 2 b\n"
		"                                       = 42 billion lack of funds\n"
		"  -> Selling bonds and loans at 10%% over 1 years for sum: 42 billion\n"
    "Year 3  gap: 100 b - 120 b -   42 b (loan) -   4.2 b   = -   66.2 billion\n"
		"(...)\n"
    "Year 20 gap: 100 b - 120 b - 1023 b (loan) - 102 b (%%) = - 1145   billion\n\n" ) ;
		printf ( "more (interpretation of figures): <enter>\n" ) ;
		getchar ( ) ;
		printf ( 
		"Payments in interest alone are larger then original budget of 100 b\n"
		"after 20 years. At that point lending becomes lending to pay interest,\n"
		"at which point the pyramid becomes highly unstable as there are more\n"
		"and more lenders necessary to come up with the funds, but the money is\n"
		"simply being rerouted to other lenders and their demands. At a certain\n"
		"point there are no more willing lenders, then all debt can not be\n"
		"repayed and the Pyramid collapses financially. If some loans are not\n"
		"being repayed, lenders will likely not want to lend money for next years\n"
		"giant budget shortage (lending baloons quickly.) At that point it will\n"
		"either have to raise taxes or cut spending after all, but only to pay\n"
		"for debts and interest, or it will have to declare bankruptcy and\n"
		"decide not to pay down any debt, which will cause the last lenders\n"
		"to lose their gambling money, while lenders that got out ahead of the\n"
		"collapse end up richer. If it decides to raise taxes or cut spending,\n"
		"but continues with the lending pyramid, then the collapse is postponed,\n" ) ;
		printf ( "more: <enter>\n" ) ;
		getchar ( ) ;
		printf ( 
		"but not prevented. Different interest rates on loans make the pyramid\n"
		"grow at differen speeds. Key points are: losses to rent profit equal\n"
		"the original budget gap - at that point taxation or cutting spending\n"
		"would have placed the same burden on the taxpayers, total debt equals\n"
		"entire budget - at that point all debt can not be repayed from\n"
		"a single year budged, and losses to rent profit equal entire bugdet -\n"
		"at that point the Government is effectively bankrupt and just pumping\n"
		"money from one lender to the other, for as long as it lasts. Once the\n"
		"lenders become suspicious to the instability the lack of lenders cause\n"
		"the pyramid to collapse. The pyramid may in reality collapse before\n"
		"it is technically bankrupt, because lenders can do the computations\n"
		"and notice that the Government will go bankrupt soon. That will at\n"
		"least increase interest rates on the loans because they are more\n"
		"risky, and this brings bankruptcy closer. In the end it may collapse\n"
		"ahead of a technical bankruptcy when fear induced higher interest\n"
		"demands on the Government, increasing the fear, and then yet higher\n"
		"interest rates, yet more fear, etc. The pyramid may also collapse\n"
		"after the point where the Government is doing nothing but sending\n"
		"money from old lenders to new lenders, going on until the entire\n"
		"world population is in the game, if nobody thinks this is a problem\n"
		"Once the last lender has lend, though, it will collapse for lack of\n"
		"lenders. If everyone is in the game there is no source of new loans.\n") ;
		printf ( "more: <enter>\n" ) ;
		getchar ( ) ;
		printf ( "                      /\\\n                    /____\\\n                   ________\n                 /   The    \\\n               /   pyramid    \\\n             /______game________\\ of Government finances.\nWhen will your Government collapse ? Compute it ahead of the game !\n" );
		return 0 ;
	}

	/* get args */
	rent = strtod ( argv [ 1 ] , NULL ) / 100 ;
	startdebt = strtod ( argv [ 2 ] , NULL ) ;
	endyear = strtod ( argv [ 3 ] , NULL ) ;
	if ( 5 <= argc ) {
		income = strtod ( argv [ 4 ] , NULL ) ;
		do_income = YES ;
	}
	/* report args */
	printf ( "       Paying finance-gap with new debt, pyramid instability:\n" ) ;
	printf ( "Interest rate per term: %f%% (fraction: %f)\n" , rent * 100 , rent ) ;
	printf ( "    Debt in first term: %c %.2f (%E)\n" , CURRENCY , startdebt 
			, startdebt ) ;
	printf ( "   Compute for maximum: %i terms\n" , endyear ) ;
	if ( 5 <= argc ) {
	printf ( "       Income per term: %c %.2f (%E)\n" , CURRENCY , income , income ) ; 
	}

	/* start loop over each year, cummulative computation */
	printf ( "(year, total-debt, rent-losses)\n" ) ;
	debt = 0 ;
	while ( year < endyear ) {
		year ++ ;
		debt = startdebt + debt + debt * rent ;
		printf ( "(%3d, %8.2f, %7.2f)" , year , debt , debt * rent ) ;
		if ( YES == do_income ) {
			/* the key points where the pyramid enters a new phase */
			printf ( " [" ) ;
			 /* the point where total debt is larger then total budget */
			if ( income < debt ) {
				printf ( " debt>income " ) ;
			}
			 /* the point where rent-losses outweigh original finance gap */
			if ( debt * rent > startdebt ) {
				printf ( " rent>gap " ) ;
			}
			 /* the point where rent-losses are larger then income (bankruptcy) */
			if ( debt * rent > income ) {
				printf ( " rent>income BANKRUPT" ) ;
#ifdef TERMINATE_AT_BANKRUPT 
				printf ( "]\n" ) ;
				return 0 ;
#endif
			}
			printf ( "]\n" ) ;
		}
		else {
			printf ( "\n" ) ;
		}
	}
	return 0 ;
}

