SlideShare a Scribd company logo
1 of 54
確率分布をふわっと理解する
@koriakane
Tokyo.R #70
自己紹介
• 宮﨑(@koriakane)
• Tokyo.R運営チーム所属(会計、調整、諸々)
• 某ケーブルテレビ会社に所属
• 近況:今日もがんばる
この資料の目的
• 後ろに控えるy__mattu氏、LTの方々、ベイズ三銃士
(勝手に命名)に繋げる
• 「ベイズ統計モデリングってなんぞ?」という方が、
確率分布にどんなのがあるのか(ふわっと)理解する
• 「確率分布を知らないとどのようにモデル化すべきか思いつ
くのは難しい」ってアヒル本にも書いてある
この資料で扱う範囲
• 確率分布の紹介 + それぞれの確率分布に従った乱数の
生成方法 + 結果の可視化
• 時間の許す限り紹介する
• ベイズ統計モデリングで確率分布が必要な理由は、
kilometer00さんの発表の通りなので割愛
• Stan・MCMCについてはy__mattuさんがカバーしてくれ
る、って信じてる
Attention
• スクリプトは、後日どこかで公開します
• 手直し間に合わず。。
• 可視化はggplot2で作ってます
• 資料は後ほど公開します
• 数学的な解説は、ワテクシの資料より、こっちの方が
詳しい
• https://www.slideshare.net/matsukenbook/rev012
• ゆうとくけど、ワテクシも初心者
Attention
• あとこれ読みましょう。おすすめ。
では参ります
確率分布に関する関数
• d~~(q):確率密度(q:確率点)
• p~~(q):累積分布(q:確率点)
• q~~(p):確率点(q:確率)
• r~~(n):乱数生成(n:個数)
# 例:標準正規分布
X <-rnorm(5000, # 乱数の個数
mean = 0, # 平均
sd = 1 #標準偏差)
一様分布
一様分布:unif
• ある特定の区間では、どこでも同じ確率の分布
• 統計モデリングでは、無情報事前分布として使われる
# 乱数発生方法
# 今回は標準一様分布
Y <- runif(5000,
min = 0, max = 1)
ベルヌーイ分布
ベルヌーイ分布:bern
• 0/1の出現する確率の分布
• ロジスティック回帰の説明変数の分布
# 乱数発生方法
# パッケージが必要
install.packages("extraDistr")
library("extraDistr")
Y <- extraDistr::rbern(5000,
prob = 0.5)
二項分布
二項分布:binom
• ベルヌーイ試行をn回繰り返した場合、1となった回数
の分布
# 乱数発生方法
# 50%の確率で1が出るベルヌーイ試行を
# 100回やる、のを5,000回やる
N <-5000
Y_binom <- rbinom(N,
size = 100,
prob = 0.5)
ここまでやって思ったこと
淡々としててつまらん
なんというか確率分布の種類を知るというので
あれば正直書籍で体系的に学べばいいのであっ
てわざわざトーキョーアールに足を運んできた方々のた
めに本当になるのかしらんただ淡々と紹介して
もつまらないよなぁでもこういう内容も大事だ
と思うんよだって初心者向けとうたってるわけ
だしでもやっぱり実際に統計モデリングやる
時って順序だってやれずに手探りでやることを
想定すると参考にならないかなぁと思うと全然
ということで趣向を変えます
実際のデータから
近似しそうな分布を紹介します
例その1
例えばこんなデータを予測したいとする
• このデータを目的変数として、「説明変数でこの数値を
どれだけ予測できるか」が知りたい
• 山形の分布
• でも左右対称とはいいづらい
• むった裾が長いわけでもない
• 0以上
• ちなみに歩数のデータ
このデータに使えそうな分布を
みていきます
正規分布:norm
• 平均に対して左右対象になっている分布
• 分布の範囲が(-∞,∞)なので0以下もとる
X <-rnorm(5000, # 乱数の個数
mean = mean(df$WALK),
sd =sd(df$WALK))
Studentのt分布:t
• (自由度が小さい場合)正規分布よりも裾の長い分布
• 正規分布よりもノイズに強く、外れ値を含む(可能性の
ある)モデルに使われる
# パッケージが必要
install.packages("metRology")
library("metRology")
X <-rt.scaled(5000, #乱数の個数
df = 5, #自由度
mean = mean(df$WALK),
sd =sd(df$WALK))
コーシー分布:cauthy
• t分布の特殊系。裾むった長い。
• 外れ値を含んだモデルや変化点の検知に使用される
X <-rcauchy(123, #乱数の個数
location = 8471, #位置
scale =3) #尺度
対数正規分布
• ロングテールな分布
• 分布の範囲が0以上になる
me<- mean(df$WALK)
sd <- sd(df$WALK)
sdlog <- sqrt(log((sd/me)^2 + 1))
meanlog <- log(me)- (sdlog^2) / 2
X_rlnorm <- rlnorm(5000,
meanlog =meanlog,
sdlog = sdlog)
なんとなく使えそうな
気がしてきました?
次いきましょう
例その2
例えばこんなデータを予測したいとする
• 1ヶ月の間にAをした日数(回数)の分布
• その人がどれだけAをするか、を予測したい
• ロングテール
• むった0多い
• 業務でむったこの分布見る
例えばこんなデータを予測したいとする
• 0抜いてみる
このデータに使えそうな分布は?
ポアソン分布:pois
• 単位時間あたり平均λ回起こる現象が、k回起こる確率
の分布
• 「〜〜の回数」の時に使える
# 0を抜いたデータの平均値を使う
X <-rpois(5000,
lambda = mean(df$TIMES))
でもちょっと待てよ?
例えばこんなデータを予測したいとする
• 0抜いてみた
• ……0抜いてええんか?
こんな可能性もあるのでは?
今までもこれからも
Aをしない人
今回たまたましなかった人
→来月するかもしれない
つまり
• 「そもそも、Aをするタイプなのかそうでないのか」の
ベルヌーイ分布があり、
• 「Aをするタイプの人」の場合、1ヶ月あたりの日数は
ポアソン分布に従っているのではないか
• と考える
→ゼロ過剰ポアソン分布
シミュレーション
# ベルヌーイ分布で、0の多い0/1の乱数を作成
# rbinom()でsize=1を指定してもできます
# 170/1000は、実データの比率
X_bern <-rbinom(n = 5000, size= 1, prob = 170/1000)
# ポアソン分布から生成した乱数と、
# 上記ベクトルから0を抽出したものをマージ
X <-c(rpois(2000,
lambda = mean(CH_WATCH_zero$TIMES)),
X_bern[X_bern == 0])
結果
1回起こるまでの時間を
使いたい場合は?
指数分布:exp
• 期間μごとに1回くらい起こるランダムな事象が、1回
起こるまでの時間の分布
rate= 30/7 # 単位時間を30日とした時に、7日目で実
施する時のパラメータ
X <-rexp(5000, rate = rate)
ガンマ分布:gamma
• 期間μごとに1回くらい起こるランダムな事象が、n回
起こるまでの時間の分布
• 待ち時間に使う
shape = 3 # 3回する
rate = 30/7 # 単位時間を30日とした時に、7日目で実
施する時のパラメータ
X <-rgamma(5000,
shape = shape,
rate = rate)
ちょっと話題変えます
自己紹介
• 宮﨑(@koriakane)
• Tokyo.R運営チーム所属(会計、調整、諸々)
• 某ケーブルテレビ会社に所属
• 近況:今日もがんばる
ちょっと仕事っぽい話をします
番組評価指標を考える
• 番組評価指標のデファクトスタンダードは「視聴率」
• だがしかし、視聴率だとその日に観た人の率はわかる
けど、「番組を毎週観たか」ってのは分からない
• 毎週放送している番組の指標として、「番組のリピー
ト傾向」みたいなのがあるといいんではないか
• っていうのを企画書に書きたい時に、それっぽいデー
タがあると便利!(実際にやったわけではない)
週1で欠かさず観る人と、
途中で挫折した人を考慮して、
ある番組を日毎に何人観たかって
乱数作れるんでは?
こういうのを考える
• 毎週放送の番組を考える
• 「途中で挫折する人」がいるので、話数が進むにつれ
人数は減少する
• 放送当日にリアルタイムで観る人が最も多く、次いで
翌日(翌々日)に録画再生で見る人が多い
• そのため、放送日までは指数関数的に人数が減少する
こんな感じ
• うなどんさんにご教示頂きました!ありがてぇ!
Rコード
N <- 7
X <- 1:N # 1周期のタイムポイントの数
Intercept<- 50# 一番最初の値 (※適当)
gamma <-0.7 # 指数関数の係数(0-1の値) (※適当)
r<- 10 # リフトする係数 (※適当)
# 1周期目
mu_1= c(Intercept* gamma ^ X)
# 2周期目
mu_2= c(mu_1[N]* r* gamma ^ X) # 1周期目の最後の値を2倍して、それが切片
# 3周期目
mu_3= c(mu_2[N]* r* gamma ^ X)
# 4周期目
mu_4= c(mu_3[N]* r* gamma ^ X)
# 4周期分をがっちゃんこ
mu <-c(mu_1,mu_2,mu_3,mu_4)
# 指数関数で"Xが上がるほど切片が減衰する"のデータをYに
Y <- rnorm(4* N, mean = mu, sd = 0.5)
Rコード
X_label <- 1:28# 1:7を4回くりかえしたので、X軸として1:28をうつ
# plot
goboten_udon <-data.frame(X = X_label, Y = mu)%>%
ggplot(mapping = aes(x = X, y= Y, colour= Y)) +
theme_bw() +
geom_point(size = 2.5)+
geom_line(mapping = aes(y = mu), size= 1.5,alpha = 0.5)+
scale_colour_gradient(low = "#00868B",high ="#4169E1")+
guides(colour = "none")
plot(goboten_udon)
まとめ
まとめ
• 紹介しきれなかった確率分布が色々あります
• ので、自己学習をおすすめします
個人的に最近はベータ分布をよくみます
余力があれば、ブログとか書くかも?
• 確率分布を体系的に理解するには、書籍がおすすめ
• 実際には、使いたいデータの分布を見て、「これに似
た確率分布は何かな」と調べるのが吉
個人的には、実際に使う or 事例をみないと覚えられない人
間なので、業務関係なく分布は見るようにしてます。
Enjoy !

More Related Content

Featured

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 

Featured (20)

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 

Tokyo.R #70 初心者セッション2