<!DOCTYPE html>
      <html>
      <head>
<meta charset='utf-8'/>
<script async='async' src='https://cdn.datacamp.com/dcl-react-dev.js.gz'></script>
<style>
.DCexercise .datacamp-exercise {
     border: 2px solid #3D678D;
     border-radius: 10px 10px 10px 10px !important;
     }

     *[class*="lm_"], .ace_gutter, textarea[class*="ace_"], .ace_scroller {
     background-color: #F0EFF0 !important;
     }

     div[class*="Editor-module__editor"], div[class*="dcl__Footer"] {
     background-color: #3D678D !important; 
     } 

     *[class*="lm_"] {
     border-radius: 10px 10px 0px 0px !important;
     }

     div[class*="dcl__Footer"] {
     border-radius: 0px 0px 10px 10px !important;
     }

     .lm_content {
     border-radius: 0px !important;
     }

     .lm_splitter {
     background-color: #3D678D !important; 
     width: 3px !important;
     }

     .lm_drag_handle {
     background: transparent !important;
     }

     div[class*="dcl__index-module"] {
     outline-color: transparent !important;
     border-radius: 10px !important;
     }

     button[class*="secondary-light"] {
     background-color: #FF0000;
     }
</style>
      </head>
      <body>
      <div data-datacamp-exercise data-lang='r' data-height =320><code data-type="pre-exercise-code">
    f <- function(x){(x/4)*exp(-x^2/8)}
	</code>
	
	<code data-type="sample-code">
  # define the function ex


  # compute the expected value of X


  # define the function ex2


  # compute the variance of X


	</code>
	
	<code data-type="solution">
  # define the function ex
  ex <- function(x){x*f(x)}

  # compute the expected value of X
  expected_value <- integrate(ex, 0, Inf)$value

  # define the function ex2
  ex2 <- function(x){x^2*f(x)}

  # compute the variance of X
  variance <- integrate(ex2, 0, Inf)$value - expected_value^2
	</code>
	
	<code data-type="sct">
  test_function_definition("ex",
                         function_test = {
                           test_expression_result("ex(1)")
                           test_expression_result("ex(4)")
                           test_expression_result("ex(10)")
                           test_expression_result("ex(100)")
                         })
  test_function("integrate", args = c("f", "lower", "upper"), eval = c(F, T, T)) 
  test_object("expected_value")
  test_function_definition("ex2",
                         function_test = {
                           test_expression_result("ex2(1)")
                           test_expression_result("ex2(4)")
                           test_expression_result("ex2(10)")
                           test_expression_result("ex2(100)")
                         })
  test_function("integrate", args = c("f", "lower", "upper"), eval = c(F, T, T))
  test_object("variance")
	</code></div>
      </body>
      </html>