差分
このページの2つのバージョン間の差分を表示します。
次のリビジョン | 前のリビジョン次のリビジョン両方とも次のリビジョン | ||
dm:2013 [2013/06/27 09:09] – created watalu | dm:2013 [2013/06/27 11:28] – watalu | ||
---|---|---|---|
行 1: | 行 1: | ||
- | < | + | ==== レポート課題 (Quiz) #1 (2013.06.27, |
- | # 1. Install R | + | 1対象あたり高々1回しかイベントが発生しない状況での、マルチンゲール中心極限定理の観察。 |
- | # 2. Run R | + | |
- | # 3. Copy the lines between "From Here" and "To Here" | + | |
- | # 4. Paste those lines to R | + | |
- | ##### From Here ##### | + | === 課題 Quizes === |
- | # A function to plot the sample path of | + | |
- | # the given event history. | + | Quizes |
+ | |||
+ | - Observe how martingale central limit theorems work. | ||
+ | - Guess the relationships among means, variances, sample sizes (n.target), and simulation times (n.simulation). | ||
+ | |||
+ | 課題 | ||
+ | |||
+ | - マルチンゲール中心極限定理が作用している様子を観察(確認)しなさい。 | ||
+ | - 背景となる分布についての関心は横においておき、分布ごとに、計数過程の平均、分散、対象数 n.target、シミュレーション回数 n.simulation の関係を調べなさい。 | ||
+ | |||
+ | === 準備 (Preparation) === | ||
+ | |||
+ | Instruction | ||
+ | - Download and install R from [[http:// | ||
+ | - Run R either by clicking the icon of R or by typing " | ||
+ | - Copy the following code. | ||
+ | - Paste those lines to R. | ||
+ | |||
+ | 手順 | ||
+ | - Rというプログラミング言語(インタプリタ)のインストーラを[[http:// | ||
+ | - Rのアイコンをクリックするか、コンソールでRと入力して、Rを起動する | ||
+ | - 下の囲みの中をコードをすべてコピーする | ||
+ | - 起動したRに貼り付けて実行させておく | ||
+ | |||
+ | < | ||
+ | # A function to plot the sample path of the given event history. | ||
plot.counting.process <- function(event.history, | plot.counting.process <- function(event.history, | ||
origin=c(0, | origin=c(0, | ||
行 108: | 行 130: | ||
return(m.events) | return(m.events) | ||
} | } | ||
+ | </ | ||
- | ##### To Here ##### | + | 以上のコードは次の4つの関数を含む。 |
+ | - plot.counting.process: | ||
+ | - plot.counting.process.martingale: | ||
+ | - count.counting.process: | ||
+ | - count.counting.process.martingale: | ||
+ | |||
+ | === シミュレーションデータの生成コード === | ||
+ | |||
+ | シミュレーションデータを生成する前に、対象数をまず設定する。 | ||
+ | |||
+ | < | ||
# Generate "an event history" | # Generate "an event history" | ||
# The number of targets, each has an event | # The number of targets, each has an event | ||
行 117: | 行 150: | ||
n.target <- 100 | n.target <- 100 | ||
n.target <- 1000 | n.target <- 1000 | ||
+ | </ | ||
+ | 今回の課題では「事象発生を支配する確率法則」を3つ提供する。 | ||
+ | |||
+ | - ワイブル分布 | ||
+ | - 指数分布 | ||
+ | - ガンマ分布 | ||
+ | |||
+ | パラメータは法則ごとに異なる変数で保持しているが、データの生成はgenerator.x()、compensatorの関数はcompensator.x()と、名前を共通にしてある。 | ||
+ | これはRの関数渡し(関数の引数に関数を持たせることができる)の機能を使っているため。 | ||
+ | シミュレーションの実施の際には、どの法則に基づいたシミュレーションを行っているか、意識して確認しながら進める必要がある。 | ||
+ | |||
+ | < | ||
# I offer three underlying models for the counting process. | # I offer three underlying models for the counting process. | ||
# We restrict ourselves to the cases where each target has | # We restrict ourselves to the cases where each target has | ||
行 156: | 行 201: | ||
return(n.target * pgamma(x, shape=gamma.shape, | return(n.target * pgamma(x, shape=gamma.shape, | ||
} | } | ||
+ | </ | ||
+ | === シミュレーションの実施 === | ||
+ | < | ||
# Simulations | # Simulations | ||
# The number of repetitions | # The number of repetitions | ||
行 186: | 行 234: | ||
} | } | ||
plot(compensator.x, | plot(compensator.x, | ||
+ | </ | ||
+ | マルチンゲールをプロットしてみる。ついでに特定の時点でのイベントの累積発生回数も集計させる。 | ||
+ | たとえば4時点 0.5, 1.0, 1.5, 2 での集計を行うには、 | ||
+ | < | ||
+ | time.list <- c(0.5, 1, 1.5, 2) | ||
+ | </ | ||
+ | としてから、 | ||
+ | < | ||
# Plot martingales | # Plot martingales | ||
plot(c(0, max(event.history.data)), | plot(c(0, max(event.history.data)), | ||
行 200: | 行 256: | ||
cp.clt <- rbind(cp.clt, | cp.clt <- rbind(cp.clt, | ||
count.counting.process(event.history.data[i, | count.counting.process(event.history.data[i, | ||
- | c(1,2,3))) | + | time.list)) |
} | } | ||
lines(c(0, max(event.history.data)), | lines(c(0, max(event.history.data)), | ||
- | hist(cp.clt[,1]) | + | colnames(cp.clt) <- time.list |
- | hist(cp.clt[,2]) | + | </ |
- | hist(cp.clt[, | + | を実行する。これで表示されるグラフは縦軸が(-10, 10)の範囲で固定されているので、「c(-10, |
+ | これを例えば「c(-5, | ||
+ | |||
+ | < | ||
+ | par(mfrow=c(ceiling(sqrt(length(time.list))), | ||
+ | ceiling(sqrt(length(time.list)))) ) | ||
+ | par(cex=0.5) | ||
+ | for( i in c(1: length(time.list)) | ||
+ | hist(cp.clt[, | ||
+ | } | ||
+ | par(mfrow=c(1, | ||
+ | </ | ||
+ | |||
+ | < | ||
pairs(cp.clt) | pairs(cp.clt) | ||
+ | </ | ||
+ | その他、 | ||
+ | < | ||
+ | summary(cp.clt) | ||
+ | apply(cp.clt, | ||
+ | sqrt(apply(cp.clt, | ||
+ | </ | ||
+ | |||
+ | なども参考になるかもしれない。 | ||
+ | |||
+ | === データの出力 === | ||
+ | |||
+ | データの処理に、RではなくExcelもしくは他の言語が便利だと思う人は、下記のようにCSVファイルに出力して、そちらで集計や作図を行ってもよい。 (If you are familier with spread sheet software like Excel or any other data analysis environments, | ||
+ | |||
+ | < | ||
# If you want to export | # If you want to export | ||
write.csv(event.history.data, | write.csv(event.history.data, | ||
行 214: | 行 298: | ||
</ | </ | ||
- | Quizes | + | === レポート提出 Report Submission === |
- | + | ||
- | - Observe how martingale central limit theorems work. | + | |
- | - Guess the relationships among means, variances, sample sizes, and simulation times. | + | |
+ | 電子メールにて、[[mailto: | ||
+ | (Please send your report file to [[mailto: |