我有一个有趣的问题,我不确定是否最好的地方把它放在这里或堆栈数学,但由于我试图通过编程解决它(在R中,但任何编程语言都可以),也许StackOverflow是最好的地方。这个问题涉及到复合加密货币。问题如下:您在流动资金池中下注了"K“数量的令牌,以获得利息。流动资金池给你的令牌一个“年利率”( APR ),即年利率,APR不是复利。你可以在任何时候增加你的兴趣,但每次你这样做的时候,你都必须支付一小笔费用。
我最初试图用一些for循环来解决这个问题,如果假设用户每"D“天进行一次复合,估计最终的回报。当前示例使用蛋糕和BNB令牌。简化解决方案的几个假设。假设APR、bnb_value和cake_value是固定值(实际上并不是这样)。
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那么,如何正确计算最佳复合区间呢?
发布于 2021-03-25 13:30:13
我在GitHub页面https://github.com/amendez/cakecalc/issues/2#issuecomment-806364370上发表了评论。
但我想我现在明白你的意思了。我同意你的观点,就连我的亲身经历也证明了你的观点。cakeCalc为您提供最佳复合比例的方式存在一些问题。当计算最终结果时,它认为你的余额在每次复利后是固定的,这似乎是相当有问题的。因此,每次你混合你的蛋糕时,cakeCalc会给你一个不同的结果,即使你完全按照cakeCalc的建议操作,因为你的余额现在已经更新了
我从来没有试着写下方程来推导出最佳复合区间,但有一件事我可以肯定,最佳复合区间不会是固定的。例如,您可能希望找到类似以下答案的最佳结果:
20天合成,38天合成,53天合成,...
所以找到数字N作为复数并不能给出最好的答案。解决这个问题的一种方法是:考虑到你正在寻找一年内的最佳复利率。假设你想在这段时间内复数"m“次。现在,你有了"m“个变量和一个你想要最大化的表达式。我现在不确定,但也许你可以用一个简单的凸优化方法来解决它(只需要检查方程的凸性,因为变量之间的约束都是仿射的)。然后对不同数量的"m“尝试相同的方法,并找出其中最大的数量。这个解决方案是实用的,因为数字"m“是有界的(显然它是一个整数!),您可能希望找到与cakeCalc方法的结果如此接近的"m”。我想答案也不会有太多不同。因此,您可以更改"m“,例如少于10次,并找到最佳解决方案。
https://stackoverflow.com/questions/66457308
复制相似问题