R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

Derivation of the Value of \(a\)

Let \(X\) be a continuous random variable denoting the time between the product being sold out and placed back on the shelf. The probability density function (PDF) of \(X\) is given by:

\[ p_\lambda(x) = \begin{cases} e^{-\lambda (x-b)}, & x \geq b \\ 0, & x < b \end{cases} \]

We need to determine the value of \(a\). To do this, we use the normalization condition for the probability density function, which states that the total probability must equal 1:

\[ \int_{-\infty}^\infty p_\lambda(x) \, dx = 1 \]

Since the PDF is 0 for \(x < b\), the limits of integration are adjusted to \(b\) to \(\infty\):

\[ \int_b^\infty a e^{-\lambda (x-b)} \, dx = 1 \]

Step 1: Simplify the Exponent

Factor out \(-\lambda b\) from the exponent:

\[ \int_b^\infty a e^{-\lambda x} e^{\lambda b} \, dx = 1 \]

This can be rewritten as:

\[ a e^{\lambda b} \int_b^\infty e^{-\lambda x} \, dx = 1 \]

This can be rewritten as:

\[ a e^{\lambda b} \int_b^\infty e^{-\lambda x} \, dx = 1 \]

Step 2: Solve the Integral

The integral of the exponential function \(e^{-\lambda x}\) is well known. Adjusting the limits:

\[ \int_b^\infty e^{-\lambda x} \, dx = \frac{e^{-\lambda b}}{\lambda} \]

Substitute this result back:

\[ a e^{\lambda b} \cdot \frac{e^{-\lambda b}}{\lambda} = 1 \]

Step 3: Simplify the Expression

The terms \(e^{\lambda b}\) and \(e^{-\lambda b}\) cancel out:

\[ a \cdot \frac{1}{\lambda} = 1 \]

Step 4: Solve for \(a\)

Multiply through by \(\lambda\):

\[ a = \lambda \]

Conclusion

Thus, the value of \(a\) is:

\[ a = \lambda \]

Derivation of Mean and Variance

Let \(p_\lambda(x)\) be the PDF of a shifted exponential distribution:

\[ p_\lambda(x) = \begin{cases} \lambda e^{-\lambda (x - b)} & \text{if } x \geq b, \\ 0 & \text{if } x < b. \end{cases} \]

1. Population Mean (\(\mu_X\))

The population mean \(\mu_X\) is given by:

\[ \mu_X = \mathbb{E}[X] = \int_{-\infty}^\infty x \cdot p_\lambda(x) \, dx \]

Since \(p_\lambda(x) = 0\) for \(x < b\), the integration limits reduce to \([b, \infty)\):

\[ \mu_X = \int_b^\infty x \cdot \lambda e^{-\lambda (x - b)} \, dx \]

Simplify \(-\lambda (x - b)\) as \(-\lambda x + \lambda b\):

\[ \mu_X = \int_b^\infty x \cdot \lambda e^{\lambda b} e^{-\lambda x} \, dx \]

Factor out constants \(\lambda e^{\lambda b}\):

\[ \mu_X = \lambda e^{\lambda b} \int_b^\infty x e^{-\lambda x} \, dx \]

Perform a substitution: let \(u = x - b\), so \(x = u + b\) and \(dx = du\). The integration limits change to \(u = 0\) and \(u \to \infty\):

\[ \mu_X = \lambda e^{\lambda b} \int_0^\infty (u + b) e^{-\lambda (u + b)} \, du \]

Expand the terms \(u + b\) in the integral:

\[ \mu_X = \lambda e^{\lambda b} \left( \int_0^\infty u e^{-\lambda u} e^{-\lambda b} \, du + \int_0^\infty b e^{-\lambda u} e^{-\lambda b} \, du \right) \]

Combine \(e^{-\lambda b}\):

\[ \mu_X = \lambda \left( \int_0^\infty u e^{-\lambda u} \, du + b \int_0^\infty e^{-\lambda u} \, du \right) \]

Solve the integrals

  1. First integral: \(\int_0^\infty u e^{-\lambda u} \, du\)

Using the formula for the expected value of \(X\) for an exponential distribution: \[ \int_0^\infty u e^{-\lambda u} \, du = \frac{1}{\lambda^2} \]

  1. Second integral: \(\int_0^\infty e^{-\lambda u} \, du\)

