「検査法の正答率９割」を全列挙する

ある感染症の感染者と非感染者を識別する問題を考えます。ある検査法において陽性・陰性という結果が出た時、検査結果は次の４種類に分類できます。

- TP (True Positive) ：検査結果が陽性と出た感染者
- FN (False Negative) ：検査結果が陰性と出た感染者
- FP (False Positive)：検査結果が陽性と出た非感染者
- TN (True Negative) ：検査結果が陰性と出た非感染者

このとき、正答率（正解率）は Accuracy = (TP + TN) / (TP + FN + FP + TN) と計算できます。

このとき、次の問題について考えてみましょう。

> 【問題】「正答率９割」を謳っている検査法がある。この検査法で正答率を計算した時点において、検査を受けた人の５０％が感染者の場合、１０％が感染者の場合、１％が感染者の場合という３つのケースに対して、陽性者が感染者である確率を推定しなさい。

この問題に答えるPythonコードを書いてみます。

# 日本語版 matplotlib をインストール


```Python
!pip install japanize-matplotlib
```

    Collecting japanize-matplotlib
    [?25l  Downloading https://files.pythonhosted.org/packages/aa/85/08a4b7fe8987582d99d9bb7ad0ff1ec75439359a7f9690a0dbf2dbf98b15/japanize-matplotlib-1.1.3.tar.gz (4.1MB)
    [K     |████████████████████████████████| 4.1MB 4.0MB/s 
    [?25hRequirement already satisfied: matplotlib in /usr/local/lib/python3.6/dist-packages (from japanize-matplotlib) (3.2.2)
    Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib->japanize-matplotlib) (2.4.7)
    Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib->japanize-matplotlib) (1.3.1)
    Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib->japanize-matplotlib) (0.10.0)
    Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib->japanize-matplotlib) (2.8.1)
    Requirement already satisfied: numpy>=1.11 in /usr/local/lib/python3.6/dist-packages (from matplotlib->japanize-matplotlib) (1.19.5)
    Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from cycler>=0.10->matplotlib->japanize-matplotlib) (1.15.0)
    Building wheels for collected packages: japanize-matplotlib
      Building wheel for japanize-matplotlib (setup.py) ... [?25l[?25hdone
      Created wheel for japanize-matplotlib: filename=japanize_matplotlib-1.1.3-cp36-none-any.whl size=4120276 sha256=286795079465880c2c9d5d742e7259a51cb2f2923b5bf644de4121990dcccd1b
      Stored in directory: /root/.cache/pip/wheels/b7/d9/a2/f907d50b32a2d2008ce5d691d30fb6569c2c93eefcfde55202
    Successfully built japanize-matplotlib
    Installing collected packages: japanize-matplotlib
    Successfully installed japanize-matplotlib-1.1.3


# 「検査法の正答率９割」を全列挙する

どのような場合に「検査法の正答率９割」になるのか、全列挙するコードはこちらになります。


```Python
import numpy as np
import matplotlib.pyplot as plt
import japanize_matplotlib 

percentage_infected = 0.1  # 検査を受けた人のうち感染者の割合
population_size = 100 # 検査を受けた人数
target_accuracy = 0.9 # 正答率の値

def enumerate_cases(percentage_infected, population_size, target_accuracy):
    infected = population_size * percentage_infected # 感染者の数
    notinfected = population_size * (1 - percentage_infected) # 非感染者の数
    
    case_id = 0
    for TP in range(int(infected) + 1): # 全感染者のうちのTPの数
        FN = int(infected - TP) # 全感染者のうちTPを除いた数がFN
        for TN in range(int(notinfected) + 1): # 全非感染者のうちのTNの数
            FP = int(notinfected - TN) # 全非感染者のうちTNを除いた数がFP
            accuracy = (TP+TN)/(TP+FN+FP+TN) # 正答率の計算
            if accuracy == target_accuracy: # 目的の正答率と一致したら表示
                case_id += 1
                print("Case {}".format(case_id))
                fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(12, 4))
                axes[0].set_title("検査人数: {}名".format(population_size))
                axes[0].pie([TP, FN, FP, TN], counterclock=False, autopct="%.1f%%", startangle=90, 
                            labels=["TP（感染・陽性）", "FN（感染・陰性）", "FP（非感染・陽性）", "TN（非感染・陰性）"], 
                            colors=["red", "orange", "pink", "skyblue"])

                axes[1].set_title("検査結果が陽性: {}名".format(TP+FP))
                axes[1].pie([TP, FP], counterclock=False, autopct="%.1f%%", startangle=90, 
                            labels=["TP（感染・陽性）", "FP（非感染・陽性）"], colors=["red", "pink"])
                
                axes[2].set_title("検査結果が陰性: {}名".format(FN+TN))
                axes[2].pie([FN, TN], counterclock=False, autopct="%.1f%%", startangle=90, 
                            labels=["FN（感染・陰性）", "TN（非感染・陰性）"], colors=["orange", "skyblue"])
                plt.show()
```

# 50%が感染者（検査人数100人）の場合


```Python
percentage_infected = 0.5  # 検査を受けた人のうち感染者の割合
population_size = 100 # 検査を受けた人数
target_accuracy = 0.9 # 正答率の値

enumerate_cases(percentage_infected, population_size, target_accuracy)
```

    Case 1


![accuracy90_6_1.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/bcc78603-2a6f-0023-351b-44b9f90cfb93.png)




    Case 2


![accuracy90_6_3.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/85429cbf-11b8-5acd-3a67-69b7c9c8ba8c.png)




    Case 3


![accuracy90_6_5.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/26252eac-9be0-0182-2595-b60678430afc.png)




    Case 4


![accuracy90_6_7.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/e4ed9a86-41dd-d774-53dd-517036377a23.png)




    Case 5


![accuracy90_6_9.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/127f238e-94ec-c934-137a-c01aaccbd0ae.png)




    Case 6


![accuracy90_6_11.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/9622053f-0b06-2b48-94fa-1c08096e161d.png)




    Case 7


![accuracy90_6_13.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/b6f2b0c0-f4ca-31e9-7b16-edd90f187fb9.png)




    Case 8


![accuracy90_6_15.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/73655088-796d-9ded-d6bd-0fa83b2e98db.png)




    Case 9


![accuracy90_6_17.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/e600173c-c1a0-cff4-8c26-0f6a6a1d2d19.png)




    Case 10


![accuracy90_6_19.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/b75224ec-8c92-083c-483b-c27825cb22d8.png)




    Case 11


![accuracy90_6_21.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/f41b4d16-e600-0324-a619-70db299b3187.png)



以上の１１ケースが得られました。感染者・非感染者の数が拮抗している場合は、「正答率９割」はけっこう信頼できそうな気がします。

# 10%が感染者（検査人数100人）の場合


```Python
percentage_infected = 0.1  # 検査を受けた人のうち感染者の割合
population_size = 100 # 検査を受けた人数
target_accuracy = 0.9 # 正答率の値

enumerate_cases(percentage_infected, population_size, target_accuracy)
```

    Case 1


![accuracy90_8_1.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/8c8a4ae4-1728-34f0-1c6f-a9fd9560caa1.png)




    Case 2


![accuracy90_8_3.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/baf8d6b4-ebf9-0d9a-8d52-011f6931e822.png)




    Case 3


![accuracy90_8_5.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/9ac7e97a-c7cc-f9fb-10d1-1b0234443926.png)




    Case 4


![accuracy90_8_7.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/45848d32-a4b1-ec8a-419f-8fc9ff20af4d.png)




    Case 5


![accuracy90_8_9.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/493fae17-60d1-cdc3-87a8-6a52b093b72c.png)




    Case 6


![accuracy90_8_11.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/342aea0e-a48c-1aa9-d6d2-23e71cec3baa.png)




    Case 7


![accuracy90_8_13.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/323440a0-d1f6-cbb7-6954-dd583ce1f77a.png)




    Case 8


![accuracy90_8_15.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/2a9c43da-dc2c-7c93-a1eb-fc21d82739d9.png)




    Case 9


![accuracy90_8_17.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/32840286-97aa-0c92-5aa1-303f195bb7a7.png)




    Case 10


![accuracy90_8_19.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/08f13fb4-3cf1-ecbb-0664-9cfdcf52335c.png)




    Case 11


![accuracy90_8_21.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/77ea4af7-89d5-2629-02b8-b7ef2c6efa56.png)




こちらも１１ケースが得られましたが、感染者の割合が10%まで少なくなると、「正答率９割」と言えども、「検査結果が陽性」の人の中の「感染者」の割合は５０％に過ぎないということが分かります。


# 1%が感染者（検査人数1000人）の場合


```Python
percentage_infected = 0.01  # 検査を受けた人のうち感染者の割合
population_size = 1000 # 検査を受けた人数
target_accuracy = 0.9 # 正答率の値

enumerate_cases(percentage_infected, population_size, target_accuracy)
```

    Case 1


![accuracy90_12_1.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/a3510443-040e-a256-5731-ef7877ebc5f7.png)



    Case 2


![accuracy90_12_3.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/25468855-8778-d733-ddd0-cf75790a649c.png)




    Case 3


![accuracy90_12_5.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/682ce669-3357-00de-39a5-8e98aab0c07b.png)




    Case 4


![accuracy90_12_7.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/c8077b8f-f401-8e0b-a245-e5b7c28d7089.png)




    Case 5


![accuracy90_12_9.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/53f3f172-45ab-f263-0cc3-e52659f05cdb.png)




    Case 6


![accuracy90_12_11.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/47680fad-32fc-fddb-c455-6c7698475694.png)




    Case 7


![accuracy90_12_13.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/19cda9bc-1280-de34-67ea-eb6cf1ec2cb2.png)




    Case 8


![accuracy90_12_15.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/c20cefc7-fe14-3035-65a9-c68740bf5c77.png)




    Case 9


![accuracy90_12_17.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/1d21fe51-33a7-726d-018c-8cde9a629539.png)




    Case 10


![accuracy90_12_19.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/c27839d3-aadf-d2ca-e2c7-ef21e3c73e2b.png)




    Case 11


![accuracy90_12_21.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211162/12e6989a-6651-e206-05f6-c0d7a35c6757.png)



検査人数１０００人の場合、１％が感染したケースは１１とおり考えられますが、いずれも「陽性の人が感染者である確率」はけっこう低いことが分かります。

# 教訓

「正答率９割」という、たったひとつの数字だけでは正しい情報は得られないので、情報提供する側にはもっとデータを開示していただきたいし、情報を受け取る側も、たったひとつの数字だけで判断しないように気をつけていただきたいと思います。