---
title: "Integrated random walk"
date: "2015-10-06"
---


The uncertainty in estimating the position using a noisy accelerometer increases with time. 

Let us model the acceleration as a uniformly random real number $a(t) = \operatorname{rand}(-1, 1)$. Suppose you have a random walk where $v(t) = \sum_{i=1}^t a(i)$. This is the velocity. Then, we would like to investigate $x(t) = \sum_{i=0}^t v(i)$, which is the position.

![Typical realization of 100 random accelerations.](a.svg)

To do this, we conduct a numerical experiment. We repeat the simulation $n$ times and then plot the RMS value of the $n$ trials at each time step.

By inspection, we see that $v(t)\approx c_1 t^{0.5}$ and $x(t) \approx c_2 t^{1.5}$ where $c_1$ and $c_2$ are constants.

<style>
svg {
    display: block;
    margin: 0 auto;
    max-width: 100%;
}
input {
    width: 100%;
    display: block;
}
</style>
<p>Number of trials: <span id="tt">100</span></p>

<input type="range" id="trials" min="1" max="100000" value="51171"/> 

<p>Mean: <span id="mm">0</span></p>
<input type="range" id="mean" min="0" max="600" value="0"/>

<ul>
<li><span style="color:#f30;font-weight:bold;">&mdash;</span> <img src="http://daniel.lawrence.lu/texcache/1e6e10711717e89fe92117cf7582a6decf3f68d5.svg" alt="v(t)" style="vertical-align: -0.638ex; margin: 1px 0ex; position: static;"/></li>
<li><span style="color:#a21;font-weight:bold;">&mdash;</span> <img src="http://daniel.lawrence.lu/texcache/59699d93fb13ce5260e813cc1f4eaaeb05c721fa.svg" alt="t^{0.5}" style="vertical-align: -0.128ex; margin: 1px 0ex; position: static;"/></li>
<li><span style="color:#58f;font-weight:bold;">&mdash;</span> <img src="http://daniel.lawrence.lu/texcache/62b10cd9e1396c7ea33fd211e67de2fb29019cfc.svg" alt="x(t)" style="vertical-align: -0.638ex; margin: 1px 0ex; position: static;"/></li>
<li><span style="color:#128;font-weight:bold;">&mdash;</span> <img src="http://daniel.lawrence.lu/texcache/ba07a5f60df31f1341beb8635e33f9169281859c.svg" alt="t^{1.5}" style="vertical-align: -0.128ex; margin: 1px 0ex; position: static;"/></li>
</ul>
<figure id="#fig2">
<div id="zxcv"></div>
<figcaption><a href="#fig2" class="fignum">FIGURE 2</a> Plot of acceleration, velocity and position, as well as the estimated fit, with respect to time.</figcaption>
</figure>
<script>
var rr = 0;
function myrand() {
    return (Math.random() - 0.5) * 2;
}
function noise( m) {
    var a = [];
    for(var i=0; i<m; i++) {
        a.push(myrand());
    }
    return a;
}
function run() {
    var n = parseInt(document.getElementById('trials').value);
    n = ~~(Math.pow(1.00009, n));
    document.getElementById('tt').innerHTML = n;

    var mea = parseInt(document.getElementById('mean').value)/600.0;
    document.getElementById('mm').innerHTML = ~~(mea*100) / 100;

    var m = 128;
    var v = [];
    var x = [];
    for (var i=0; i<n; i++) {
        var a = noise(m);
        v.push([]);
        x.push([]);
        var xx = 0, vv = 0;;
        v[i].push(0);
        x[i].push(0);
        for (var j=1; j<m; j++) {
            v[i].push(vv += a[j] + mea);
            x[i].push(xx += v[i][j]);
        }
    }

    var rmsv = [], rmsx = [];
    for (var j=0; j<m; j++) {
        var vv = 0, xx = 0;
        for (var i=0; i<n; i++) {
            vv += v[i][j]*v[i][j];
            xx += x[i][j]*x[i][j];
        }
        rmsv.push(Math.sqrt(vv/n));
        rmsx.push(Math.sqrt(xx/n));
    }
    draw(a, rmsv, rmsx);
    setTimeout(run, 100);
}
function draw(a, rmsv, rmsx) {
    var pv = 0.5, px = 1.5;
    var w = 600, h = 900;
    var asvg = '<polyline style="fill:none; stroke:#3c0; stroke-width:2" points="';
    var vsvg = '<polyline style="fill:none; stroke:#f30; stroke-width:2" points="';
    var xsvg = '<polyline style="fill:none; stroke:#58f; stroke-width:2" points="';
    var gvsvg = '<polyline style="fill:none; stroke:#a21; stroke-width:2" points="';
    var gxsvg = '<polyline style="fill:none; stroke:#128; stroke-width:2" points="';
    var mv = 0, mx = 0, mgv = 250/Math.pow(rmsv.length-1, pv), mgx = 250/Math.pow(rmsv.length-1, px);
    var ma = 0;
    for(var i=0; i<rmsv.length; i++) {
        if(Math.abs(a[i]) > ma) ma = Math.abs(a[i]);
        if(rmsv[i] > mv) mv = rmsv[i];
        if(rmsx[i] > mx) mx = rmsx[i];
    }

    var dx = 600/(rmsv.length-1);
    for(var i=0; i<rmsv.length; i++) {
        asvg += ''+(10+i*dx)+','+(150 - 140/ma*a[i])+' ';
        vsvg += ''+(10+i*dx)+','+(550 - 250/mv*rmsv[i])+' ';
        xsvg += ''+(10+i*dx)+','+(850 - 250/mx*rmsx[i])+' ';
        gvsvg += ''+(10+i*dx)+','+(550 - mgv*Math.pow(i, pv))+' ';
        gxsvg += ''+(10+i*dx)+','+(850 - mgx*Math.pow(i, px))+' ';
    }
    asvg += '" />';
    vsvg += '" />';
    xsvg += '" />';
    gvsvg += '" />';
    gxsvg += '" />';
    var svg = [ '<?xml version="1.0" encoding="UTF-8" standalone="no"?>',
                '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">',
                '<svg xmlns="http://www.w3.org/2000/svg"',
                '    viewBox="0 0 ' + (w+20) + ' ' + h + '"',
                '    width="' + (w+20) + '" height="' + h + '" preserveAspectRatio="xMidYMid meet">',
                '<line x1="10" y1="150" x2="'+(w+10)+'" y2="150" style="stroke-width:2; stroke:#888;"/>',
                '<line x1="10" y1="550" x2="'+(w+10)+'" y2="550" style="stroke-width:2; stroke:#888;"/>',
                '<line x1="10" y1="850" x2="'+(w+10)+'" y2="850" style="stroke-width:2; stroke:#888;"/>',
                gvsvg,
                gxsvg,
                asvg,
                vsvg,
                xsvg,
                '</svg>'].join('\n');
    document.getElementById('zxcv').innerHTML = svg;
}
run();
</script>

