Exemplo n.º 1
0
    def pdf(cls, parameters: Dict[str, Tensor], data: Tensor) -> Tensor:
        params = cls.getParameters(parameters=parameters)
        b, cenNnElasticnetParams, exponentialParams, lomaxParams = params

        pdfLomax = LomaxAlgorithms.pdf(lomaxParams, data)
        pdfExponential = ExponentialAlgorithms.pdf(exponentialParams, data)
        pdfElasticNet = CenNnElasticNetAlgorithms.pdf(cenNnElasticnetParams,
                                                      data)
        b = b[None] * tf.ones_like(pdfElasticNet)
        pdf = tf.where(tf.equal(b, 0.), pdfElasticNet,
                       tf.where(tf.equal(b, 1.), pdfExponential, pdfLomax))
        return (pdf)
Exemplo n.º 2
0
    def llh(cls, parameters: Dict[str, Tensor], data: tf.Tensor) -> float:
        params = cls.getParameters(parameters=parameters)
        b, cenNnElasticnetParams, exponentialParams, lomaxParams = params

        llhLomax = LomaxAlgorithms.llh(lomaxParams, data)
        llhExponential = ExponentialAlgorithms.llh(exponentialParams, data)
        llhElasticNet = CenNnElasticNetAlgorithms.llh(cenNnElasticnetParams,
                                                      data)
        b = b[None] * tf.ones_like(llhElasticNet)
        llh = tf.where(tf.equal(b, 0.), llhElasticNet,
                       tf.where(tf.equal(b, 1.), llhExponential, llhLomax))
        return (llh)
def test_exponential_mode():
    """Test if the mode is 0 for all elements."""
    beta = np.array([0.5, 1., 2.])

    nBetas = beta.shape[0]
    parameters = {"beta": tf.constant(beta)}

    mode = ExponentialAlgorithms.mode(parameters=parameters)

    with tf.Session() as sess:
        mode = sess.run(mode)

    assert (mode.shape == (nBetas, ))
    assert (np.all(mode == 0.))
Exemplo n.º 4
0
    def sample(cls, parameters: Dict[str, Tensor], nSamples: Tensor) -> Tensor:
        params = cls.getParameters(parameters=parameters)
        b, cenNnElasticnetParams, exponentialParams, lomaxParams = params

        rLomax = LomaxAlgorithms.sample(lomaxParams, nSamples)
        rExponential = ExponentialAlgorithms.sample(exponentialParams,
                                                    nSamples)
        rElasticNet = CenNnElasticNetAlgorithms.sample(cenNnElasticnetParams,
                                                       nSamples)
        b = b * tf.ones_like(rLomax)
        r = tf.where(tf.equal(b, 0.), rElasticNet,
                     tf.where(tf.equal(b, 1.), rExponential, rLomax))

        return (r)
Exemplo n.º 5
0
    def fit(cls, parameters: Dict[str, Tensor],
            data: tf.Tensor) -> Dict[str, Tensor]:
        params = cls.getParameters(parameters=parameters)
        b, cenNnElasticnetParams, exponentialParams, lomaxParams = params

        cenNnElasticnetParams = CenNnElasticNetAlgorithms.fit(
            cenNnElasticnetParams, data)
        exponentialParams = ExponentialAlgorithms.fit(exponentialParams, data)
        lomaxParams = LomaxAlgorithms.fit(lomaxParams, data)

        cenNnElasticnetLlh = CenNnElasticNetAlgorithms.llh(
            cenNnElasticnetParams, data)
        cenNnElasticnetLlh = tf.reduce_mean(cenNnElasticnetLlh, axis=0)
        exponentialLlh = ExponentialAlgorithms.llh(exponentialParams, data)
        exponentialLlh = tf.reduce_mean(exponentialLlh, axis=0)
        lomaxLlh = LomaxAlgorithms.llh(lomaxParams, data)
        lomaxLlh = tf.reduce_mean(lomaxLlh, axis=0)

        condElasticNet = tf.logical_and(cenNnElasticnetLlh > lomaxLlh,
                                        cenNnElasticnetLlh > exponentialLlh)
        condExponential = exponentialLlh > lomaxLlh

        b = tf.where(
            condElasticNet, tf.zeros_like(cenNnElasticnetLlh),
            tf.where(condExponential, tf.ones_like(exponentialLlh),
                     2. * tf.ones_like(lomaxLlh)))

        updatedParameters = {
            "b": b,
            "mu": cenNnElasticnetParams["mu"],
            "tau": cenNnElasticnetParams["tau"],
            "betaExponential": exponentialParams["beta"],
            "alpha": lomaxParams["alpha"],
            "beta": lomaxParams["beta"],
            "tauLomax": lomaxParams["tau"]
        }
        return (updatedParameters)
def test_exponential_llh():
    """Test if the llh is the same as reported by scipy."""
    beta = np.array([0.5, 1., 2.])
    nSamples = 1000

    nBetas = beta.shape[0]
    data = np.random.random((nSamples, nBetas))

    parameters = {"beta": tf.constant(beta)}
    tfData = tf.constant(data)
    llh = ExponentialAlgorithms.llh(parameters=parameters, data=tfData)

    with tf.Session() as sess:
        llh = sess.run(llh)

    assert (llh.shape == (nSamples, nBetas))
    spLlh = sp.stats.expon(scale=beta).logpdf(data)
    assert (np.allclose(llh, spLlh))
def test_exponential_pdf():
    """Test if the pdf is the same as reported by scipy."""
    beta = np.array([0.5, 1., 2.])
    nSamples = 1000

    nBetas = beta.shape[0]
    data = np.random.random((nSamples, nBetas))

    parameters = {"beta": tf.constant(beta)}
    tfData = tf.constant(data)
    probs = ExponentialAlgorithms.pdf(parameters=parameters, data=tfData)

    with tf.Session() as sess:
        probs = sess.run(probs)

    assert (probs.shape == (nSamples, nBetas))
    spProbs = sp.stats.expon(scale=beta).pdf(data)
    assert (np.allclose(probs, spProbs))
def test_exponential_sample():
    """Test if the mean of the samples equals the scale `beta`."""
    beta = np.array([0.5, 1., 2.])
    nSamples = 1000000

    nBetas = beta.shape[0]
    parameters = {"beta": tf.constant(beta)}
    tfNSamples = tf.constant(nSamples)

    r = ExponentialAlgorithms.sample(parameters=parameters,
                                     nSamples=tfNSamples)

    with tf.Session() as sess:
        r = sess.run(r)

    assert (r.shape == (nSamples, nBetas))
    betaHat = np.mean(r, axis=0)
    assert (np.allclose(betaHat, beta, atol=1e-1))
def test_exponential_fit():
    """Test if the fitted parameters match the true parameters."""
    beta = np.array([0.5, 1., 2.])
    nSamples = 100000
    nBetas = beta.shape[0]
    data = np.random.random((nSamples, nBetas))
    data = sp.stats.expon(scale=beta).rvs(size=(nSamples, nBetas))

    parameters = {"beta": tf.constant(np.ones(nBetas))}
    tfData = tf.constant(data)
    parameters = ExponentialAlgorithms.fit(parameters=parameters, data=tfData)

    with tf.Session() as sess:
        parameters = sess.run(parameters)

    betaHat = parameters["beta"]

    assert (betaHat.shape == beta.shape)
    assert (np.allclose(betaHat, beta, atol=1e-1))