On this paper by David Hand the misclassification costs are $c_0$ and $c_1$ with the ratio $c$ defined as $c=c_0/(c_0+c_1).$ The threshold considered optimal is
$$T(c)=\underset{t}{\text{arg min}}\left\{ c\pi_0(1-F_0(t)) + (1-c) \pi_1 F_1(t) \right\}$$
with $t$ being any threshold in the scores, and $1=\pi_0+\pi_1$ the fractions of diseased and healthy individuals in the population. $F_0$ is the distribution of diseased, and $F_1$ the distribution of healthy individuals. Cases are considered class $0.$
Assuming a one-to-one relationship of $c$ with $T$ and differentiating, he gets the following result (equation (5)):
$$c= \color{red}{\Pr(1\vert T)} = \pi_1 f_1(T) / \left\{ \pi_0f_0(T) + \pi_1f_1(T)\right\}$$
where $\Pr(1\vert T)$ is the conditional probability of belonging to class $1,$ given the score $T.$
I follow everything in here, except for the part in red. It may be a very basic misunderstanding but shouldn't the conditional probability be calculated from a ratio of cdf's, as opposed to pdf's?
I presume the answer is in considering the scores $s(x)$ in the logistic regression as a monotonically increasing transformation of $\Pr(1 \vert x).$
Here is an unsuccessful attempt at hacking this:
install.packages('pROC')
install.packages('ROCR')
install.packages('Epi')
library(pROC)
library(ROCR)
library(Epi)
set.seed(561)
cost0 = 1 # Cost of mis-classifying a normal as having cancer in million $
cost1 = 10 # Cost of mis-classifying a cancer patient as normal (death?)
b = cost0 + cost1
c = cost0/(b)
n = 7000 # Total cases
pi0 =.8 # Percentage of normal
pi1 =.2 # Percentage of disease
# Actual values of the test for normals and disease (D higher test values)
testA_Normals = rnorm(n*pi0, mean=3, sd=1)
testA_Sick = rnorm(n*pi1, 6, 1)
# Determining a threshold based on cost
# arg t min {Loss = cost0 * (1 - pnorm(t,3,1)) * pi0 +
# cost1 * pnorm(t,6,1) * pi1}
t = seq(0,10,0.0001)
loss <- cost0 * (1 - pnorm(t,3,1)) * pi0 + cost1 * pnorm(t,6,1) * pi1
Threshold = data.frame(t,loss)[which(loss==min(loss)),]$t
hist(testA_Normals,border=F, xlim=c(0,10))
hist(testA_Sick,col=2,border=F, add=T)
abline(v=Threshold)
Comparing the 3 equalities in the equation:
c
pi1 * dnorm(Threshold,6,1) / (pi0 * dnorm(Threshold,3,1) + pi1 * dnorm(Threshold,6,1))
#P(1|T) = P(T|1) * P(1) / [P(T|0) * P(0) + P(T|1) * P(1)]
(pnorm(Threshold,6,1,F)*pi1)/(pnorm(Threshold,6,1,F)*pi1+
pnorm(Threshold,3,1,F)*pi0)
0.0909090909090909
0.0909165896894187
0.6749524!!!!!(***)
$(***) \text{Edit}:$ After getting help from a very reliable source (unnamed lest I misquote) the thinking behind $\Pr(1\vert T)$ is not $\Pr(1\vert s \geq T),$ which would be what my code would suggest, but rather $\Pr(1\vert s=T),$ or $$\Tiny\lim_{d \to 0}\frac{d \pi_1 f_1(T)}{ d \pi_1 f_1(T) + d \pi_0 f_0(T) }= \lim_{d \to 0} \Pr\left(1\Big\vert -\frac d 2 +T +\frac d 2\right). $$