Custom likelihood function (again) with DensityDist and Theano function

Thanks, it works now thanks to your advice!
Additionally, I choose a HalfNormal prior on theta and everything works fine!
I modified the code like this to add a condition on the theta parameter.

import pymc3 as pm
import numpy as np
from matplotlib import pyplot as plt
import theano.tensor as T

def logp(theta, time, y):
    N = y.size
    yty = T.dot(y,y)
    if T.gt(theta, 0):
        f = (1 - T.exp(-time / theta))
        ytf = T.dot(y, f)
        ftf = T.dot(f, f)
        logp = -N / 2 * (yty - ytf ** 2 / ftf)
    else:
        logp = -N / 2 * yty

    return logp

if __name__ == '__main__':

    # Generate here noisy data
    S0=10
    sigma=0.1
    theta=3
    time=np.linspace(0,9,10)
    mu=S0*(1-np.exp(-time/theta))
    y=np.random.normal(mu, sigma)

    with pm.Model() as model:
        theta=pm.HalfNormal('theta', sd=100)
        likelihood=pm.DensityDist('Likelihood', logp, observed=dict(theta=theta,
                                                                    time=time,
                                                                    y=y))
        trace = pm.sample(10000, cores=2)

    pm.traceplot(trace)
    plt.show()

I have an additional question regarding the if statement, I’ve seen that there is a switch and ifelse function in theano (here). Would you recommend using it instead of what I implemented?

Thanks!