Interactive Tutorial Question example using ShinyLive

Linear Function of a Random Variable

In Tutorial 2 Part 2 Qn1 1, we have a random varaible , \(X\), that is i.i.d. from a \(N\left( \mu_X,1 \right)\) distribution and another random variable, \(Y\) defined as \(Y=2+2X\).

It turns out \(Y\thicksim N(2+2\mu_X,4)\)

How did we get this?

In general (using the properites of Expectations as outlined in Lecture 2) if one i.i.d. random variable (i.e. \(Y\)) is a linear combination of another random variable, (i.e. \(X\)) such that

\[Y=a+bX\]
the mean of \(Y\) is \[\mu_Y=a+b_{\mu_X}\]
snf the variance of \(Y\) is \[\sigma_Y^2=b^2\sigma_X^2\]
in this question \(a=2\), \(b=2\) and \(\sigma_X^2=1\) \(\Rightarrow\) \[ \mu_Y=2+2_{\mu_X}\, \text{and}\, \sigma_Y^2=2^2 \times 1=4\]

Try this yourself for other linear combinations

Use the sliding bar to select \(\mu_X\) and then enter another linear transormation then look at the conditional distribution of \(Y\) in the last panel.
In the tutorial question you were asked to evaluate the distribution of \(Y\) for values of \(\mu_X=2,5,\) and \(10\); the distribution of \(Y\) should be \(N(6,4),N(12,4)\) and \(N(22,4)\) respectively. Replicate the above graphically. Then try it for another linear combination of \(X\), say \(Y=2+4X\) - what does the conditional distribution now look like - compare this to what you estimated above - what changes?

#| '!! shinylive warning !!': |
#|   shinylive does not work in self-contained HTML documents.
#|   Please set `embed-resources: false` in your metadata.
#| standalone: true
#| viewerHeight: 650

library(shiny)
library(bslib)

ui <- fluidPage(
  titlePanel("Linear Transformation of a Normally Distributed Random Variable"),
  h4(HTML("Assume X is i.i.d. distributed from a N(μ<sub>x</sub>, 1) distribution")),
  sidebarLayout(
    sidebarPanel(
      br(), br(),  # Adding vertical spacing for centering
      sliderInput("mu_X", HTML("Select &mu;<sub>x</sub>:"), min = 2, max = 10, value = 5, step = 1),
      textInput("custom_transform", "Enter Linear Transformation (e.g., 2 + 4*X):", "2 + 4*X"),
      br(), br(),br(), br()
    ),
    mainPanel(
      plotOutput("distPlotX"),
      plotOutput("distPlotY"),
      plotOutput("distPlotCustom")
    )
  )
)

server <- function(input, output) {
  output$distPlotX <- renderPlot({
    mu_X <- input$mu_X
    sd_X <- 1  # Given that X ~ N(μ_x, 1)
    x_vals <- seq(mu_X - 4 * sd_X, mu_X + 4 * sd_X, length.out = 100)
    x_density_vals <- dnorm(x_vals, mean = mu_X, sd = sd_X)
    
    # Plot X distribution
    plot(x_vals, x_density_vals, type = "l", lwd = 2, col = "green",
         xlab = "X Values", ylab = "Density",
         main = bquote("Distribution of X (" ~ mu[x] ~ "= " * .(mu_X) * ")"))
    
    # Clip abline within the range of the density plot
    lines(rep(mu_X, 2), c(0, max(x_density_vals)), col = "red", lwd = 2, lty = 2)
  })
  
  output$distPlotY <- renderPlot({
    mu_X <- input$mu_X
    mu_Y <- 2 + 2 * mu_X
    sd_Y <- 2  # Given variance of 4, so std dev is sqrt(4) = 2
    y_vals <- seq(mu_Y - 4 * sd_Y, mu_Y + 4 * sd_Y, length.out = 100)
    y_density_vals <- dnorm(y_vals, mean = mu_Y, sd = sd_Y)
    
    # Plot Y distribution
    plot(y_vals, y_density_vals, type = "l", lwd = 2, col = "blue",
         xlab = "Y Values", ylab = "Density",
         main = bquote("Conditional Distribution of Y (" ~ mu[x] ~ "= " * .(mu_X) * ")"))
    
    # Clip abline within the range of the density plot
    lines(rep(mu_Y, 2), c(0, max(y_density_vals)), col = "red", lwd = 2, lty = 2)
  })
  
  output$distPlotCustom <- renderPlot({
    mu_X <- input$mu_X
    
    # Parse user input for transformation
    transform_expr <- gsub("X", as.character(mu_X), input$custom_transform)
    mu_Custom <- eval(parse(text = transform_expr))
    
    # Assume same variance transformation as Y (scaling variance by b^2 if transformation is a + bX)
    sd_Custom <- 2  # Placeholder for variance scaling logic
    custom_vals <- seq(mu_Custom - 4 * sd_Custom, mu_Custom + 4 * sd_Custom, length.out = 100)
    custom_density_vals <- dnorm(custom_vals, mean = mu_Custom, sd = sd_Custom)
    
    # Plot Custom transformation distribution
    plot(custom_vals, custom_density_vals, type = "l", lwd = 2, col = "purple",
         xlab = "Transformed Values", ylab = "Density",
         main = bquote("Conditional Distribution of " ~ .(input$custom_transform) ~ "(" ~ mu[x] ~ "= " * .(mu_X) * ")"))
    
    # Clip abline within the range of the density plot
    lines(rep(mu_Custom, 2), c(0, max(custom_density_vals)), col = "red", lwd = 2, lty = 2)
  })
}

shinyApp(ui = ui, server = server)

Another?