Saturday, February 27, 2016

Doing Basic Calculus Using R!

Doing Basic Calculus Using R!
Differentiation Rules/Rules for Calculating Derivatives
  • Constant: f'(c) = 0, here c as a constant
  • Scalar Multiple: f'[cf(x)] = cf'(x)
  • Sum and Difference: [f(x) ± g(x)]' = f' (x) ± g' (x)
  • Product: [f(x) * g(x)]' = f'(x) * g(x) + f(x) * g'(x)
  • Quotient: [f(x) / g(x)]' = [g(x) * f'(x) - f(x) * g'(x)] / g(x)2
  • Power: f'(xn) = n * x(n-1)
  • Chain Rule: [f(g(x)]' = f'(g(x)) * g'(x)
  • Exponential: f'(ex) = ex                  Arbitrary base: f'(bx) = bx * lnb
  • Logarithmic: f'(ln|x|) = 1/x                  Arbitrary base: f'b(logx) = 1/(x lnb)
Calculating Derivative and Integration Using R!
  • R can symbolically find the derivative of any function by using the function D() with function expression(). R knows how to use the chain rule as well.
    • First derivative:     D(expression(x^2), "x") ===> 2 * x
    • Higher derivative: D(D(expression(x^2),"x"), "x") ===> 2
    • Partial derivative: D(expression((y-x)/y),"x") ===> -(1/y) and D(expression((y-x)/y),"y") ===> 1/y - (y - x)/y^2, which is equal to x/y^2
    • with the eval() function, you can get the value using particular values of its parameters: x =10; eval(D(expression(x^2), "x"))  ===> 20
    • D(expression(pnorm(x)),"x") ===> dnorm(x)
    • D(expression(dnorm(x)),"x") ===> -(x * dnorm(x))
  • R  can numerically perform one dimentsional integration using function integrate()
    • integrate(dnorm,-Inf,Inf) ===> 1 with absolute error < 9.4e-05
    • integrate(dnorm,-2.58,2.58) ===> 0.99012 with absolute error < 1.9e-08
    • integrate(function(x) {x^3 + x}, 0, 1) ===> 0.75 with absolute error < 8.3e-15
  • Other differentiation related R packages
    • Deriv is for symbolic differentiation.
    • Ryacas allows R users to access the yacas computer algebra system that does an excellent job of differentiation.
  • Use R to Compute Numerical Integrals
Online Calculators: 
Taylor's Series is a series expansion of a function near a point. A real function f(x) which is close to a point a can be estimated as:
  • f(x) =(f(n)(a)/n! * (x - a)n
  • If a = 0, the expansion is known as a Maclaurin series.
  • Mathematical Annotation to write math symbols and expressions in R graphics (cheat sheet). 

Wednesday, February 10, 2016

accept-reject algorithm

Accept-reject algorithm
Accept-reject algorithm (acceptance-rejection method) or reject sampling is a simple and general simulation method to decide observations with or without a trait from the probability of a distribution. In this way, we can convert a probability into a dichotomous condition (i.e. yes or no). Basically, there are three steps:
  • Step 1. Generate Y from density g [Y = f(x), the pdf of f(x) is the target distribution]
    • Sample a point (an x-position) from the proposal density distribution (g) and draw a vertical line at this point, get the density (an y-position) [X ~ g(x)]. The density function of Y has a upper, a constant c, and c is >=1.
  • Step 2. Generate U from the uniform distribution on the interval (0, cg(x)) [U = cg(x), the pdf of cg(x) is the proposal distribution]
    • Sample uniformly along in the range of x-position (i.e. uniformly from 0 to the maximum of the probability density function) [U ~ runif(0, 1)]
  • Step 3. If U <= Y, then set Y = X ("accept"), else repeat Steps 1 and 2
Pr(X|accept) = Pr(accept|X) x Pr(X)/Pr(accept), using Bayes' theorem
Pr(accept|X) = f(x)/cg(x)
Pr(X) = g(x)
Pr(accept) = 1/c
therefore, Pr(X|accept) = f(x)


Example: Stata simulation and define the event


clear
set seed 770488
set obs 1000

gen x = runiform() - .5
gen z = runiform() - .5
gen xb = x + 8*z

 gen y = 1 / (1 + exp(xb)) < runiform() // y defined as 0 or 1
logistic y x z