# bivariate density: extra notes page 98 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 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 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