生成一个随机平稳趋势(随机游走)在JavaScript [英] Generating a Smooth Random Trend (Random Walk) in JavaScript

查看:599
本文介绍了生成一个随机平稳趋势(随机游走)在JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一个随机游走/随机潮流算法的JavaScript实现。我需要的东西,将与趋势(所以,只是简单的随机变化量是出)坚持,同时还保持了一些规定范围内。 我试着写一些东西从我的头顶通过选择基于加权平均数字(使用高斯函数的重量计算),并结束了一个稍微平滑的线(不够好)。然后,我花了一个不那么直接的方式,并试图在互联网上搜索,尽管我发现了几个轮廓,没有什么具体的不多,所以我的理解。

I am looking for a JavaScript implementation of a random walk/random trend algorithm. I need something that will stick with a trend (so, just plain random deltas are out) while still staying within some specified boundaries. I tried writing something off the top of my head by choosing numbers based on a weighted average (the weight was calculated using the Gaussian function) and ended up with a slightly smoother line (not good enough). I then took a less direct approach and tried searching on the internet, and although I found a few outlines, there was nothing specific enough that I understood.

事实证明(我不知道这本来),似乎已经有一个家庭使用高斯公式进行随机潮流算法。但是,尽管搜索的时间,我找不到比抽象的方程式是没有用的,我更多。我能找到的是这个博客<最好的/ A>,在那里他显示随机的数据,如我在寻找的照片。他列出公式,但我不知道这些其实都应该是说(对我来说,它似乎并不像它甚至有一个完整的解决方案)。

As it turns out (I was not aware of this originally), it seems there is already a family of algorithms that use the Gaussian equation to make a random trend. But, despite hours of searching, I couldn't find much more than abstract equations that were no use to me. The best that I could find was this blog where he shows a picture of random data like I'm looking for. He lists equations, but I have no idea what those actually are supposed to mean (to me, it doesn't seem like it's even a full solution).

什么算法都已经在那里(的JavaScript或C类实现preferably)来产生这样的数据?

What algorithms are already out there (JavaScript or C-like implementations preferably) to generate data like this?

推荐答案

下面是我想出了从阅读您链接的博客。据我所知,这是作者做了他的第一个图形。

Here is what I came up with from reading the blog that you linked. As far as I can tell, this is what the author did for his first graph.

CSS

#container {
    min-width: 310px;
    height: 400px;
    margin: 0 auto;
}

HTML

HTML

<div id="container"></div>

Javascript的

Javascript

箱穆勒变换 产生 高斯随机数

var boxMullerRandom = (function () {
    var phase = 0,
        RAND_MAX,
        array,
        random,
        x1, x2, w, z;

    if (crypto && typeof crypto.getRandomValues === 'function') {
        RAND_MAX = Math.pow(2, 32) - 1;
        array = new Uint32Array(1);
        random = function () {
            crypto.getRandomValues(array);

            return array[0] / RAND_MAX;
        };
    } else {
        random = Math.random;
    }

    return function () {
        if (!phase) {
            do {
                x1 = 2.0 * random() - 1.0;
                x2 = 2.0 * random() - 1.0;
                w = x1 * x1 + x2 * x2;
            } while (w >= 1.0);

            w = Math.sqrt((-2.0 * Math.log(w)) / w);
            z = x1 * w;
        } else {
            z = x2 * w;
        }

        phase ^= 1;

        return z;
    }
}());

随机游走 发电机

function randomWalk(steps, randFunc) {
    steps = steps >>> 0 || 100;
    if (typeof randFunc !== 'function') {
        randFunc = boxMullerRandom;
    }

    var points = [],
        value = 0,
        t;

    for (t = 0; t < steps; t += 1) {
        value += randFunc();
        points.push([t, value]);
    }

    return points;
}

辅助函数从随机游走点得到Y值

function getYValues(points) {
    return points.map(function (point) {
        return point[1];
    });
}

辅助功能,产生X地块为图

function generatePlots(howMany) {
    howMany = howMany >>> 0 || 10;
    var plots = [],
        index;

    for (index = 0; index < howMany; index += 1) {
        plots.push({
            name: 'plot' + index,
            data: getYValues(randomWalk())
        });
    }

    return plots;
}

图的结果,使用 的jQuery highcharts.js

$('#container').highcharts({
    title: {
        text: 'Random Walk',
        x: -20 //center
    },
    subtitle: {
        text: 'Random Walk',
        x: -20
    },
    xAxis: {
        type: 'linear'
    },
    yAxis: {
        title: {
            text: 'Value'
        },
        plotLines: [{
            value: 0,
            width: 1,
            color: '#808080'
        }]
    },
    tooltip: {
        valueSuffix: ' units'
    },
    legend: {
        layout: 'vertical',
        align: 'right',
        verticalAlign: 'middle',
        borderWidth: 0
    },
    series: generatePlots(10)
});

的jsfiddle

这篇关于生成一个随机平稳趋势(随机游走)在JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