如何模拟R中的每日股票收益率
我需要模拟股票的每日收益。我得到的如何模拟R中的每日股票收益率,r,R,我需要模拟股票的每日收益。我得到的r=(P(t+1)-P(t))/P(t)(正态分布)平均值为µ=1%,标准差为σ=5%。P(t)是第t天结束时的股价。模拟100000个这样的每日回报实例 因为我是一个新的R用户,所以如何为这个示例设置t。我假设P应设置为: P <- rnorm(100000, .01, .05) r=(P(t+1)-P(t))/P(t) P你弄错了:从你写的内容来看,mean和sd适用于返回,而不是价格。此外,我假设平均值设定为每年一次(1%从一天到另一天的回报率非常
r=(P(t+1)-P(t))/P(t)
(正态分布)平均值为µ=1%,标准差为σ=5%。P(t)是第t天结束时的股价。模拟100000个这样的每日回报实例
因为我是一个新的R用户,所以如何为这个示例设置t。我假设P应设置为:
P <- rnorm(100000, .01, .05)
r=(P(t+1)-P(t))/P(t)
P你弄错了:从你写的内容来看,
mean
和sd
适用于返回
,而不是价格
。此外,我假设平均值设定为每年一次(1%
从一天到另一天的回报率非常大!),并且t
在252
天的范围内移动
根据这些假设,您可以在R
中获得一系列日收益率,具体如下:
r = rnorm(100000, .01/252, .005)
假设您提到的模型,您可以得到价格p的系列(包含100001个元素,我将取p[1]=100
-如果需要,使用您自己的值进行更改):
其中一个经典模型(并不意味着该模型是现实的)假设观察到的日志返回值
遵循正态分布
希望这能有所帮助。你弄错了:从你写的内容来看,平均值和sd
适用于退货
,而不是价格
。此外,我假设平均值设定为每年一次(1%
从一天到另一天的回报率非常大!),并且t
在252
天的范围内移动
根据这些假设,您可以在R
中获得一系列日收益率,具体如下:
r = rnorm(100000, .01/252, .005)
假设您提到的模型,您可以得到价格p的系列(包含100001个元素,我将取p[1]=100
-如果需要,使用您自己的值进行更改):
其中一个经典模型(并不意味着该模型是现实的)假设观察到的日志返回值
遵循正态分布
希望这有帮助
P
P我知道你已经有了答案,而且上校可能比我有更多的领域知识(假设这是商业或金融作业)。我的做法有点不同,我将发布一份评论成绩单。他的方法使用了
我知道你已经有了答案,而且上校可能比我有更多的领域知识(假设这是商业或金融作业)。我的方法有点不同,我将发布一份评论成绩单。他的方法使用模拟每日股票的日志回报,使用以下方法:
考虑使用256天的每日股票回报数据
将原始数据加载到R中
创建另一个data.frame以模拟日志返回
代码:
logr <- data.frame(Date=gati$Date[1:255], Shareprice=gati$Adj.Close[1:255], LogReturn=log(gati$Adj.Close[1:251]/gati$Adj.Close[2:256]))
logr要模拟每日股票的对数回报,请使用以下方法:
考虑使用256天的每日股票回报数据
将原始数据加载到R中
创建另一个data.frame以模拟日志返回
代码:
logr <- data.frame(Date=gati$Date[1:255], Shareprice=gati$Adj.Close[1:255], LogReturn=log(gati$Adj.Close[1:251]/gati$Adj.Close[2:256]))
logr股票收益率对于简单收益率(“R”)不是正态分布的,因为每个复合期的下限为-1。但是,日志返回(“r”)通常为。下面的内容是根据上面@42的帖子改编的。从#Rstats中的Log Mean(“预期收益”)和Log Stdev(“风险”)进行模拟似乎没有任何解决方案,因此我在这里为那些寻找“使用Log预期收益和Log标准偏差的蒙特卡罗模拟”(Monte Carlo Simulation using Log Expected Return and Log Standard Deviation)的人提供了这些解决方案,它们是正态分布的,并且在-1处没有下限。注意:从这个单一的例子来看,模拟一个投资组合需要循环数千次——也就是说,像下面这样叠加10万个图,并平均一个切片,以计算所选远期月份投资组合的平均预期回报。以下内容应为这样做提供良好的基础
startPrice = 100
forwardPeriods = 12*10 # 10 years * 12 months with Month-over-Month E[r]
factor = exp(rnorm(forwardPeriods, .04, .10)) # Monthly Expected Ln Return = .04 and Expected Monthly Risk = .1
temp = startPrice
P = c(startPrice, sapply(1:forwardPeriods, function(u){p = factor[u]*temp; temp <<- p; p}))
plot(P, type = "b", xlab = "Forward End of Month Prices", ylab = "Expected Price from Log E[r]", ylim = c(0,max(P)))
股票收益率不是简单收益率(“R”)的正态分布,因为每个复合期的下限为-1。但是,日志返回(“r”)通常为。下面的内容是根据上面@42的帖子改编的。从#Rstats中的Log Mean(“预期收益”)和Log Stdev(“风险”)进行模拟似乎没有任何解决方案,因此我在这里为那些寻找“使用Log预期收益和Log标准偏差的蒙特卡罗模拟”(Monte Carlo Simulation using Log Expected Return and Log Standard Deviation)的人提供了这些解决方案,它们是正态分布的,并且在-1处没有下限。注意:从这个单一的例子来看,模拟一个投资组合需要循环数千次——也就是说,像下面这样叠加10万个图,并平均一个切片,以计算所选远期月份投资组合的平均预期回报。以下内容应为这样做提供良好的基础
startPrice = 100
forwardPeriods = 12*10 # 10 years * 12 months with Month-over-Month E[r]
factor = exp(rnorm(forwardPeriods, .04, .10)) # Monthly Expected Ln Return = .04 and Expected Monthly Risk = .1
temp = startPrice
P = c(startPrice, sapply(1:forwardPeriods, function(u){p = factor[u]*temp; temp <<- p; p}))
plot(P, type = "b", xlab = "Forward End of Month Prices", ylab = "Expected Price from Log E[r]", ylim = c(0,max(P)))
横轴是天数,纵轴是价格
n_prices <- 1000
volatility <- 0.2
amplitude <- 10
chng <- amplitude * rnorm(n_prices, 0, volatility)
prices <- cumsum(chng)
plot(prices, type='l')
n_prices横轴是天数,纵轴是价格
n_prices <- 1000
volatility <- 0.2
amplitude <- 10
chng <- amplitude * rnorm(n_prices, 0, volatility)
prices <- cumsum(chng)
plot(prices, type='l')
n_pricesr=(P(t+1)-P(t))/P(t)
应该是回报的公式(这只是比例变化)。。您需要P1和P2(表示t和t+1)。然后根据这两个向量计算r。你的第二行根本不是R格式。如果t+1和t不是二进制的,我怎么设置呢?下面是完整的答案。t是时间,它不是二进制的。你有两个时间段,你在计算这两个时间段的比例变化。r=(P(t+1)-P(t))/P(t)
应该是回报的公式(这只是比例变化)。。您需要P1和P2(表示t和t+1)。然后根据这两个向量计算r。你的第二行根本不是R格式。如果t+1和t不是二进制的,我怎么设置呢?下面是完整的答案。t是时间,它不是二进制的。您有两个时间段,您正在计算这两个时间段内的比例变化。谢谢。还有一个问题
r = rnorm(100000, .01/252, .005)
factor = 1 + r
temp = 100
P = c(100, sapply(1:300, function(u){
p = factor[u]*temp
temp<<-p
p
}))
> system.time( {r <- rnorm(10000, .01/250, .05)
+ P <- P_1*cumprod(1+r)
+ })
user system elapsed
0.001 0.000 0.002
> system.time({r = rnorm(10000, .01/252, .05)
+ factor = 1 + r
+ temp = 100
+ P = c(100, sapply(1:300, function(u){
+ p = factor[u]*temp
+ temp<<-p
+ p
+ }))})
user system elapsed
0.079 0.004 0.101
logr <- data.frame(Date=gati$Date[1:255], Shareprice=gati$Adj.Close[1:255], LogReturn=log(gati$Adj.Close[1:251]/gati$Adj.Close[2:256]))
startPrice = 100
forwardPeriods = 12*10 # 10 years * 12 months with Month-over-Month E[r]
factor = exp(rnorm(forwardPeriods, .04, .10)) # Monthly Expected Ln Return = .04 and Expected Monthly Risk = .1
temp = startPrice
P = c(startPrice, sapply(1:forwardPeriods, function(u){p = factor[u]*temp; temp <<- p; p}))
plot(P, type = "b", xlab = "Forward End of Month Prices", ylab = "Expected Price from Log E[r]", ylim = c(0,max(P)))
n <- length(P)
logRet <- log(P[-1]/P[-n])
# Notice, with many samples this nearly matches our initial log E[r] and stdev(r)
mean(logRet)
# [1] 0.04540838
sqrt(var(logRet))
# [1] 0.1055676
min(P)
# [1] 100
max(P)
# [1] 23252.67
n_prices <- 1000
volatility <- 0.2
amplitude <- 10
chng <- amplitude * rnorm(n_prices, 0, volatility)
prices <- cumsum(chng)
plot(prices, type='l')