---
title: "Introduction to UniLindleyApprox: Bayesian Estimation via Lindley's Approximation"
author: "Shikhar Tyagi, Arvind Pandey, Bhupendra Singh, Vrijesh Tripathi"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Introduction to UniLindleyApprox: Bayesian Estimation via Lindley's Approximation}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

## Overview

The `UniLindleyApprox` package provides a generalized computational framework for performing Bayesian parameter point estimation using **Lindley's Approximation (1980)** for arbitrary univariate probability distributions under complete, censored, and truncated data.

Lindley's approximation evaluates posterior expectations of arbitrary smooth functions $g(\boldsymbol{\theta})$:
$$\mathrm{E}[g(\boldsymbol{\theta}) \mid \mathbf{x}] = \frac{\int g(\boldsymbol{\theta}) e^{\ell(\boldsymbol{\theta}) + \rho(\boldsymbol{\theta})} d\boldsymbol{\theta}}{\int e^{\ell(\boldsymbol{\theta}) + \rho(\boldsymbol{\theta})} d\boldsymbol{\theta}}$$
using Taylor series expansions around the posterior mode (MAP) or MLE.

## Features

- **No distribution-specific code**: Driven entirely via user-supplied PDF, CDF, survival, and log-prior functions.
- **23 Censoring & Truncation Schemes**: Complete, right, left, interval, random, progressive Type-II, hybrid, joint censoring, truncation, etc.
- **Multiple Loss Functions**: SELF, WSELF, MQSELF, PLF, ELF, LINEX, GELF, K-Loss, and custom loss functions.
- **Diagnostics & Model Comparison**: AIC, AICc, BIC, HQIC, CAIC, KIC, goodness-of-fit (KS, AD, CvM, Watson, Chi-sq), and residual analysis.

## Example: Exponential Distribution under Complete Data

```{r}
library(UniLindleyApprox)

# Define probability functions for Exponential(rate)
dexp_custom <- function(x, theta) dexp(x, rate = theta[1])
pexp_custom <- function(x, theta) pexp(x, rate = theta[1])
sexp_custom <- function(x, theta) 1 - pexp(x, rate = theta[1])

# Gamma prior for rate parameter
logprior <- function(theta) dgamma(theta[1], shape = 2, rate = 1, log = TRUE)

# Simulated data
set.seed(42)
x <- rexp(50, rate = 2)

# Fit model using Lindley's approximation
fit <- lindley_fit(
  data = x,
  pdf = dexp_custom,
  cdf = pexp_custom,
  survival = sexp_custom,
  log_prior = logprior,
  theta0 = c(1.5),
  scheme = "complete",
  loss = "SELF"
)

# Print results
print(fit)
summary(fit)
```

## Bayes Estimation Under Multiple Loss Functions

```{r}
# SELF Bayes estimate
b_self <- bayes_estimate(fit, loss = "SELF")

# LINEX Bayes estimate
b_linex <- bayes_estimate(fit, loss = "LINEX", a = 0.5)

# GELF Bayes estimate
b_gelf <- bayes_estimate(fit, loss = "GELF", q = 2)

cat("SELF Bayes Estimate: ", b_self, "\n")
cat("LINEX Bayes Estimate: ", b_linex, "\n")
cat("GELF Bayes Estimate: ", b_gelf, "\n")
```

## Diagnostics & Model Information Criteria

```{r}
cat("AIC: ", AIC(fit), "\n")
cat("BIC: ", BIC(fit), "\n")
cat("AICc: ", AICc(fit), "\n")
cat("HQIC: ", HQIC(fit), "\n")
```
