########################################################################## ########################################################################## # bivariate density: discussion section week 5 # contour and 3d perspective plots of the function # f( x, y ) = 5 * ( x^2 + y ) / 4 # in the region # 0 < y < 1 - x^2 n.grid <- 500 x.grid <- seq( -1, 1, length = n.grid ) y.grid <- seq( 0, 1, length = n.grid ) f.grid <- matrix( NA, n.grid, n.grid ) for ( i in 1:n.grid ) { for ( j in 1:n.grid ) { f.grid[ i, j ] <- ifelse( y.grid[ j ] < 1 - x.grid[ i ]^2, 5 * ( x.grid[ i ]^2 + y.grid[ j ] ) / 4, NA ) } } contour( x.grid, y.grid, f.grid, nlevels = 50, col = 'red', xlab = 'x', ylab = 'y' ) lines( x.grid, 1 - x.grid^2, lty = 2, lwd = 2, col = 'blue' ) library( plotly ) f.grid.t <- t( f.grid ) interactive.bivariate.density.perspective.plot <- plot_ly( x = x.grid, y = y.grid, z = f.grid.t ) %>% add_surface( contours = list( z = list( show = T, usecolormap = T, highlightcolor = '#ff0000', project = list( z = T ) ) ) ) %>% layout( title = 'Perspective plot of the bivariate density', scene = list( xaxis = list( title = 'x' ), yaxis = list( title = 'y' ), zaxis = list( title = 'f' ), camera = list( eye = list( x = 1.5, y = 0.5, z = -0.5 ) ) ) ) %>% colorbar( title = 'Bivariate Density' ) interactive.bivariate.density.perspective.plot ########################################################################## # bivariate density: extra notes page 98 # contour and 3d perspective plots of the function # f( x, y ) = 21 * ( x^2 * y ) / 4 # in the region # 0 < x^2 < y < 1 n.grid <- 500 x.grid <- seq( -1, 1, length = n.grid ) y.grid <- seq( 0, 1, length = n.grid ) f.grid <- matrix( NA, n.grid, n.grid ) for ( i in 1:n.grid ) { for ( j in 1:n.grid ) { f.grid[ i, j ] <- ifelse( x.grid[ i ]^2 < y.grid[ j ], 21 * x.grid[ i ]^2 * y.grid[ j ] / 4, NA ) } } contour( x.grid, y.grid, f.grid, nlevels = 50, col = 'red', xlab = 'x', ylab = 'y' ) lines( x.grid, x.grid^2, lty = 2, lwd = 2, col = 'blue' ) library( plotly ) f.grid.t <- t( f.grid ) interactive.bivariate.density.perspective.plot <- plot_ly( x = x.grid, y = y.grid, z = f.grid.t ) %>% add_surface( contours = list( z = list( show = T, usecolormap = T, highlightcolor = '#ff0000', project = list( z = T ) ) ) ) %>% layout( title = 'Perspective plot of the bivariate density', scene = list( xaxis = list( title = 'x' ), yaxis = list( title = 'y' ), zaxis = list( title = 'f' ), camera = list( eye = list( x = 1.5, y = 0.5, z = -0.5 ) ) ) ) %>% colorbar( title = 'Bivariate Density' ) interactive.bivariate.density.perspective.plot ########################################################################## # bivariate density: extra notes page 113 # contour and 3d perspective plots of the function # f( x, y ) = 24 * ( x^2 * y^2 ) / pi # in the region # 0 < x^2 + y^2 < 1 n.grid <- 500 x.grid <- seq( -1, 1, length = n.grid ) y.grid <- seq( -1, 1, length = n.grid ) f.grid <- matrix( NA, n.grid, n.grid ) for ( i in 1:n.grid ) { for ( j in 1:n.grid ) { f.grid[ i, j ] <- ifelse( x.grid[ i ]^2 + y.grid[ j ]^2 < 1, 24 * x.grid[ i ]^2 * y.grid[ j ]^2 / pi, NA ) } } contour( x.grid, y.grid, f.grid, nlevels = 50, col = 'red', xlab = 'x', ylab = 'y' ) lines( x.grid, sqrt( 1 - x.grid^2 ), lty = 2, lwd = 2, col = 'blue' ) lines( x.grid, - sqrt( 1 - x.grid^2 ), lty = 2, lwd = 2, col = 'blue' ) library( plotly ) f.grid.t <- t( f.grid ) interactive.bivariate.density.perspective.plot <- plot_ly( x = x.grid, y = y.grid, z = f.grid.t ) %>% add_surface( contours = list( z = list( show = T, usecolormap = T, highlightcolor = '#ff0000', project = list( z = T ) ) ) ) %>% layout( title = 'Perspective plot of the bivariate density', scene = list( xaxis = list( title = 'x' ), yaxis = list( title = 'y' ), zaxis = list( title = 'f' ), camera = list( eye = list( x = 1.5, y = 0.5, z = -0.5 ) ) ) ) %>% colorbar( title = 'Bivariate Density' ) interactive.bivariate.density.perspective.plot ########################################################################## # bivariate density extra notes page 114 # contour and 3d perspective plots of the function # f( x, y ) = 2 * exp( - ( x + 2 * y ) ) # in the region # 0 < x < 3 and 0 < y < 3 n.grid <- 500 x.grid <- seq( 0, 3, length = n.grid ) y.grid <- seq( 0, 3, length = n.grid ) f.grid <- matrix( NA, n.grid, n.grid ) for ( i in 1:n.grid ) { for ( j in 1:n.grid ) { f.grid[ i, j ] <- 2 * exp( - ( x.grid[ i ] + 2 * y.grid[ j ] ) ) } } contour( x.grid, y.grid, f.grid, nlevels = 50, col = 'red', xlab = 'x', ylab = 'y' ) library( plotly ) f.grid.t <- t( f.grid ) interactive.bivariate.density.perspective.plot <- plot_ly( x = x.grid, y = y.grid, z = f.grid.t ) %>% add_surface( contours = list( z = list( show = T, usecolormap = T, highlightcolor = '#ff0000', project = list( z = T ) ) ) ) %>% layout( title = 'Perspective plot of the bivariate density', scene = list( xaxis = list( title = 'x' ), yaxis = list( title = 'y' ), zaxis = list( title = 'f' ), camera = list( eye = list( x = 1.5, y = 0.5, z = -0.5 ) ) ) ) %>% colorbar( title = 'Bivariate Density' ) interactive.bivariate.density.perspective.plot ########################################################################## ##########################################################################