/
RW_simulation.R
66 lines (48 loc) · 1.68 KB
/
RW_simulation.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# ----------------------------------------------------- #
# Rescorla-Wagner (RW) モデルのシミュレーション
# ----------------------------------------------------- #
# メモリとグラフのクリア
rm(list=ls())
graphics.off()
# 描画のためのライブラリ読み込み
library(tidyverse)
# RW modelのシミュレーションの実行 ----------------------------------------------
# 乱数のシードの設定
set.seed(928)
# 試行数
T <- 50
# 学習率
alpha <- 0.2
# 報酬確率
pr <- 0.8
# 連合強度( 価値) V と報酬r の値を格納するベクトルを作成
V <- numeric(T)
r <- numeric(T)
for (t in 1:T) {
# 報酬の有無の決定
r[t] <- as.numeric( runif(1 ,0 ,1) < pr)
# 連合強度の更新
if (t < T) {
V[t +1] <- V[t] + alpha * (r[t] - V[t])
}
}
# グラフの描画 ------------------------------------------------------------------
# ggplotのテーマ設定
ggplot() + theme_set(theme_bw(base_size = 18))
# 描画用ウィンドウの生成
# (別ウインドウに図をプロットします。
# RStudio内やRmarkdown内でプロットする際はこの行は外してください。)
x11()
# 描画するデータをデータフレームにまとめる
dfplot <- data.frame(trials = 1:T,
V = V,
r = as.factor(r))
# 描画
g <- ggplot(dfplot, aes(x = trials, y = V)) +
geom_line(aes(y = V), linetype = 1, size=1.2) +
geom_point(data = dfplot %>% filter(r == 1), # 報酬有りの試行を抜きだす
aes(x = trials, y = 1.12), shape = 25, size = 1.5) +
scale_y_continuous(breaks=c(0, 0.5, 1.0), labels = c(0,0.5,1)) +
xlab("試行") +
ylab("連合強度 V")
print(g)