---
title: "Importance Sampling Estimation of Generalized Process Capability Indices"
author: "Shikhar Tyagi"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Importance Sampling Estimation of Generalized Process Capability Indices}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(gpciImpSam)
```

## Introduction

The **`gpciImpSam`** package provides a generalized framework for parameter estimation and Generalized Process Capability Indices (GPCIs) under uncensored data using **Importance Sampling (ImpSam)**.

Supported capability indices include:
- $C_{py}$ (Yield ratio)
- $C_p, C_{pk}, C_{pu}, C_{pl}, C_{pm}, C_{pmk}$
- $C_{pTk}$ (Saha et al., 2019)
- $S_{pmk}$ (Dey & Saha, 2019)
- $C_{pc}$ (Saha et al., 2022)
- $CN_{pk}$ (Saha et al., 2018)
- $CN_{pmc}$ (Alotaibi et al., 2022)
- $CN_{pmkc}$ (Saha et al., 2024)
- $C_p(u, v)$ (Vännman's generalized family)

## Example: Importance Sampling Analysis with User Functions

In this example, we provide sample uncensored data and custom user PDF and CDF functions.

```{r example-fit}
set.seed(123)
# Simulate 50 observations from a Normal process
process_data <- rnorm(50, mean = 10, sd = 1.2)

# Fit GPCIs using Importance Sampling
fit <- gpci_impsam(
  data = process_data,
  pdf = function(x, mean = 0, sd = 1) dnorm(x, mean = mean, sd = sd),
  cdf = function(x, mean = 0, sd = 1) pnorm(x, mean = mean, sd = sd),
  chain_length = 500,
  burn_in = 100,
  thinning = 1,
  USL = 13.5,
  LSL = 6.5,
  target = 10
)

# Print diagnostic summary table
summary_df <- summary(fit)
knitr::kable(summary_df[, c("Index", "Point_Estimate", "Posterior_Mean", "Bias", "MSE", "Risk_Value", "HPD95_Lower", "HPD95_Upper", "Convergence_Prob")])
```

## Visualizing Posterior Distributions

```{r plot-density, fig.width = 6, fig.height = 4}
plot(fit, type = "density", index = "Cpy")
```
