【发布时间】:2021-03-03 12:41:26
【问题描述】:
我有一个有趣的问题,我不确定将其放在此处还是堆栈数学中的最佳位置,但由于我尝试以编程方式解决它(在 R 中,但任何编程语言都可以)也许 StackOverflow 是最好的地方。问题涉及复合加密货币。 问题如下: 您在流动性池中质押“K”数量的代币以获得利息。 流动性池为您的代币提供“年利率”(APR),即年利率,APR 不复利。 您可以随时复利,但每次都需要支付少量费用。
我最初尝试使用一些 for 循环来解决它,如果假设用户每“D”天复利一次,则估计最终回报。 当前示例使用 CAKE 和 BNB 令牌。一些简化解决方案的假设。假设 APR、bnb_value 和 cake_value 是 FIXED 值(它们在现实中并不存在)。
APR=1.349 ## the interest value showed in the pool, divided by 100
APR_day=APR/365.0 ## the daily APR
bnb_value=210 ## current value of the BNB token, in euro or dollar or any FIAT
fee_bnb=0.002*bnb_value ## current fee in euro or dollar or any FIAT
initial_cakes=10
cake_value=10
adf=data.frame(col1=NULL, col2=NULL, col3=NULL, col4=NULL)
## we generate a sequence containing fractional days up to the third day
## and then full days starting from the fourth day
comp_intervals=c(seq(0.1, 3, 0.1), 4:30)
for(j in comp_intervals){
acquired_int=0 ## acquired interest in "j" days
current_val=initial_cakes ## current value of the capital
all_fees=0 ## fees paid for the transactions
aseq=seq(j, 365, j) ## list of the compounding events in days
# apr_seq is APR for the "j" period. If "j" is 1 it's the daily APR,
# but for longer compounding intervals the APR for the "j" period
# become larger
apr_seq=APR_day*j
for(i in aseq){
acquired_int=current_val*apr_seq
current_val=current_val+acquired_int
all_fees=all_fees+fee_bnb
acquired_int=0
}
## we add the interest for the remaining days of the year, if any, to current_val
acquired_int=(365-max(aseq))*APR_day*current_val
current_val=current_val+acquired_int
final_gain=round(current_val*cake_value - all_fees, 2)
# msg=paste0("Final gain in Euro minus fees: ", final_gain)
# print(msg)
apy_i_day= round(final_gain/(initial_cakes*cake_value), 5)
# msg=paste0("apy compounding every ", j, " days is: ", apy_i_day)
# print(msg)
# cat("\n")
adf=rbind(adf, data.frame(fiat_value=final_gain,
APY_val=100*apy_i_day, compounding_days=j, cakes=current_val))
}
# finally we show, who, among the various compounding, had the hiest yield
adf[adf$APY_val==max(adf$APY_val), ]
问题在于,您刚刚看到的代码并没有真正告诉您什么是复利的最佳时期。如果用户每“D”天复合一次,它会告诉更多的收益。它接近真正的解决方案,但事实并非如此! 您可以直观地理解延长时间是错误的。您从少量资本开始,因此为了获得良好的回报,您“很少”因为费用而复利,但是时间越长,您的资本增长就越多。您的资本增长越频繁,您就应该复利。
然后我尝试了不同的方法。
给出“年收益率”的一般公式如下:
APY = (1 + APR/N)^N -1
如果您还考虑费用和您拥有的初始资本:
Final_capital=Initial_capital*APY - single_fee*N
其中 APR 是百分比,N 是复合事件的数量(在这个公式中,它们在时间上是均匀分布的)。
通过“dN”对 Final_capital 进行微分并找到等式的零点,您可以获得最佳数量的复合事件。如果您将 365 除以复合事件的最佳数量,您应该在复合代币的天数后获得。 我从微分公式获得的结果与第一个解决方案不同,我不知道为什么。我也认为,但我不确定,后一种解决方案与前一种解决方案有相同的限制。
library(utils)
### interest APY formula minus fees
final_v=function(x, APR, fee_bnb, initial_value){
return( initial_value*( (1+APR/x)^x - 1 ) -fee_bnb*x )
}
## differential respect to X of the interest APY formula minus the fees
a_diff_comp=function(x, APR, fee_bnb, initial_value){
return( (initial_value*( (APR/x + 1)^x )*( log(APR/x + 1) - APR/( ((1/x)+1)*x ) ) ) - fee_bnb )
}
x=3:40
y=sapply(x, a_diff_comp, APR=APR, fee_bnb=fee_bnb, initial_value=(initial_cakes*cake_value))
plot(x,y)
y2=sapply(x, final_v, APR=APR, fee_bnb=fee_bnb, initial_value=(initial_cakes*cake_value))
plot(x,y2)
xmin <- uniroot(a_diff_comp, c(1, 100), tol = 0.000001, APR=APR, fee_bnb=fee_bnb, initial_value=(initial_cakes*cake_value))
xmin$root
那么,如何正确计算最佳复利区间?
【问题讨论】:
标签: r math optimization logic cryptocurrency