2017年5月2日火曜日

学習環境

数学読本〈4〉数列の極限,順列/順列・組合せ/確率/関数の極限と微分法(松坂 和夫(著)、岩波書店)の第16章(確からしさをみる - 確率)、16.2(条件つき確率と確率の乗法定理)、確率と数列、問37.を取り組んでみる。


  1. q n =1( r n + p n ) q n+1 = 1 2 r n + 1 2 p n q n+1 = 1 2 ( r n + p n ) q n+1 = 1 2 ( 1 q n ) x= 1 2 ( 1x ) 2x=1x x= 1 3 q n+1 1 3 = 1 2 ( q n ) q n+1 1 3 = 1 2 ( q n 1 3 ) q 1 1 3 = 1 2 1 3 = 1 6 q n 1 3 = 1 6 ( 1 2 ) n1 q n = 1 6 ( 1 2 ) n1 + 1 3 = 1 6 ( 2+ ( 1 2 ) n1 ) r 1 = 1 2 r n = q n

コード(Emacs)

HTML5

<div id="graph0"></div>
<pre id="output0"></pre>
<label for="n0">コインを投げる回数 n =</label>
<input id="n0" type="number" min="1" step="1" value="10">
<label for="m0">試行回数: </label>
<input id="m0" type="number" min="1" step="1" value="1000">
<br>
<button id="run0">run</button>
<button id="clear0">clear</button>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.6/d3.min.js" integrity="sha256-5idA201uSwHAROtCops7codXJ0vja+6wbBrZdQ6ETQc=" crossorigin="anonymous"></script>
<script src="sample37.js"></script>    

JavaScript

let pre0 = document.querySelector('#output0'),
    input_n = document.querySelector('#n0'),
    input_m = document.querySelector('#m0'),
    btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0'),
    div0 = document.querySelector('#graph0'),
    width = 600,
    height = 600,
    padding = 50,
    p = (x) => pre0.textContent += x + '\n';

let range = (n) => {
    let result = [];
    for (let i = 0; i < n; i += 1) {
        result.push(i);
    }
    return result;
};

let A = 0,
    B = 1,
    C = 2;

let f = (n) => {
    let p = A;

    range(n).forEach(() => {
        let coin = Math.floor(Math.random() * 2);

        if (coin === 0) {
            if (p === A) {
                p = B;
            } else if (p === B) {
                p = C;
            } else {
                p = A;
            }
        } else {
            if (p === A) {
                p = C;
            } else if (p === B) {
                p = A;
            } else {
                p = B;
            }
        }
    });
    return p;
};

let output = () => {
    p('37.');
    let n = parseInt(input_n.value, 10),
        m = parseInt(input_m.value, 10),
        points = [];

    points = range(m).map((i) => {
        return [i + 1,
                range(i + 1)
                .map(() => f(n))
                .filter((x) => x === B)
                .length / (i + 1)]
    });
    
    let t = points[points.length - 1][1],
        result = 1 / 6 * (2 + Math.pow(-1 / 2, n - 1));
    p(t ===  result);
    p(t);
    p(result);
    p(Math.abs(t - result));

    let xscale = d3.scaleLinear()
        .domain([1, m])
        .range([padding, width - padding]);
    let yscale = d3.scaleLinear()
        .domain([0, 1])
        .range([height - padding, padding]);

    let xaxis = d3.axisBottom().scale(xscale);
    let yaxis = d3.axisLeft().scale(yscale);
    div0.innerHTML = '';
    let svg = d3.select('#graph0')
        .append('svg')
        .attr('width', width)
        .attr('height', height);

    svg.selectAll('circle')
        .data(points)
        .enter()
        .append('circle')
        .attr('cx', (d) => xscale(d[0]))
        .attr('cy', (d) => yscale(d[1]))
        .attr('r', 1)
        .attr('fill', 'green');

    svg.append('g')
        .attr('transform', `translate(0, ${height - padding})`)
        .call(xaxis);

    svg.append('g')
        .attr('transform', `translate(${padding}, 0)`)
        .call(yaxis);
};

input_n.onchange = output;
btn0.onclick = output;
btn1.onclick = () => pre0.textContent = '';

output();






0 コメント:

コメントを投稿