差分
このページの2つのバージョン間の差分を表示します。
| 次のリビジョン | 前のリビジョン | ||
| r:markovchain [2018/12/11 23:14] – created watalu | r:markovchain [2018/12/17 11:47] (現在) – watalu | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| - | ==== マルコフ性とマルコフ過程 | + | ==== markovchain |
| - | 時間をtで表し、時間の経過とともに変化する状態X[t]を考える。 | + | markovchainパッケージは、離散状態離散時間マルコフ過程(通称は離散マルコフ過程、あるいはマルコフ連鎖)をRで扱うのに便利な機能を提供する。以下の説明は、[[https:// |
| - | 時間の間隔は等間隔とし、整数で表す。 | + | |
| - | X[0], X[1], ..., X[t-1],X[t],X[t+1],...は時間の添え字を持つ確率変数の列である。 | + | * [[https:// |
| - | この確率変数の列は(離散時間)確率過程と呼ばれる。 | + | * [[https:// |
| - | 任意の時点tより昔の確率変数の実現値の列x[0], x[1], ..., x[t-1]を時点tより前の履歴(history)といい、H[t-]と記す。 | + | * [[https:// |
| + | | ||
| - | 任意の時点tの状態X[t]の、それ以前のすべての時点の履歴を与えた条件付き確率分布Pr[X[t]=i|H[t-]] = Pr[X[t]=i|X[t-1]=x[t-1], | + | このパッケージはR |
| - | マルコフ性を持つ確率過程をマルコフ過程という。 | + | === マルコフ解析 === |
| - | 以上ではX[t]が離散確率変数の場合を紹介したが、連続確率変数でも条件付き確率密度関数を用いて、同様の議論が展開できる。 | + | マルコフ過程に基づいて現象の性質を明らかにしたり、未来の時点のおける状態の確率分布を推定することなどを、マルコフ解析という。 |
| - | それを連続状態離散時間マルコフ過程という。 | + | |
| - | 現在の時点をt、少し先の未来の時点をt+1と置くと、 | + | === マルコフ解析に必要なもの === |
| - | Pr[X[t+1]=i|H[t]] = Pr[X[t+1]=i|X[t]=x[t]] | + | |
| - | となり、未来を予測するのに、過去のすべてのデータを参照する必要はなく、現在の状態x[t]が必要なすべての情報である、ということにマルコフ性は等しい。 | + | マルコフ解析に必要なものは、現在の状態もしくは初期の状態と、対象の状態遷移行列である。 |
| + | |||
| + | * 状態 | ||
| + | * 状態遷移行列 | ||
| + | |||
| + | 天気の移り変わりを例に説明を進める。 | ||
| + | |||
| + | < | ||
| + | weatherStates <- c(" | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | byRow <- TRUE | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | | ||
| + | 0.3, 0.4, 0.3, | ||
| + | 0.2, 0.45, 0.35), byrow = byRow, nrow = 3, | ||
| + | | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | mcWeather <- new(" | ||
| + | | ||
| + | </ | ||
| + | |||
| + | |||
| + | === markovchainのメソッド === | ||
| + | |||
| + | |Method|Purpose| | ||
| + | |*|Direct multiplication for transition matrices.| | ||
| + | |[|Direct access to the elements of the transition matrix.| | ||
| + | |==|Equality operator between two transition matrices.| | ||
| + | |as|Operator to convert markovchain objects into data.frame and table object.| | ||
| + | |dim|Dimension of the transition matrix.| | ||
| + | |names|Equal to states.| | ||
| + | |names< | ||
| + | |name|Get the name of markovchain object.| | ||
| + | |name< | ||
| + | |plot|plot method for markovchain objects.| | ||
| + | |print|print method for markovchain objects.| | ||
| + | |show|show method for markovchain objects.| | ||
| + | |sort|sort method for markovchain objects.| | ||
| + | |states|Name of the transition states.| | ||
| + | |t|Transposition operator (which switches byrow slot value and modifies the transition matrix coherently).| | ||
| + | |||
| + | === markovchainの使い方の基本 === | ||
| + | |||
| + | 上で定義したマルコフ連鎖を確認しておく。 | ||
| + | |||
| + | < | ||
| + | weatherStates | ||
| + | weatherMatrix | ||
| + | mcWeather | ||
| + | </ | ||
| + | |||
| + | さて、初期状態は曇りcloudy。 | ||
| + | |||
| + | < | ||
| + | initialState = c(0, 1, 0) | ||
| + | </ | ||
| + | |||
| + | 二日後の天気の確率分布を求めてみる。 | ||
| + | |||
| + | < | ||
| + | after2Days <- initialState * (mcWeather * mcWeather) | ||
| + | </ | ||
| + | |||
| + | 求めた確率分布を確認する。 | ||
| + | |||
| + | < | ||
| + | after2Days | ||
| + | </ | ||
| + | |||
| + | 7日後の天気の条件付き確率分布を推定する。 | ||
| + | |||
| + | < | ||
| + | after7Days <- initialState * (mcWeather ^ 7) | ||
| + | </ | ||
| + | |||
| + | 求めた確率分布を確認する。 | ||
| + | |||
| + | < | ||
| + | after7Days | ||
| + | </ | ||
| + | |||
| + | こんなに長い桁は必要ないので、四捨五入して小数点以下第3位まで表示してみる。 | ||
| + | |||
| + | < | ||
| + | round(after7Days, | ||
| + | </ | ||
| + | |||
| + | 状態を列ベクトルで表すなら、すべてを転地すればいい。 | ||
| + | |||
| + | < | ||
| + | initialState <- c(0, 1, 0) | ||
| + | after2Days <- (t(mcWeather) * t(mcWeather)) * initialState | ||
| + | after7Days <- (t(mcWeather) ^ 7) * initialState | ||
| + | after2Days | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | fvals< | ||
| + | out< | ||
| + | names(initialstate)< | ||
| + | for (i in 0:n) | ||
| + | { | ||
| + | iteration< | ||
| + | out< | ||
| + | } | ||
| + | out< | ||
| + | out< | ||
| + | return(out) | ||
| + | } | ||
| + | fvals(mchain=mcWeather, | ||
| + | </ | ||
| + | |||
| + | 関数statesはマルコフ連鎖のオブジェクトmcWeatherの中の状態の一覧を表示させる。 | ||
| + | < | ||
| + | states(mcWeather) | ||
| + | </ | ||
| + | |||
| + | 関数namesもstatesと同じ動作をする。 | ||
| + | < | ||
| + | names(mcWeather) | ||
| + | </ | ||
| + | |||
| + | 関数dimは状態数を返す。 | ||
| + | < | ||
| + | dim(mcWeather) | ||
| + | </ | ||
| + | |||
| + | 関数nameはマルコフ連鎖の名前を返す。これはnewで作成するときにつけている。 | ||
| + | < | ||
| + | name(mcWeather) | ||
| + | </ | ||
| + | |||
| + | |||
| + | < | ||
| + | markovchain::: | ||
| + | </ | ||
| + | |||
| + | |||
| + | < | ||
| + | transitionProbability(mcWeather, | ||
| + | </ | ||
| + | |||
| + | |||
| + | < | ||
| + | mcWeather[2,3] | ||
| + | </ | ||
| + | |||
| + | |||
| + | < | ||
| + | print(mcWeather) | ||
| + | </ | ||
| + | |||
| + | |||
| + | < | ||
| + | show(mcWeather) | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | plot(mcWeather, | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | mcDf <- as(mcWeather, | ||
| + | mcNew <- as(mcDf, " | ||
| + | mcDf | ||
| + | </ | ||
| + | |||
| + | |||
| + | < | ||
| + | mcIgraph <- as(mcWeather, | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | require(msm) | ||
| + | Q <- rbind ( c(0, 0.25, 0, 0.25), | ||
| + | c(0.166, 0, 0.166, 0.166), | ||
| + | c(0, 0.25, 0, 0.25), | ||
| + | c(0, 0, 0, 0) ) | ||
| + | cavmsm <- msm(state ~ years, subject = PTNUM, data = cav, qmatrix = Q, death = 4) | ||
| + | msmMc <- as(cavmsm, " | ||
| + | msmMc | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | myMatr< | ||
| + | myMc< | ||
| + | myMc | ||
| + | </ | ||
| + | |||
| + | === マルコフ連鎖の確率推論 === | ||
| + | |||
| + | |Method|Returns| | ||
| + | |absorbingStates|the absorbing states of the transition matrix, if any.| | ||
| + | |steadyStates|the vector(s) of steady state(s) in matrix form.| | ||
| + | |communicatingClasses|list of communicating classes. sj , given actual state si.| | ||
| + | |canonicForm|the transition matrix into canonic form.| | ||
| + | |is.accessible|verification if a state j is reachable from state i.| | ||
| + | |is.irreducible|verification whether a DTMC is irreducible.| | ||
| + | |period|the period of an irreducible DTMC.| | ||
| + | |recurrentClasses|list of recurrent classes.| | ||
| + | |steadyStates|the vector(s) of steady state(s) in matrix form.| | ||
| + | |summary|DTMC summary.| | ||
| + | |transientStates|the transient states of the transition matrix, if any.| | ||
| + | |||
| + | < | ||
| + | conditionalDistribution(mcWeather, | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | steadyStates(mcWeather) | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | absorbingStates(mcWeather) | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | summary(mcWeather) | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | transientStates(mcWeather) | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | is.accessible(object = mcWeather, from = " | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | period(mcWeather) | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | firstPassage(object = mcWeather, state = " | ||
| + | </ | ||
| + | |||
| + | === マルコフ連鎖のための統計的推測 === | ||
| + | |||
| + | |Function|Purpose| | ||
| + | |markovchainFit|Function to return fitted Markov chain for a given sequence.| | ||
| + | |predict|Method to calculate predictions from markovchain or markovchainList objects.| | ||
| + | |rmarkovchain|Function to sample from markovchain or markovchainList objects.| | ||
| + | |||
| + | 状態遷移のデータがあるとする。 | ||
| + | |||
| + | Alofi島の1日あたりの降雨のデータがある。 | ||
| + | |||
| + | < | ||
| + | data(rain) | ||
| + | </ | ||
| + | |||
| + | このデータは、2つのフィールドV1とrainを持つ。使うのは rain の方。 | ||
| + | 一日ごとの推移を集計してみる。 | ||
| + | |||
| + | < | ||
| + | table(data.frame(before=rain$rain[-1], after=rain$rain[-length(rain$rain)])) | ||
| + | </ | ||
| + | |||
| + | 出力は次の通り。 | ||
| + | |||
| + | < | ||
| + | after | ||
| + | before | ||
| + | | ||
| + | 1-5 126 90 79 | ||
| + | | ||
| + | </ | ||
| + | |||
| + | この推移をグラフに表してみる。Gmiscパッケージの中の関数transitionPlotを使うために、準備をする。 | ||
| + | |||
| + | < | ||
| + | install.packages(c(" | ||
| + | reps = c(" | ||
| + | install.packages(" | ||
| + | </ | ||
| + | |||
| + | そして、使ってみる。 | ||
| + | |||
| + | < | ||
| + | require(Gmisc) | ||
| + | transitionPlot(table(data.frame(r1=rain$rain[-1],r2=rain$rain[-length(rain$rain)])), | ||
| + | | ||
| + | min_lwd = unit(2, " | ||
| + | </ | ||
| + | |||
| + | {{: | ||
| + | |||
| + | < | ||
| + | library(dplyr) | ||
| + | library(stringr) | ||
| + | library(DiagrammeR) | ||
| + | library(networkD3) | ||
| + | </ | ||
| + | |||
| + | この準備で頑張ると、[[https:// | ||
| + | |||
| + | この推移から、遷移行列を推定する。 | ||
| + | |||
| + | < | ||
| + | markovchainFit(rain$rain, | ||
| + | </ | ||
| + | |||
| + | 次の推定値などが表示される。 | ||
| + | |||
| + | < | ||
| + | $estimate | ||
| + | 0 | ||
| + | 0 | ||
| + | 1-5 0.4625850 0.3061224 0.2312925 | ||
| + | 6+ 0.1976285 0.3122530 0.4901186 | ||
| + | |||
| + | |||
| + | $standardError | ||
| + | | ||
| + | 0 | ||
| + | 1-5 0.03966634 0.03226814 0.02804834 | ||
| + | 6+ 0.02794888 0.03513120 0.04401395 | ||
| + | |||
| + | $confidenceLevel | ||
| + | [1] 0.95 | ||
| + | |||
| + | $lowerEndpointMatrix | ||
| + | 0 | ||
| + | 0 | ||
| + | 1-5 0.3973397 0.2530461 0.18515711 | ||
| + | 6+ 0.1516566 0.2544673 0.41772208 | ||
| + | |||
| + | $upperEndpointMatrix | ||
| + | 0 | ||
| + | 0 | ||
| + | 1-5 0.5278304 0.3591988 0.2774279 | ||
| + | 6+ 0.2436003 0.3700387 0.5625151 | ||
| + | |||
| + | $logLikelihood | ||
| + | [1] -1040.419 | ||
| + | </ | ||
| + | |||
| + | rain.MCに代入しておく。 | ||
| + | |||
| + | < | ||
| + | rain.mcfit <- markovchainFit(rain$rain, | ||
| + | </ | ||
| + | |||
| + | 返して貰った内容を見てみる。 | ||
| + | |||
| + | < | ||
| + | str(rain.mcfit) | ||
| + | </ | ||
| + | |||
| + | rain.MCはS3オブジェクトのリストだが、rain.MC$estimateはS4オブジェクトとややこしい。 | ||
| + | 遷移行列を行列として取り出すには、次のようにリストの名前とその中のスロットを指定する。 | ||
| + | |||
| + | < | ||
| + | rain.mcfit$estimate@transitionMatrix | ||
| + | </ | ||
| + | |||
| + | これによって新たなマルコフ連鎖を定義するには、次のように書く。 | ||
| + | |||
| + | < | ||
| + | mcrain = new(" | ||
| + | byrow = TRUE, | ||
| + | transitionMatrix = rain.mcfit$estimate@transitionMatrix, | ||
| + | </ | ||
| + | |||
| + | 関数markovchainFitは、markovchainオブジェクトをリストの要素として出力しているので、そちらを使うのも良い。 | ||
| + | |||
| + | < | ||
| + | mcrain = rain.mcfit$estimate | ||
| + | </ | ||
| + | |||
| + | |||
| + | < | ||
| + | rain.mcfit <- markovchainFit(rain$rain, | ||
| + | </ | ||
| - | なお、互いに独立な確率変数の列もマルコフ性は満たす。 | ||