Using the exponential integral: \[ \int_0^\infty e^{-\lambda u} \, du = \frac{1}{\lambda} \]

Substitute these results into the expression for \(\mu_X\):

\[ \mu_X = \lambda \left( \frac{1}{\lambda^2} + b \cdot \frac{1}{\lambda} \right) \]

Simplify:

\[ \mu_X = \frac{1}{\lambda} + b \]

Thus, the mean is:

\[ \mu_X = b + \frac{1}{\lambda} \]


2. Population Variance (\(\sigma_X^2\))

The variance is given by:

\[ \sigma_X^2 = \mathbb{E}[X^2] - (\mathbb{E}[X])^2 \]

Step 1: Compute \(\mathbb{E}[X^2]\)

The second moment is:

\[ \mathbb{E}[X^2] = \int_{-\infty}^\infty x^2 \cdot p_\lambda(x) \, dx \]

Again, limits reduce to \([b, \infty)\):

\[ \mathbb{E}[X^2] = \int_b^\infty x^2 \cdot \lambda e^{-\lambda (x - b)} \, dx \]

Substitute \(u = x - b\), \(x = u + b\), and \(dx = du\):

\[ \mathbb{E}[X^2] = \lambda e^{\lambda b} \int_0^\infty (u + b)^2 e^{-\lambda (u + b)} \, du \]

Expand \((u + b)^2\):

\[ \mathbb{E}[X^2] = \lambda e^{\lambda b} \int_0^\infty \left( u^2 + 2ub + b^2 \right) e^{-\lambda (u + b)} \, du \]

Distribute the terms:

\[ \mathbb{E}[X^2] = \lambda \left( \int_0^\infty u^2 e^{-\lambda u} \, du + 2b \int_0^\infty u e^{-\lambda u} \, du + b^2 \int_0^\infty e^{-\lambda u} \, du \right) \]

Solve the integrals

  1. First integral: \(\int_0^\infty u^2 e^{-\lambda u} \, du\)

Using the formula for the second moment of \(X\) in an exponential distribution: \[ \int_0^\infty u^2 e^{-\lambda u} \, du = \frac{2}{\lambda^3} \]

  1. Second integral: \(\int_0^\infty u e^{-\lambda u} \, du = \frac{1}{\lambda^2}\)
  2. Third integral: \(\int_0^\infty e^{-\lambda u} \, du = \frac{1}{\lambda}\)

Substitute these results:

\[ \mathbb{E}[X^2] = \lambda \left( \frac{2}{\lambda^3} + 2b \cdot \frac{1}{\lambda^2} + b^2 \cdot \frac{1}{\lambda} \right) \]

Simplify:

\[ \mathbb{E}[X^2] = \frac{2}{\lambda^2} + \frac{2b}{\lambda} + b^2 \]

Step 2: Variance

Substitute \(\mathbb{E}[X^2]\) and \((\mathbb{E}[X])^2\) into the variance formula:

\[ \sigma_X^2 = \mathbb{E}[X^2] - (\mathbb{E}[X])^2 \]

We know:

\[ \mathbb{E}[X] = b + \frac{1}{\lambda}, \quad (\mathbb{E}[X])^2 = \left( b + \frac{1}{\lambda} \right)^2 \]

Substitute and simplify:

\[ \sigma_X^2 = \frac{2}{\lambda^2} + \frac{2b}{\lambda} + b^2 - \left( b^2 + \frac{2b}{\lambda} + \frac{1}{\lambda^2} \right) \]

Cancel terms:

\[ \sigma_X^2 = \frac{1}{\lambda^2} \]

Thus, the variance is:

\[ \sigma_X^2 = \frac{1}{\lambda^2} \]


Final Results

  • Mean: \(\mu_X = b + \frac{1}{\lambda}\)
  • Variance: \(\sigma_X^2 = \frac{1}{\lambda^2}\)

Problem Description

We are given \(X_1, X_2, \dots, X_n\), independent samples from an exponential distribution with a rate parameter \(\lambda > 0\) and a location parameter \(b\). The probability density function (PDF) is:

\[ p_\lambda(x) = \begin{cases} \lambda e^{-\lambda (x - b)} & \text{if } x \geq b, \\ 0 & \text{if } x < b. \end{cases} \]

