Mar*_*des 16 algorithm stochastic-process stock
我最近写了一些Javascript代码来生成随机的假股票数据,因为我想要显示一张图表,乍一看看起来像真实的股票数据 - 但我想出的只是非常点头.我只是想知道是否有一些资源可以解释如何"正确"地完成这项工作,即您可以获得具有与实际库存数据中相同模式的逼真数据?
Jim*_*hel 49
一个简单的算法是使用一个简单的波动率数字来限制股票在给定时期(例如,一天)内的变化程度.数字越大,越不稳定.因此,您每天都可以通过以下方式计算新价格:
rnd = Random_Float(); // generate number, 0 <= x < 1.0
change_percent = 2 * volatility * rnd;
if (change_percent > volatility)
change_percent -= (2 * volatility);
change_amount = old_price * change_percent;
new_price = old_price + change_amount;
Run Code Online (Sandbox Code Playgroud)
稳定的股票的波动率可能为2%.波动率为10%会显示出一些相当大的波动.
不完美,但它看起来很逼真.
样品

小智 7
# The following is an adaptation from a program shown at page 140 in
# "Stochastic Simulations and Applications in Finance",
# a book written by Huynh, Lai and Soumaré.
# That program was written in MatLab and this one was written in R by me.
# That program produced many price paths and this one produces one.
# The latter is also somewhat simpler and faster.
# Y is the time period in years, for instance 1 (year)
# NbSteps is the number of steps in the simulation,
# for instance 250 (trading days in a year).
# DeltaY is the resulting time step.
# The computations shown implement the exact solution
# to the stochastic differential equation for
# the geometric Brownian motion modelling stock prices,
# with mean mu and volatility sigma, thus generating a stochastic price path
# such as that exhibited by stock prices when price jumps are rare.
PricePath <- function(Y,NbSteps,mu,sigma,InitPrice) {
DeltaY <- Y/NbSteps; SqrtDeltaY <- sqrt(DeltaY)
DeltaW <- SqrtDeltaY * rnorm(NbSteps)
Increments <- (mu-sigma*sigma/2)*DeltaY + sigma*DeltaW
ExpIncr <- exp(Increments)
PricePath <- cumprod(c(InitPrice,ExpIncr))
return(PricePath)
}
Run Code Online (Sandbox Code Playgroud)

有几个答案给出了相当的教科书答案:使用几何布朗运动来模拟股票价格.但是有一个主要原因可以考虑这个错误.实际股票价格不像几何布朗运动(GBM).我会稍微解释一下.
GBM在教科书中用于模拟股票价格过程的原因是为了简单起见.它可以帮助您从理论上获得理论,并得出一些似乎"基本上"正确的基本结果.这并不意味着您应该认为股票价格"看起来像".这就像得出一个忽略摩擦的运动方程(这在理论上非常有用)然后认为这就是现实生活中的运动,例如每个人都像滑冰鞋一样在他们的鞋子上滑行.
GBM理论上最有用的特性之一是未来的变化与过去的变化无关.股票价格是真的吗?不.一点也不.串行关联到处都是.不仅如此,大幅下降通常伴随着波动性增加,而大幅上涨通常伴随着波动性下降.
我想我可能会被指责为挑剔,但这些风格化的事实通常为投资者和经济学家所知,所以我认为对于任何熟悉股市行为的人来说,GBM看起来并不现实.
计量经济学家已经提出了大量的股票价格模型.似乎在很多情况下工作的是条件均值的自回归模型和波动率的(G)Arch类型模型.对于波动率模型,具有胖尾分布的非对称GARCH(如Student's t)似乎对各种金融市场都是最好的.
我有一本书分形市场分析(最近刚刚摆脱它)谈论了股票价格的统计特性.对投资不是很有用,但它可能能够帮助你.
您需要使用所需统计属性为随机过程建模的东西.随机过程的两个例子是高斯白噪声和维纳过程(后者模拟布朗运动并且也是具有小步长的随机游动的极限).
如果我从分形市场分析书中记得,有一种断言,股票价格的对数具有类似于所谓的"1/f噪音"或"粉红噪音"的特征,所以你可以尝试寻找有关粉红噪音的文章在软件中生成.(然后取结果并将它们插入e ^ x)(编辑:oops,我记错了.看起来它更像是分数布朗运动)
(这是一篇很好的可读文章,讲述了分形随机过程研究背后的历史 - 以及尼罗河洪水如何与股市相关 - 遗憾的是它没有涉及技术数据,但也许有搜索术语像赫斯特指数,可以让你开始.)
如果您需要多个库存数据系列,问题就变得更加困难.(在这种情况下,股票之间存在某种相关性,这取决于各种常见因素,例如国民经济,行业类型等)我不确定你怎么做,但首先从一个随机过程开始.
我写了一个简短的javascript版本,灵感来自Peter P.的回复.我需要创建每周,每年和整体趋势,以便接受一系列参数并叠加这些参数以获得更复杂(假)的趋势.
function getRandomData(numPoints, center, min, max, cycles)
{
var result = [];
var phase = Math.random() * Math.PI;
var y = center;
function randomPlusMinus() { return (Math.random() * 2) - 1; }
$.each(cycles, function(i,thisCycle) {
thisCycle.phase = Math.random() * Math.PI;
thisCycle.increment = Math.PI / thisCycle.length;
});
for (var i = 0; i < numPoints; i++)
{
$.each(cycles, function(i,thisCycle) {
thisCycle.phase += thisCycle.increment * randomPlusMinus();
y += (Math.sin(thisCycle.phase) * (thisCycle.variance / thisCycle.length) * (randomPlusMinus() * thisCycle.noise)) + (thisCycle.trend / thisCycle.length);
});
if (min) y = Math.max(y,min);
if (max) y = Math.min(y,max);
result.push(y);
}
return result;
}
var data = getRandomData(365,80,20,100,
[{ length: 7, variance: 50, noise: 1, trend: 0},
{ length: 365, variance: 30, noise: 1, trend: 0},
{ length: 700, variance: 2, noise: 0, trend: 100}]);
Run Code Online (Sandbox Code Playgroud)
我在那里放了一个图表来显示结果:http://jsfiddle.net/z64Jr/3/