VPL
According to the Wikipedia formula:
Net present value
[...] is the mathematical-financial formula capable of determining the present value of future discounted payments at an appropriate interest rate, less the investment cost.
Therefore, the calculation consists of summing the values of future payments (calculated at present) and subtracting the value of the initial investment.
A possible VPL implementation in Javascript:
/*
* Calcula o Valor Presente Líquido para
* uma variação de período constante
*
* @taxa => taxa de desconto
* @montantes => vetor com os valores com os recebimentos ou pagamentos
*
*/
function vpl(taxa, montantes)
{
var ret = montantes[0];
for (var i=1; i<montantes.length; i++)
ret += montantes[i] / Math.pow( (1.0 + taxa), i);
return ret;
}
TIR
According to Wikipedia:
Internal rate of return
[...] is a hypothetical discount rate that, when applied to a cash flow, makes the values of expenses, brought to present value, equal to the values of returns on investments, also brought to present value.
The IRR is the interest rate that "Zera" the NPV, ie, vpl(TIR, montantes) = 0
.
For the calculation of the IRR, it is necessary to find the root of the above equation.
There are several methods to find this root:
Algorithm to find root
Below is an example that uses the Bisection (which is one of the simplest to implement):
/*
* Calcula a Taxa Interna de Retorno (Método da Bisseção)
*
* @montantes => vetor com os valores
*/
function tir(montantes)
{
var ret = -1000000000.0;
var juros_inicial = -1.0;
var juros_medio = 0.0;
var juros_final = 1.0;
var vpl_inicial = 0.0;
var vpl_final = 0.0;
var vf = 0.0;
var erro = 1e-5; // Valor mínimo que determina que a raiz foi encontrada
// Procura um possível intervalo para a raiz
// O looping deve terminar antes de i chegar a 100!
for (var i=0; i<100; i++) {
vpl_inicial = vpl(juros_inicial, montantes);
vpl_final = vpl(juros_final, montantes);
if (sinal(vpl_inicial) != sinal(vpl_final))
break;
juros_inicial -= 1.0;
juros_final += 1.0;
};
// Contador que evita um looping infinito
var count = 0;
// Busca por Bisseção
for (;;) {
var juros_medio = (juros_inicial + juros_final) / 2.0;
var vpl_medio = vpl(juros_medio, montantes)
if (Math.abs(vpl_medio) <= erro) {
// Resultado foi encontrado
return juros_medio*100.0;
};
if (sinal(vpl_inicial) == sinal(vpl_medio)) {
juros_inicial = juros_medio;
vpl_inicial = vpl(juros_medio, montantes);
} else {
juros_final = juros_medio;
vpl_final = vpl(juros_medio, montantes);
};
// Evita um possível looping infinito
if (++count > 10000)
throw "looping inválido";
};
// O algoritmo nunca deve chegar aqui.
return ret;
};
See working on jsfiddle
Below is a link to a page that makes these (and other) calculations interactively (more didactic):
Do the Count
You could post along with your question what you’ve got so far?
– Chun
ready friend, but the formatting was not good.
– Luan Rabelo
To format code, just select the code and click the button
{}
above the editor.– Chun
Thanks Chun, well the code is this, where it prints the result automatically using the input onkeyup.
– Luan Rabelo