Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explore stan and R support for overlapping swindow and D #25

Open
seabbs opened this issue Sep 3, 2024 · 1 comment
Open

Explore stan and R support for overlapping swindow and D #25

seabbs opened this issue Sep 3, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@seabbs
Copy link
Contributor

seabbs commented Sep 3, 2024

An example that demonstrates functionality that I think should work is:

test_that(
  "rprimarycensoreddist is consistent with dprimarycensoreddist and
   pprimarycensoreddist when D is not cleanly divided by swindow",
  {
    n <- 10000
    pwindow <- 2
    swindow <- 3
    D <- 13
    rate <- 0.5

    samples <- rpcens(
      n, rexp, pwindow, swindow,
      D = D,
      rate = rate
    )

    # Check empirical mean and pmf
    empirical_pmf <- as.vector(table(samples) / n)
    empirical_mean <- mean(samples)
    empirical_sd <- sd(samples)

    # Theoretical calculations
    x_values <- seq(0, D, by = swindow)
    pmf <- dpcens(
      x_values, pexp, pwindow, swindow,
      D = D,
      rate = rate
    )
    theoretical_mean <- sum(x_values * pmf)
    theoretical_sd <- sqrt(sum((x_values - theoretical_mean)^2 * pmf))

    # Compare means and standard deviations
    expect_equal(empirical_mean, theoretical_mean, tolerance = 0.1)
    expect_equal(empirical_sd, theoretical_sd, tolerance = 0.1)

    # Compare empirical and theoretical PMFs
    expect_equal(empirical_pmf, pmf, tolerance = 0.1)

    # Check empirical CDF against theoretical CDF
    empirical_cdf <- ecdf(samples)(x_values)
    theoretical_cdf <- ppcens(
      x_values + swindow, pexp, pwindow, D,
      rate = rate
    )
    expect_equal(empirical_cdf, theoretical_cdf, tolerance = 0.02)

    # Ensure the last CDF value is close to 1
    expect_equal(theoretical_cdf[length(theoretical_cdf)], 1, tolerance = 1e-3)

    # Check that PMF sums to approximately 1
    expect_equal(sum(pmf), 1, tolerance = 1e-3)
  }
)

This issue is an extension of #23

On top of the above test passing we would also need an equivalent stan test to pass

@seabbs seabbs added the bug Something isn't working label Sep 3, 2024
@seabbs
Copy link
Contributor Author

seabbs commented Sep 13, 2024

My current solution to this is to warn the user and force them to resize the offending windows. We should likely improve the warnings for this and possibly offer some functionality to do it automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant