Skip to content

Instantly share code, notes, and snippets.

@Zengez
Created August 23, 2014 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zengez/1acd7a0d02eeb330f384 to your computer and use it in GitHub Desktop.
Save Zengez/1acd7a0d02eeb330f384 to your computer and use it in GitHub Desktop.
function [ FlowAnalysis ] = CashFlowAnalysisDailyComp( Principle, APR, Payment, MinPay )
%CASHFLOWANALYSISDAILYCOMP [ FlowAnalysis ] = CashFlowAnalysisDailyComp(
%Payment, Principle, APR, MinPay ) This will take inputs and assign an
%analysis number to the loan which depicts how good it is to pay off this
%loan given a certain payment number.
%The lower the analysis numeric
%result, the better it is to pay the loan... i.e. the higher the individual
%payments, the faster you pay the loan off. This means you increase your
%cashflow to pay off other loans faster and yields a better analysis
%number.
TempVal = 1 - APR*Principle/(1200*Payment);
%How many payments you need to make
TotalPeriods = ceil(-log(TempVal)/log(1 + APR/1200));
%The principle after the first payment
TempPrinciple = LoanCalcDailyComp(Principle, APR, Payment);
%Pay things off
for k = 1:TotalPeriods - 1
TempPrinciple = LoanCalcDailyComp(TempPrinciple, APR, Payment);
end
%How much extra you have from not needing a full payment at the end of the
%last pay period.
Overpaid = TempPrinciple;
%Find out how much you needed to spend to pay off loan
TotalSpent = TotalPeriods*Payment + Overpaid;
%Find the ratio of pay off to how much cash flow you freed up.
FlowAnalysis = [TotalSpent/MinPay, TotalPeriods, -Overpaid];
end
function [ NewVal ] = LoanCalcDailyComp( Principle, APR, Payment, varargin )
%LoanCalcDailyComp( Principle, Interest, Payment ) Calculates the new
%principle on a loan after a month of being at the balance of the current
%principle, and after the current payment.
%For just 1 month payment
NewVal = Principle*(1+APR/36500)^30 - Payment;
%For duration payments
if nargin == 4
Time = varargin{1};
for j = 1:Time-1
NewVal = NewVal*(1+APR/36500)^30 - Payment;
end
end
end
function [ ] = LoanPay( MonthlyPayment, LoanFile )
%This is the primary program to calculate loans. It takes the total monthly
%payment you want to make toward debt (Remeber this INCLUDES minimum
%payments... this is NOT the payment you make on top of monthly payments)
%as well as a matrix of all your loans. See LoanPrioritizer for the
%formatting of the loan matrix)
%First get the Analysis
Analysis = LoanPrioritizer(MonthlyPayment, LoanFile);
NewRow = {'Loan #', 'Months to Pay', 'Total Payment'};
[Len, Wid] = size(Analysis);
%intialize some Variables
Time = 0;
Leftover = 0;
TempFile = LoanFile;
TrackingMatrix = zeros(Len, 3);
fileID = fopen('Results.txt','w');
fprintf(fileID,'FORMATTING DESCRIPTION:\n\nThe matricies below are formated as follows: \nThe first column is the order of the loans you should pay off, \nThe second column is how many months it will take to pay off that loan. \nThe Last column is how much you should pay towards that loan each month.\n');
fprintf(fileID,'For example if your matrix looks like this: \n')
%fprintf(fileID,'LoanNumber','Payment Length','Payment');
Example = [3, 5, 266; 1, 10, 2291; 2, 7, 312];
%fprintf(fileID,'%8.2f %8.2f %8.2f\n'
fprintf(fileID,' Loan# Payoff Time(Months) Payment\n %3i %3i $%6.2f\n',Example');
fprintf(fileID,' This means you should pay the third loan you listed first, \nand it should take 5 months at $266 a month. \n Next you shold pay off the first loan you listed \nand it should take 10 months at $291 a month. \n Finally you should pay the second loan you listed \nwhich should take 7 months at $312 a month.\n\n\n\n\n');
%We will iterate down the matrix, paying one loan at a time
for j = 1:Len
Temp = sum(TempFile);
%Find the total minimum payment needed
ExtraPayment = MonthlyPayment - Temp(4);
%Leftover from paying off the last loan, for the first payment on the
%next loan
Overpaid = Leftover;
%Find the next best loan to pay off
[X LoanRow] = min(Analysis(:,1));
%Pull from analysis how long you need to pay off that loan
NextTimeInterval = Analysis(LoanRow, 2);
%Track which loan you're paying off, how long it takes, and what the
%payment each month should be
TrackingMatrix(j, 1) = LoanFile(LoanRow,1);
TrackingMatrix(j, 2) = NextTimeInterval;
TrackingMatrix(j, 3) = ExtraPayment + TempFile(LoanRow, 4);
%Iterate the current loan until it is paid off. First line includes the
%remainder from paying off the last loan.
TempFile(LoanRow, 2) = LoanCalcDailyComp(TempFile(LoanRow, 2), TempFile(LoanRow, 3), TempFile(LoanRow, 4) + ExtraPayment + Overpaid);
TempFile(LoanRow, 2) = LoanCalcDailyComp(TempFile(LoanRow, 2), TempFile(LoanRow, 3), TempFile(LoanRow, 4) + ExtraPayment, NextTimeInterval - 1);
%Iterate other loans with minimum payments while we pay one of them
%off.
for i = 1:Len
if i ~= LoanRow
TempFile(i, 2) = LoanCalcDailyComp(TempFile(i, 2), TempFile(i, 3), TempFile(i, 4));
end
end
Leftover = Analysis(LoanRow, 3);
%Fix the Loan File to make sure it doesn't keep trying to pay off already
%paid off loans
if TempFile(LoanRow, 2) < 0
TempFile(LoanRow, 2) = inf;
TempFile(LoanRow, 4) = 0;
end
%Re-analyse the Loan File for next target loan
Analysis = LoanPrioritizer(MonthlyPayment, TempFile);
TempFile;
Analysis;
end
TotalTimeToPayOffLoans = sum(TrackingMatrix(:,2));
TotalPayOffCost = TotalTimeToPayOffLoans*MonthlyPayment-Leftover;
fprintf(fileID,'ACTUAL RESULTS:\n\n');
fprintf(fileID,'METHOD 1: MY HOMEBREW OPTIMIZING DESIGN\n\n\n\n');
fprintf(fileID,'This is the matrix assuming we want to use my formula.\n');
%This was some error checking code, mostly gone now.
fprintf(fileID,' Loan# Payoff Time(Months) Payment\n %3i %3i $%6.2f\n',TrackingMatrix');
fprintf(fileID,'Using this method you would pay all the debt off in %i months. \n This method costs a total of $%.2f.\n\n\n\n', TotalTimeToPayOffLoans, TotalPayOffCost);
%Just some handy info and I'm too lazy to fprint.
%Now let's check if we were to pay off lowest loan first.
%First get the Analysis
Analysis = LoanPrioritizer(MonthlyPayment, LoanFile);
[Len, Wid] = size(Analysis);
%intialize some Variables
Time = 0;
Leftover = 0;
TempFile = LoanFile;
TrackingMatrix = zeros(Len, 3);
for j = 1:Len
Temp = sum(TempFile);
%Find the total minimum payment needed
ExtraPayment = MonthlyPayment - Temp(4);
%Leftover from paying off the last loan, for the first payment on the
%next loan
Overpaid = Leftover;
%Find the next best loan to pay off
[X LoanRow] = min(TempFile(:,2));
%Pull from analysis how long you need to pay off that loan
NextTimeInterval = Analysis(LoanRow, 2);
%Track which loan you're paying off, how long it takes, and what the
%payment each month should be
TrackingMatrix(j, 1) = LoanFile(LoanRow,1);
TrackingMatrix(j, 2) = NextTimeInterval;
TrackingMatrix(j, 3) = ExtraPayment + TempFile(LoanRow, 4);
%Iterate the current loan until it is paid off. First line includes the
%remainder from paying off the last loan.
TempFile(LoanRow, 2) = LoanCalcDailyComp(TempFile(LoanRow, 2), TempFile(LoanRow, 3), TempFile(LoanRow, 4) + ExtraPayment + Overpaid);
TempFile(LoanRow, 2) = LoanCalcDailyComp(TempFile(LoanRow, 2), TempFile(LoanRow, 3), TempFile(LoanRow, 4) + ExtraPayment, NextTimeInterval - 1);
%Iterate other loans with minimum payments while we pay one of them
%off.
for i = 1:Len
if i ~= LoanRow
TempFile(i, 2) = LoanCalcDailyComp(TempFile(i, 2), TempFile(i, 3), TempFile(i, 4));
end
end
Leftover = Analysis(LoanRow, 3);
%Fix the Loan File to make sure it doesn't keep trying to pay off already
%paid off loans
if TempFile(LoanRow, 2) < 0
TempFile(LoanRow, 2) = inf;
TempFile(LoanRow, 4) = 0;
end
%Re-analyse the Loan File for next target loan
Analysis = LoanPrioritizer(MonthlyPayment, TempFile);
end
TotalTimeToPayOffLoans = sum(TrackingMatrix(:,2));
TotalPayOffCost = TotalTimeToPayOffLoans*MonthlyPayment-Leftover;
fprintf(fileID,'METHOD 2: PAY THE LOWEST PRINCIPLE FIRST. THE SO CALLED "SNOWBALL METHOD"\n\n\n\n')
fprintf(fileID,'This is the matrix assuming we want to pay the lowest principle loan first.\n');
fprintf(fileID,' Loan# Payoff Time(Months) Payment\n %3i %3i $%6.2f\n',TrackingMatrix');
fprintf(fileID,'Using this method you would pay all the debt off in %i months. \n This method costs a total of $%.2f.\n\n\n\n', TotalTimeToPayOffLoans, TotalPayOffCost);
%And Now let's check paying highest interest first:
%First get the Analysis
Analysis = LoanPrioritizer(MonthlyPayment, LoanFile);
[Len, Wid] = size(Analysis);
%intialize some Variables
Time = 0;
Leftover = 0;
TempFile = LoanFile;
TrackingMatrix = zeros(Len, 3);
for j = 1:Len
Temp = sum(TempFile);
%Find the total minimum payment needed
ExtraPayment = MonthlyPayment - Temp(4);
%Leftover from paying off the last loan, for the first payment on the
%next loan
Overpaid = Leftover;
%Find the next best loan to pay off
[X LoanRow] = max(TempFile(:,3));
%Pull from analysis how long you need to pay off that loan
NextTimeInterval = Analysis(LoanRow, 2);
%Track which loan you're paying off, how long it takes, and what the
%payment each month should be
TrackingMatrix(j, 1) = LoanFile(LoanRow,1);
TrackingMatrix(j, 2) = NextTimeInterval;
TrackingMatrix(j, 3) = ExtraPayment + TempFile(LoanRow, 4);
%Iterate the current loan until it is paid off. First line includes the
%remainder from paying off the last loan.
TempFile(LoanRow, 2) = LoanCalcDailyComp(TempFile(LoanRow, 2), TempFile(LoanRow, 3), TempFile(LoanRow, 4) + ExtraPayment + Overpaid);
TempFile(LoanRow, 2) = LoanCalcDailyComp(TempFile(LoanRow, 2), TempFile(LoanRow, 3), TempFile(LoanRow, 4) + ExtraPayment, NextTimeInterval - 1);
%Iterate other loans with minimum payments while we pay one of them
%off.
for i = 1:Len
if i ~= LoanRow
TempFile(i, 2) = LoanCalcDailyComp(TempFile(i, 2), TempFile(i, 3), TempFile(i, 4));
end
end
Leftover = Analysis(LoanRow, 3);
%Fix the Loan File to make sure it doesn't keep trying to pay off already
%paid off loans
if TempFile(LoanRow, 2) < 0
TempFile(LoanRow, 2) = inf;
TempFile(LoanRow, 4) = 0;
TempFile(LoanRow, 3) = 0;
end
Analysis;
TempFile;
%Re-analyse the Loan File for next target loan
Analysis = LoanPrioritizer(MonthlyPayment, TempFile);
end
%This was some error checking code, mostly gone now.
TotalTimeToPayOffLoans = sum(TrackingMatrix(:,2));
TotalPayOffCost = TotalTimeToPayOffLoans*MonthlyPayment-Leftover;
fprintf(fileID,'METHOD 3: PAY HIGHEST INTEREST RATES FIRST. THE SO CALLED "Avalanche" METHOD:\n\n\n\n');
fprintf(fileID,'This is the matrix assuming we want to payoff the highest interest rate items first.\n');
fprintf(fileID,' Loan# Payoff Time(Months) Payment\n %3i %3i $%6.2f\n',TrackingMatrix');
fprintf(fileID,'Using this method you would pay all the debt off in %i months. \n This method costs a total of $%.2f.\n\n\n\n', TotalTimeToPayOffLoans, TotalPayOffCost);
%Just some handy info and I'm too lazy to fprint.
TotalTimeToPayOffLoans = sum(TrackingMatrix(:,2));
%Now evenly divide the payments among all loans.
TempFile = LoanFile;
Temp = sum(TempFile);
Time = 0;
%Find the total minimum payment needed
ExtraPayment = (MonthlyPayment - Temp(4))/Len;
while Temp(2) > 0
TempAddPay = 0;
for i = 1:Len
if TempFile(i,2)>0
TempFile(i, 2) = LoanCalcDailyComp(TempFile(i, 2), TempFile(i, 3), TempFile(i, 4) + ExtraPayment);
end
end
while min(TempFile(:,2))~=0
[A I] = min(TempFile(:,2));
TempAddPay = TempAddPay + A;
TempFile(I, 2:4) = 0;
end
Temp = sum(TempFile);
ExtraPayment = (MonthlyPayment - Temp(4)+TempAddPay)/Len;
Time = Time+1;
end
TotalTimeToPayOffLoans = Time;
TotalPayOffCost = MonthlyPayment * Time - TempAddPay;
fprintf(fileID,'METHOD 4: EVENLY SPLIT EXTRA PAYMENT AMONG ALL LOANS. THE "LAZY" METHOD:\n\n\n\n');
fprintf(fileID,'This assumes we split all the extra money evenly among all loans.\n');
fprintf(fileID,'Using this method you would pay all the debt off in %i months. \n This method costs a total of $%.2f.\n\n\n\n', TotalTimeToPayOffLoans, TotalPayOffCost);
end
function [ FinalAnalysis ] = LoanPrioritizer( TotalPayment, LoanFile )
%LOANPRIORITIZER LoanPrioritizer( TotalPayment, LoanFile ) this will take
%in a matrix of loan information. The matrix should be a new row for each
%loan, and have a the following format: [Loan Number, Principle, APR,
%MinPay]. Make sure to enter APR as the percentage, not decimal. The result
%of this function is a matrix with 3 colums and a row for each loan where
%the first collum is the "score" of that loan, the second collum is how
%many periods it takes to completely pay off that loan, and the third is
%the amount left over from the last payperiod which can go toward a new
%loan.
%Check to see if your loan file is at least the right size. Hopefully
%formatting is correct.
[Len, Wid] = size(LoanFile);
if Wid ~= 4
fprintf('Your Loanfile does not appear to be formatted correctly. \nPlease put it in the right format. \nType: \n\nhelp LoanPrioritizer\n\nfor more information.\n')
end
ColSum = sum(LoanFile);
TotalMinPay = ColSum(4);
%Check to see if you can even make your payment obligations.
if TotalMinPay > TotalPayment
fprintf('it appears your total monthly payment cannot cover your total \n minimum payments. This is bad, fix it.\n')
end
RemainPay = TotalPayment - TotalMinPay;
Analysis = zeros(Len,3);
%Build the analysis of your loans. The result will score each loan and give
%you some information about the loan.
for L = 1:Len
if LoanFile(L, 2) == inf
Analysis(L, :) = [ inf, inf, 0];
else
TempAnalysis = CashFlowAnalysisDailyComp(LoanFile(L,2), LoanFile(L,3), LoanFile(L,4)+RemainPay, LoanFile(L,4));
Analysis(L, :) = TempAnalysis(1, :);
end
%Assign final output.
FinalAnalysis = Analysis;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment