# Path: autograd/extend.py

# 
# Path: autograd/numpy/numpy_vjps.py
# def unbroadcast_f(target, f):
#     target_meta = anp.metadata(target)
#     return lambda g: unbroadcast(f(g), target_meta)
# 
# Path: autograd/scipy/special.py
# def make_gammainc_vjp_arg1(sign):
#     def gammainc_vjp_arg1(ans, a, x):
# def make_grad_logsumexp(ans, x, axis=None, b=1.0, keepdims=False):
#     def vjp(g):
# def fwd_grad_logsumexp(g, ans, x, axis=None, b=1.0, keepdims=False):


import autograd.numpy as np
import scipy.stats
from autograd.extend import primitive, defvjp
from autograd.numpy.numpy_vjps import unbroadcast_f
from autograd.scipy.special import beta, psi
from __future__ import absolute_import


cdf = primitive(scipy.stats.beta.cdf)
logpdf = primitive(scipy.stats.beta.logpdf)
pdf = primitive(scipy.stats.beta.pdf)

def grad_beta_logpdf_arg0(x, a, b):
    return (1 + a * (x-1) + x * (b-2)) / (x * (x-1))

def grad_beta_logpdf_arg1(x, a, b):
    return np.log(x) - psi(a) + psi(a + b)

def grad_beta_logpdf_arg2(x, a, b):
    return np.log1p(-x) - psi(b) + psi(a + b)


# Path: autograd/extend.py

# 
# Path: autograd/numpy/numpy_vjps.py
# def unbroadcast_f(target, f):
#     target_meta = anp.metadata(target)
#     return lambda g: unbroadcast(f(g), target_meta)
# 
# Path: autograd/scipy/special.py
# def make_gammainc_vjp_arg1(sign):
#     def gammainc_vjp_arg1(ans, a, x):
# def make_grad_logsumexp(ans, x, axis=None, b=1.0, keepdims=False):
#     def vjp(g):
# def fwd_grad_logsumexp(g, ans, x, axis=None, b=1.0, keepdims=False):


# Path: autograd/scipy/stats/beta.py
import autograd.numpy as np
import scipy.stats
from autograd.extend import primitive, defvjp
from autograd.numpy.numpy_vjps import unbroadcast_f
from autograd.scipy.special import beta, psi

from __future__ import absolute_import


cdf = primitive(scipy.stats.beta.cdf)
logpdf = primitive(scipy.stats.beta.logpdf)
pdf = primitive(scipy.stats.beta.pdf)

def grad_beta_logpdf_arg0(x, a, b):
    return (1 + a * (x-1) + x * (b-2)) / (x * (x-1))

def grad_beta_logpdf_arg1(x, a, b):
    return np.log(x) - psi(a) + psi(a + b)

def grad_beta_logpdf_arg2(x, a, b):
    return np.log1p(-x) - psi(b) + psi(a + b)


defvjp(cdf, lambda ans, x, a, b: unbroadcast_f(x, lambda g: g * np.power(x, a-1) * np.power(1-x, b-1) / beta(a, b)), argnums=[0])