Coldfusion 冷聚变内部收益率计算
我试图在excel中复制IRR(内部收益率)函数。我在riaforge.com上找到了一个cfc,但它返回的值与excel的irr值不同 牛顿-拉斐逊方法使用导数,我不知道如何计算coldfusion中的导数Coldfusion 冷聚变内部收益率计算,coldfusion,Coldfusion,我试图在excel中复制IRR(内部收益率)函数。我在riaforge.com上找到了一个cfc,但它返回的值与excel的irr值不同 牛顿-拉斐逊方法使用导数,我不知道如何计算coldfusion中的导数 year cash flow ---- -------- 0 -4000 1 1200 2 1410 3 1875 4 1050 应返回14.3%(来自维基百科的示例) 以前有人这样做过吗? 谢谢我不知道什么是Co
year cash flow
---- --------
0 -4000
1 1200
2 1410
3 1875
4 1050
应返回14.3%(来自维基百科的示例)
以前有人这样做过吗?
谢谢我不知道什么是ColdFusion,但是找到内部收益率的想法非常简单 内部收益率是一个数字,即
sum i = 0 to N C_i * (1 + r)^(-t_i) = 0
其中有N+1
现金流C_0,C_1,…,C_N
时t_0,t_1,…,t_N
。定义
f(r) = sum i = 0 to N C_i * (1 + r)^(-t_i).
然后
选择初始猜测r_0
并通过
r_{n + 1} = r_n - f(r_n) / f'(r_n)
在您的具体示例中,您有
t_0 = 0 C_0 = -4000
t_1 = 1 C_1 = 1200
t_2 = 2 C_2 = 1410
t_3 = 3 C_3 = 1875
t_4 = 4 C_4 = 1050
尝试猜测r\u 0=0.1
同样,我不知道什么是ColdFusion,但它必须是一种编程语言,因此它应该允许计算这个基本数学。我不知道ColdFusion是什么,但找到IRR的想法非常简单
<cffunction name="calcIRR">
<cfargument name="arrCashFlow" type="Array" required="true" hint="array of cashflow">
<cfscript>
var guess = 0.1;
var inc = 0.00001;
do {
guess += inc;
npv = 0; //net present value
for (var i=1; i<=arrayLen(arguments.arrCashFlow); i++) {
npv += arguments.arrCashFlow[i] / ((1 + guess) ^ i);
}
} while ( npv > 0 );
guess = guess * 100;
</cfscript>
<cfreturn guess>
</cffunction>
<cfscript>
cFlow = arrayNew(1);
cFlow[1] = -4000;
cFlow[2] = 1200;
cFlow[3] = 1410;
cFlow[4] = 1875;
cFlow[5] = 1050;
c = calcIRR(cFlow);
</cfscript>
<cfdump var="#cFlow#">
<cfdump var="#c#">
内部收益率是一个数字,即
sum i = 0 to N C_i * (1 + r)^(-t_i) = 0
其中有N+1
现金流C_0,C_1,…,C_N
时t_0,t_1,…,t_N
。定义
f(r) = sum i = 0 to N C_i * (1 + r)^(-t_i).
然后
选择初始猜测r_0
并通过
r_{n + 1} = r_n - f(r_n) / f'(r_n)
在您的具体示例中,您有
t_0 = 0 C_0 = -4000
t_1 = 1 C_1 = 1200
t_2 = 2 C_2 = 1410
t_3 = 3 C_3 = 1875
t_4 = 4 C_4 = 1050
尝试猜测r\u 0=0.1
同样,我不知道ColdFusion是什么,但它必须是一种编程语言,因此它应该允许计算这个基本数学。
<cffunction name="calcIRR">
<cfargument name="arrCashFlow" type="Array" required="true" hint="array of cashflow">
<cfscript>
var guess = 0.1;
var inc = 0.00001;
do {
guess += inc;
npv = 0; //net present value
for (var i=1; i<=arrayLen(arguments.arrCashFlow); i++) {
npv += arguments.arrCashFlow[i] / ((1 + guess) ^ i);
}
} while ( npv > 0 );
guess = guess * 100;
</cfscript>
<cfreturn guess>
</cffunction>
<cfscript>
cFlow = arrayNew(1);
cFlow[1] = -4000;
cFlow[2] = 1200;
cFlow[3] = 1410;
cFlow[4] = 1875;
cFlow[5] = 1050;
c = calcIRR(cFlow);
</cfscript>
<cfdump var="#cFlow#">
<cfdump var="#c#">
var guess=0.1;
var inc=0.00001;
做{
猜+=公司;
净现值=0;//净现值
对于(VarI=1;I0);
猜测=猜测*100;
cFlow=arrayNew(1);
cFlow[1]=-4000;
cFlow[2]=1200;
cFlow[3]=1410;
cFlow[4]=1875;
cFlow[5]=1050;
c=钙抑制率(cFlow);
var guess=0.1;
var inc=0.00001;
做{
猜+=公司;
净现值=0;//净现值
对于(VarI=1;I0);
猜测=猜测*100;
cFlow=arrayNew(1);
cFlow[1]=-4000;
cFlow[2]=1200;
cFlow[3]=1410;
cFlow[4]=1875;
cFlow[5]=1050;
c=钙抑制率(cFlow);
扩展到Jason所说的,您需要实现一个高效工作的代码,而不是Falconeyes建议的暴力算法。这里没有任何个人信息。我第一次将IRR编程为服务器端脚本时,它使用暴力,一天后,我的网络主机打电话给我,说他们让我的网站离线因为代码消耗了100%的系统资源
下面是一个循序渐进的使用方法,您可以遵循它并在冷聚变中实现这些想法
f(x) = -4000(1+i)^0 +1200(1+i)^-1 +1410(1+i)^-2 +1875(1+i)^-3 +1050(1+i)^-4
f'(x) = -1200(1+i)^-2 -2820(1+i)^-3 -5625(1+i)^-4 -4200(1+i)^-5
x0 = 0.1
f(x0) = 382.0777
f'(x0) = -9560.2616
x1 = 0.1 - 382.0777/-9560.2616 = 0.139965195884
Error Bound = 0.139965195884 - 0.1 = 0.039965 > 0.000001
x1 = 0.139965195884
f(x1) = 25.1269
f'(x1) = -8339.5497
x2 = 0.139965195884 - 25.1269/-8339.5497 = 0.142978177747
Error Bound = 0.142978177747 - 0.139965195884 = 0.003013 > 0.000001
x2 = 0.142978177747
f(x2) = 0.126
f'(x2) = -8256.0861
x3 = 0.142978177747 - 0.126/-8256.0861 = 0.142993440675
Error Bound = 0.142993440675 - 0.142978177747 = 1.5E-5 > 0.000001
x3 = 0.142993440675
f(x3) = 0
f'(x3) = -8255.6661
x4 = 0.142993440675 - 0/-8255.6661 = 0.142993441061
Error Bound = 0.142993441061 - 0.142993440675 = 0 < 0.000001
IRR = x4 = 0.142993441061 or 14.3%
f(x)=-4000(1+i)^0+1200(1+i)^-1+1410(1+i)^-2+1875(1+i)^-3+1050(1+i)^-4
(x):-1200(1+i)^-2-2820(1+i)^-3-5625(1+i)^-4-4200(1+i)^-5
x0=0.1
f(x0)=382.0777
f'(x0)=-9560.2616
x1=0.1-382.0777//-9560.2616=0.139965195884
错误界限=0.139965195884-0.1=0.039965>0.000001
x1=0.139965195884
f(x1)=25.1269
f'(x1)=-8339.5497
x2=0.139965195884-25.1269//-8339.5497=0.142978177747
错误界限=0.142978177747-0.139965195884=0.003013>0.000001
x2=0.142978177747
f(x2)=0.126
f'(x2)=-8256.0861
x3=0.142978177747-0.126//-8256.0861=0.142993440675
错误界限=0.142993440675-0.142978177747=1.5E-5>0.000001
x3=0.142993440675
f(x3)=0
f'(x3)=-8255.6661
x4=0.142993440675-0/-8255.6661=0.142993441061
误差范围=0.142993441061-0.142993440675=0<0.000001
内部收益率=x4=0.142993441061或14.3%
扩展到Jason所说的,您需要实现一个高效工作的代码,而不是Falconeyes建议的暴力算法。这里没有任何个人信息。我第一次将IRR编程为服务器端脚本时,它使用暴力,一天后,我的网络主机打电话给我,说他们让我的网站离线因为代码消耗了100%的系统资源
下面是一个循序渐进的使用方法,您可以遵循它并在冷聚变中实现这些想法
f(x) = -4000(1+i)^0 +1200(1+i)^-1 +1410(1+i)^-2 +1875(1+i)^-3 +1050(1+i)^-4
f'(x) = -1200(1+i)^-2 -2820(1+i)^-3 -5625(1+i)^-4 -4200(1+i)^-5
x0 = 0.1
f(x0) = 382.0777
f'(x0) = -9560.2616
x1 = 0.1 - 382.0777/-9560.2616 = 0.139965195884
Error Bound = 0.139965195884 - 0.1 = 0.039965 > 0.000001
x1 = 0.139965195884
f(x1) = 25.1269
f'(x1) = -8339.5497
x2 = 0.139965195884 - 25.1269/-8339.5497 = 0.142978177747
Error Bound = 0.142978177747 - 0.139965195884 = 0.003013 > 0.000001
x2 = 0.142978177747
f(x2) = 0.126
f'(x2) = -8256.0861
x3 = 0.142978177747 - 0.126/-8256.0861 = 0.142993440675
Error Bound = 0.142993440675 - 0.142978177747 = 1.5E-5 > 0.000001
x3 = 0.142993440675
f(x3) = 0
f'(x3) = -8255.6661
x4 = 0.142993440675 - 0/-8255.6661 = 0.142993441061
Error Bound = 0.142993441061 - 0.142993440675 = 0 < 0.000001
IRR = x4 = 0.142993441061 or 14.3%
f(x)=-4000(1+i)^0+1200(1+i)^-1+1410(1+i)^-2+1875(1+i)^-3+1050(1+i)^-4
(x):-1200(1+i)^-2-2820(1+i)^-3-5625(1+i)^-4-4200(1+i)^-5
x0=0.1
f(x0)=382.0777
f'(x0)=-9560.2616
x1=0.1-382.0777//-9560.2616=0.139965195884
错误界限=0.139965195884-0.1=0.039965>0.000001
x1=0.139965195884
f(x1)=25.1269
f'(x1)=-8339.5497
x2=0.139965195884-25.1269//-8339.5497=0.142978177747
错误界限=0.142978177747-0.139965195884=0.003013>0.000001
x2=0.142978177747
f(x2)=0.126
f'(x2)=-8256.0861
x3=0.142978177747-0.126//-8256.0861=0.142993440675
错误界限=0.142993440675-0.142978177747=1.5E-5>0.000001
x3=0.142993440675
f(x3)=0
f'(x3)=-8255.6661
x4=0.142993440675-0/-8255.6661=0.142993441061
误差范围=0.142993441061-0.142993440675=0<0.000001
内部收益率=x4=0.142993441061或14.3%
我尝试了所有提交的解决方案,但没有一个像excel那样工作。
以下是计算XIRR的代码,该代码正常工作。对于IRR,您只需更改此行:
<cfset npv = npv + (arguments.values[i] / ((1 + arguments.rate) ^ time_span))>
这是整个剧本
<cfoutput>
#XIRR(values=[-5000,500,110500], dates=["2015-07-06","2016-07-06","2017-07-06"])#
</cfoutput>
<cffunction name="XIRR">
<cfargument name="values" required="true">
<cfargument name="dates" required="true">
<cfset var do_calculation = check_data(values=arguments.values, dates=arguments.dates)>
<cfif do_calculation.is_ok>
<cfset var rate = 1>
<cfset var npv = calculate_NPV(rate=rate, values=arguments.values, dates=arguments.dates)>
<cfloop condition="#npv# gt 10e-6">
<cfset rate = rate + 1>
<cfset npv = calculate_NPV(rate=rate, values=arguments.values, dates=arguments.dates)>
</cfloop>
<cfloop from="1" to="6" index="pow">
<cfset fac = 1 / (10 ^ pow)>
<cfset npv = 0>
<cfloop condition="#npv# lt 10e-6">
<cfset rate = rate - fac>
<cfset npv = calculate_NPV(rate=rate, values=arguments.values, dates=arguments.dates)>
</cfloop>
<cfset rate = rate + fac>
</cfloop>
<cfreturn rate>
<cfelse>
<cfreturn "error: #do_calculation.error#">
</cfif>
</cffunction>
<cffunction name="check_data">
<cfargument name="values" required="true">
<cfargument name="dates" required="true">
<cfset var is_ok = true>
<cfset var has_negative = false>
<cfset var has_positive = false>
<cfset var error = 0>
<cfset var return = structNew()>
<cfset var date_prev = "">
<cfset var date_curr = "">
<cfif arguments.values[1] gte 0>
<cfset is_ok = false>
<cfset error = -1>
</cfif>
<cfloop array="#arguments.values#" item="value">
<cfif value gt 0>
<cfset has_positive = true>
</cfif>
<cfif value lt 0>
<cfset has_negative = true>
</cfif>
</cfloop>
<cfif !has_negative or !has_positive>
<cfset is_ok = false>
<cfset error = -2>
</cfif>
<cfif arrayLen(arguments.values) neq arrayLen(arguments.dates)>
<cfset is_ok = false>
<cfset error = -3>
</cfif>
<cfloop from="2" to="#arrayLen(arguments.dates)#" index="d">
<cfset date_prev = arguments.dates[d-1]>
<cfset date_curr = arguments.dates[d]>
<cfif dateDiff("d", date_prev, date_curr) lte 0>
<cfset is_ok = false>
<cfset error = -4>
</cfif>
</cfloop>
<cfset return.is_ok = is_ok>
<cfset return.error = error>
<cfreturn return>
</cffunction>
<cffunction name="calculate_NPV">
<cfargument name="rate" required="false" default="1">
<cfargument name="values" required="true">
<cfargument name="dates" required="true">
<cfset var npv = arguments.values[1]>
<cfset var time_span = "">
<cfloop from="2" to="#arrayLen(arguments.values)#" index="i">
<cfset time_span = dateDiff('d', arguments.dates[1], arguments.dates[i]) / 365>
<cfset npv = npv + (arguments.values[i] / ((1 + arguments.rate) ^ time_span))>
</cfloop>
<cfreturn npv>
</cffunction>
#XIRR(值=[-5000500110500],日期=[“2015-07-06”,“2016-07-06”,“2017-07-06”])#