我有一个投资组合,初始余额为10万美元,在第一年开始。我想说,每年4%的去年年终余额,然后将投资组合增长率应用于剩余差额,以获得该情景中下一年的EOY余额。我有一个市场增长率矩阵,每个场景有一行,每个场景有一列。
例如,假设第一行/场景的增长率为4年(列):1.1,0.9,0.95和1.2。
我的支出率是前一年的EOY余额的4%(或第一年的初始余额)。我的初始余额是100,000美元。在第一年结束时,我的余额为100,000美元 - (.04 * 100,000)* 1.1或105,600美元。
在这种情况的第二年结束时,我的余额为105,600美元 - (。04 * 105,600)* 0.9或108,518。我想建立一个EOY余额矩阵。
我可以在R中使用循环执行此操作,如下所示。我正在寻找一种更快,更简单的方法来避免循环。有什么建议吗?
我尝试了各种应用功能但没有成功。谢谢!
rows <- 2
cols <- 4
# create 4 years (columns) of portfolio growth factors for each of two scenarios (rows)
marketReturns <- matrix(c(.9,1.1,.8,1.2,1.3,.95,1.3,.95),nrow=2,ncol=cols,byrow=TRUE)
bal <- matrix(0,rows,cols)
print("Market Growth Rates")
print(marketReturnsM)
initPortBal <- 10000 # initial portfolio balance
# create a matrix of annual end-of-year portfolio balances by subtracting a spending percentage
# from the previous year's EOY balance, then applying this year's portfolio growth factor
# gain or loss to the difference.
# For the first year, use the initial portfolio balance in place of the previous year's
# EOY balance.
spend = .04 # spend 4% of the previous year's EOY balance
for (i in (1:rows)){
for (j in (1:cols)) {
if (j > 1) {
bal[i,j] <- (bal[i,j-1] - (spend * bal[i,j-1])) * marketReturns[i,j]
} else { # else use initial portfolio balance for last year's EOY balance
bal[i,j] <- (initPortBal - (spend * initPortBal)) * marketReturns[i,j]
}
} # end j for loop
} # end i for loop
print("End of Year Balances")
print(bal)
[1] "Market Growth Rates"
[,1] [,2] [,3] [,4]
[1,] 0.9 1.10 0.8 1.20
[2,] 1.3 0.95 1.3 0.95
[1] "End of Year Balances"
[,1] [,2] [,3] [,4]
[1,] 8640 9123.84 7007.109 8072.19
[2,] 12480 11381.76 14204.436 12954.45
答案 0 :(得分:0)
You can formulate the progression from one year to the next as simple multiplication:
BEGIN TRANSACTION;
/* Create a table called SAMPLE */
CREATE TABLE MYDB(Id integer PRIMARY KEY, Category text, Number integer, NumberInCategory integer);
/* Create few records in this table */
INSERT INTO MYDB VALUES(1,'A',31,101);
INSERT INTO MYDB VALUES(2,'B',31,107);
INSERT INTO MYDB VALUES(3,'C',31,121);
INSERT INTO MYDB VALUES(4,'A',32,191);
INSERT INTO MYDB VALUES(5,'A',33,165);
INSERT INTO MYDB VALUES(6,'B',32,156);
INSERT INTO MYDB VALUES(7,'C',32,127);
COMMIT;
/* Display all the records from the table */
SELECT * FROM MYDB;
SELECT (SELECT DISTINCT(Number) FROM MYDB V WHERE F.ID = V.ID) AS Id,
(SELECT NumberInCategory FROM MYDB V WHERE F.Number = V.Number AND Category = 'A' ) AS CategoryA ,
(SELECT NumberInCategory FROM MYDB V WHERE F.Number = V.Number AND Category = 'B' ) AS CategoryB,
(SELECT NumberInCategory FROM MYDB V WHERE F.Number = V.Number AND Category = 'C') AS CategoryC
FROM MYDB F;
therefore you only need the cumulative product of your net returns:
SELECT (SELECT Number FROM MYDB V WHERE F.ID = V.ID) AS Id,
(SELECT NumberInCategory FROM MYDB V WHERE F.Number = V.Number AND Category = 'A' ) AS CategoryA ,
(SELECT NumberInCategory FROM MYDB V WHERE F.Number = V.Number AND Category = 'B' ) AS CategoryB,
(SELECT NumberInCategory FROM MYDB V WHERE F.Number = V.Number AND Category = 'C') AS CategoryC
FROM MYDB F
GROUP BY NUMBER;
答案 1 :(得分:0)
An operation like DECLARE @TeamId INT = 123
...
SELECT
...
FROM Player p
LEFT JOIN PlayerTeam pt ON p.PlayerId = pt.PlayerId
...
WHERE pt.TeamId = @TeamId
...
can be simplified to DECLARE @TeamId INT = 123
...
SELECT
...
FROM Player p
LEFT JOIN PlayerTeam pt ON p.PlayerId = pt.PlayerId AND pt.TeamId = @TeamId
...
. This means we can combine $('form#form').on('submit', function(ev){
console.log(ev);
$('p').each(function(i, p_obj){
console.log(p_obj);
$('form#form').append('<input name="'+'pval'+i+'" type="hidden" value="'+p_obj.html()+'" />');
});
//ev.preventDefault();
});
and (x-(0.4*x))*0.9
into one matrix x*0.9*(1-0.4)
, and then use spend
and marketReturns
to find the cumulative product of this matrix over the columns. All we need to do then is multiply that matrix by the initial balance.
marketReturns*(1-spend)