File: outlier_robust_llr.R

package info (click to toggle)
tombo 1.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 24,424 kB
  • sloc: python: 12,438; sh: 613; makefile: 10
file content (36 lines) | stat: -rw-r--r-- 1,452 bytes parent folder | download | duplicates (4)
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
library(ggplot2)

scaleFactor <- 4
scaleFactor2 <- 3
scalePower <- 0.2

firstMean <- 0
secondMeans <- seq(0.05, 0.8, 0.05)
constVar <- 0.12

xlims <- c(-2,2)

pdf('llr_test.pdf', width=10)
for (secondMean in secondMeans){
    secondMean <- firstMean + secondMean
    test_vals <- seq(xlims[1], xlims[2], 0.05)

    ref_diffs <- test_vals - firstMean
    alt_diffs <- test_vals - secondMean
    scale_diffs <- test_vals - ((firstMean + secondMean) / 2)
    space_btwn <- (firstMean - secondMean)^2

    dat <- rbind.data.frame(
        data.frame(value=log(dnorm(test_vals, firstMean) / dnorm(test_vals, secondMean)), x=test_vals,
                   type='Log Likelihood Ratio'),
        data.frame(value=exp(-(scale_diffs^2) / (scaleFactor * constVar)) * ((alt_diffs)^2 - (ref_diffs)^2) /
                       (constVar * space_btwn^scalePower * scaleFactor2), x=test_vals,
                   type='Outlier-Robust LLR'),
        data.frame(value=dnorm(test_vals, firstMean, sqrt(constVar)), x=test_vals, type='Canonical Expected\nSignal Level'),
        data.frame(value=dnorm(test_vals, secondMean, sqrt(constVar)), x=test_vals, type='Alternative Expected\nSignal Level')
        )

    print(ggplot(dat) + geom_density(aes(x=x, y=value, fill=type), stat='identity', color='white', size=0, alpha=0.3) +
          theme_bw() + ggtitle(paste('Expected Signal Level Difference:', secondMean)) + ylim(-2,2) + xlim(xlims[1],xlims[2]))
}
foo <- dev.off()