False Positive(위양성)에 대한 베이즈 정리 문제 with R

Matt Lee·2020년 7월 21일
0

기초 확률론

목록 보기
14/26
post-thumbnail

이번 포스팅에서는 False Positive(위양성)에 대한 간단한 베이즈 정리 문제를 R을 활용 해서 풀어 보겠습니다.

문제

  • One prominent manufacturer of medical tests offers a test for chlamydia (a sexually transmitted disease) that has a sensitivity of 76.4%76.4\% and a specificity of 93.2%93.2\%. In other words, the test correctly identifies 76.4%76.4\% of individuals tested who have the disease by giving a positive result, and correctly identifies 93.2%93.2\% of the individuals who are healthy by giving a negative result. Currently, it is estimated that 1.5%1.5\% of the American population has chlamydia.

  • If one individual is randomly selected from the population and tests positive for chlamydia, what is the probability that he/she does not have the disease?

문제 풀이

1. 문제에서 주어진 정보를 가지고 확률을 R 코딩을 통해 정의 해 보겠습니다.

Currently, it is estimated that 1.5%1.5\% of the American population has chlamydia.

  • 위의 정보를 바탕으로 클라미디아에 걸린 미국인의 비율이 1.5%1.5\% 임을 알 수 있습니다. 여기서 C는 chlamydia를 의미 하며 확률식으로는 P(C)P(\text{C}) 입니다.
> C <- 1.5/100
> C
[1] 0.015

다음은 C의 확률을 가지고 C의 여사건의 확률을 정의 합니다.

  • 여기서 notC는 chlamydia에 걸리지 않았다는 의미 이며 확률식은 다음과 같습니다.

    P(notC)P(\text{notC}) 또는 P(C)P(\text{C}^{'})

  • R 코드는 다음과 같습니다.

> notC <- 1 - C
> notC
[1] 0.985

the test correctly identifies 76.4%76.4\% of individuals tested who have the disease by giving a positive result

  • 위의 정보는 클라미디아에 걸린 사람에 대해서 테스트를 실시 하면 클라미디에 걸렸다고 정확하게 검진 하는 경우가 76.4%76.4\%라는 의미 입니다. 여기서 PositiveTest_Given_C에 대한 확률식은 다음과 같습니다.

P(PositiveC)P(\text{Positive}|\text{C})

  • R 코드는 다음과 같습니다.
> PositiveTest_Given_C <- 76.4/100
> PositiveTest_Given_C
[1] 0.764

다음은 PositiveTest_Given_C의 확률을 가지고 PositiveTest_Given_C의 여사건의 확률을 정의 합니다. 여기서 NegativeTest_Given_C에 대한 확률식은 다음과 같습니다.

P(NegativeC)P(\text{Negative}|\text{C}) 또는 P(PositiveC)P(\text{Positive}^{'}|\text{C})

  • R 코드는 다음과 같습니다.
> NegativeTest_Given_C <- 1 - PositiveTest_Given_C
> NegativeTest_Given_C
[1] 0.236

correctly identifies 93.2%93.2\% of the individuals who are healthy by giving a negative result.

  • 위의 정보는 클라미디아에 걸리지 않은 사람에 대해서 테스트를 실시 하면 클라미디에 걸리지 않았다고 정확하게 검진 하는 경우가 93.2%93.2\%라는 의미 입니다. 여기서 NegativeTest_Given_notC에 대한 확률식은 다음과 같습니다.

P(NegativenotC)P(\text{Negative}|\text{notC}) 또는 P(PositiveC)P(\text{Positive}^{'}|\text{C}^{'})

  • R 코드는 다음과 같습니다.
> NegativeTest_Given_notC <- 93.2/100
> NegativeTest_Given_notC
[1] 0.932

다음은 NegativeTest_Given_notC의 확률을 가지고 NegativeTest_Given_notC의 여사건의 확률을 정의 합니다. 여기서 PositiveTest_Given_notC에 대한 확률식은 다음과 같습니다.

P(PositivenotC)P(\text{Positive}|\text{notC}) 또는 P(PositiveC)P(\text{Positive}^{'}|\text{C}^{'})

  • R 코드는 다음과 같습니다.
> PositiveTest_Given_notC <- 1 - NegativeTest_Given_notC
> PositiveTest_Given_notC
[1] 0.068

2. 1번에서 정의된 사건들의 확률을 바탕으로 전체 확률 법칙 정의

Total Probability for Positive

  • Positive에 대한 전체 확률 법칙은 다음과 같습니다.

    P(C)×P(PositiveC)+P(notC)×P(PositivenotC)P(\text{C}) \times P(\text{Positive}|\text{C}) + P(\text{notC}) \times P(\text{Positive}|\text{notC})

  • R 코드는 다음과 같습니다.

> PositiveTest <- (C * PositiveTest_Given_C) + (notC * PositiveTest_Given_notC)
> PositiveTest
[1] 0.07844

Total Probability for Negative

  • Negative에 대한 전체 확률 법칙은 다음과 같습니다.

P(C)×P(NegativeC)+P(notC)×P(NegativenotC)P(\text{C}) \times P(\text{Negative}|\text{C}) + P(\text{notC}) \times P(\text{Negative}|\text{notC})

  • R 코드는 다음과 같습니다.
> NegativeTest <- (C * NegativeTest_Given_C) + (notC * NegativeTest_Given_notC)
> NegativeTest
[1] 0.92156

3. 1번과 2번 정보를 바탕으로 베이즈 정리를 활용한 확률 계산

베이즈 정리를 사용하기 위한 모든 확률 정보가 생성 되었습니다. 이 정보를 바탕으로 베이즈 정리를 사용해서 우리가 원하는 목표 확률을 계산 하겠습니다.

If one individual is randomly selected from the population and tests positive for chlamydia, what is the probability that he/she does not have the disease? Using Bayes’ Theorem P(notC|PositiveTest)
  • 랜덤으로 뽑은 사람에게 클라미디아에 대한 검사를 실시해서 양성이 나왔습니다. 이 때 이 사람이 클라미디아에 걸리지 않았을 확률은 얼마입니까? 이 문제에 대한 답변은 P(notC|Positive)를 이용해서 계산 할 수 있습니다. P(notC|Positive)에 대한 베이즈 정리 확률식은 다음과 같습니다.
P(notCPositive)=P(notC)×P(PositivenotC)P(Positive)=P(notC)×P(PositivenotC)P(notC)×P(PositivenotC)+P(C)×P(PositiveC)\begin{aligned} P(\text{notC}|\text{Positive}) &= \frac{P(\text{notC}) \times P(\text{Positive}|\text{notC})}{P(\text{Positive})} \\ &= \frac{P(\text{notC}) \times P(\text{Positive}|\text{notC})}{P(\text{notC}) \times P(\text{Positive}|\text{notC}) + P(\text{C}) \times P(\text{Positive}|\text{C})} \end{aligned}
  • R 코드는 다음과 같습니다.
> notC_Given_PositiveTest <- (notC * PositiveTest_Given_notC) / PositiveTest
> notC_Given_PositiveTest
[1] 0.8539011
위의 계산 결과를 보면 랜덤으로 뽑은 사람에게 클라미디아에 대한 검사를 실시해서 양성이 나왔을 때 이 사람이 클라미디아에 걸리지 않았을 확률은 약85.39%85.39\% 임을 알 수 있습니다.
profile
미국에 서식 중인 응용 수학과 대학원생, 아직은 잉여지만 그래도 행복 :)

0개의 댓글