Our goal is to derive the Maximum Likelihood Estimate (MLE) for \(\lambda\), and compute it using R for a given dataset.


Derivation of the MLE for \(\lambda\)

Step 1: Define the Likelihood Function

The likelihood function is the joint probability of observing \(X_1, X_2, \dots, X_n\), given \(\lambda\):

\[ L(\lambda) = \prod_{i=1}^n p_\lambda(X_i). \]

Substituting the PDF:

\[ L(\lambda) = \prod_{i=1}^n \lambda e^{-\lambda (X_i - b)} \quad \text{(for \( X_i \geq b \))}. \]

Simplify the product:

\[ L(\lambda) = \lambda^n \cdot e^{-\lambda \sum_{i=1}^n (X_i - b)}. \]

Step 2: Log-Likelihood Function

Taking the natural logarithm of the likelihood function:

\[ \ell(\lambda) = \ln L(\lambda) = \ln(\lambda^n) + \ln\left(e^{-\lambda \sum_{i=1}^n (X_i - b)}\right). \]

Using logarithmic properties:

\[ \ell(\lambda) = n \ln(\lambda) - \lambda \sum_{i=1}^n (X_i - b). \]

Step 3: Differentiate the Log-Likelihood

To maximize \(\ell(\lambda)\), we take its derivative with respect to \(\lambda\):

\[ \frac{d\ell(\lambda)}{d\lambda} = \frac{d}{d\lambda} \left( n \ln(\lambda) - \lambda \sum_{i=1}^n (X_i - b) \right). \]

Differentiating term by term:

  • The derivative of \(n \ln(\lambda)\) is \(\frac{n}{\lambda}\),
  • The derivative of \(-\lambda \sum_{i=1}^n (X_i - b)\) is \(-\sum_{i=1}^n (X_i - b)\).

Thus:

\[ \frac{d\ell(\lambda)}{d\lambda} = \frac{n}{\lambda} - \sum_{i=1}^n (X_i - b). \]

Step 4: Solve for \(\lambda\)

Setting \(\frac{d\ell(\lambda)}{d\lambda} = 0\):

\[ \frac{n}{\lambda} = \sum_{i=1}^n (X_i - b). \]

Rearranging:

\[ \lambda = \frac{n}{\sum_{i=1}^n (X_i - b)}. \]

Thus, the MLE for \(\lambda\) is:

\[ \hat{\lambda} = \frac{n}{\sum_{i=1}^n (X_i - b)}. \]

question 5 : We aim to calculate the Maximum Likelihood Estimate (MLE) of \(\lambda\) for the given dataset. The formula is:

\[ \lambda_{\text{MLE}} = \frac{n}{\sum_{i=1}^n (X_i - b)}, \]

where:

  • \(b = 300\) seconds is a known constant.
  • \(n\) is the total number of observations in the dataset.
  • \(X_i\) represents the observed times in the dataset.

Calculation

The following R code calculates \(\lambda_{\text{MLE}}\):

# Load the dataset
supermarket_df <- data.frame(read.csv("supermarket_data_2024(1).csv"))

# Known constant
b <- 300

# Step 1: Calculate n and the sum
n <- length(supermarket_df$TimeLength)  # Number of observations
sum_x <- sum(supermarket_df$TimeLength - b)  # Sum of adjusted observations

# Step 2: Calculate MLE for lambda
lambda_MLE <- n / sum_x

# Step 3: Print the result
lambda_MLE
## [1] 0.01988426
# Step 1: Bootstrap Resampling and MLE Calculation
set.seed(123)  # For reproducibility
B <- 10000  # Number of bootstrap resamples

bootstrap_lambdas <- replicate(B, {
  bootstrap_sample <- sample(supermarket_df$TimeLength, size = n, replace = TRUE)
  sum_x <- sum(bootstrap_sample - b)
  n / sum_x  # Lambda MLE for the bootstrap sample
})

# Step 2: Compute the 95% Confidence Interval
ci <- quantile(bootstrap_lambdas, c(0.025, 0.975))

# Step 3: Print the Confidence Interval
cat("95% Bootstrap Confidence Interval for lambda:\n")
## 95% Bootstrap Confidence Interval for lambda:
cat(sprintf("Lower Bound: %.4f, Upper Bound: %.4f\n", ci[1], ci[2]))
## Lower Bound: 0.0191, Upper Bound: 0.0207