# How to define model depending on the output from the external program

**URL:** <https://discourse.pymc.io/t/how-to-define-model-depending-on-the-output-from-the-external-program/4605>\
**Category:** Questions\
**Created:** [March 3, 2020, 5:39pm UTC](https://discourse.pymc.io/t/how-to-define-model-depending-on-the-output-from-the-external-program/4605 "2020-03-03T17:39:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Konstantin\_Kuznetsov](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/konstantin_kuznetsov/32/2535_2.png) [@Konstantin\_Kuznetsov](https://discourse.pymc.io/u/Konstantin_Kuznetsov)\
**Post date:** [March 3, 2020, 5:39pm UTC](https://discourse.pymc.io/t/how-to-define-model-depending-on-the-output-from-the-external-program/4605/1 "2020-03-03T17:39:52Z")

</div>

Hello!

I would like to make a model like that:

```
with pm.Model() as linear_model:
   tx = pm.Uniform('tx', upper=70, lower=0)
   ty = pm.Uniform('ty', upper=70, lower=0)
   mu_sim = pm.MvNormal('mu_sim', 
                         mu=func0(tx,ty),
                         conv=R,
                         observed=mu)
    p = pm.sample()

```

It is simple but the problem that function func0 is calling some external program. (R and mu are numpy arrays) I have to transform variables tx and ty from stochastic to deterlministic to make it work, right? But I do not completelly understand how exactly it should work.  
Thanks

---

<div class="post-metadata">

**Author:** ![sammosummo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/sammosummo/32/96_2.png) [@sammosummo](https://discourse.pymc.io/u/sammosummo)\
**Post date:** [March 4, 2020, 12:59am UTC](https://discourse.pymc.io/t/how-to-define-model-depending-on-the-output-from-the-external-program/4605/2 "2020-03-04T00:59:49Z")

</div>

You need to make `func0` a theano op. What does it do exactly? If you post the code perhaps someone can help convert it.

---

<div class="post-metadata">

**Author:** ![Konstantin\_Kuznetsov](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/konstantin_kuznetsov/32/2535_2.png) [@Konstantin\_Kuznetsov](https://discourse.pymc.io/u/Konstantin_Kuznetsov)\
**Post date:** [March 4, 2020, 11:57am UTC](https://discourse.pymc.io/t/how-to-define-model-depending-on-the-output-from-the-external-program/4605/3 "2020-03-04T11:57:19Z")

</div>

Thanks for your reply. I googled about theano.Op and here I was trying to adapt one of the examples:

```
class Case(): 
.....
    def H(self, xs, ys, zs, qs):
        H = np.zeros([self.d, 1])
        for i, scalar in enumerate(self.adj_scalar_names):
            H[i] = qs * self.adj_solution.probe_real(scalar, xs, ys, zs);
        return H
....

case_2681829 = Case(mu_name = "probes_conc.csv",
                   mu_coord = "probes_coords.csv",
                   adj_sol_RESULT = 'RESULTS.case')

class theano_wrapper_H(tt.Op):
    itypes = [tt.dscalar]
    otypes = [tt.dscalar]

def perform(self, node, inputs, outputs):
    xs = inputs[0]
    ys = inputs[1]
    zs = inputs[2]
    qs = inputs[3]
    mu = case_2681829.H(xs, ys, zs, qs) 
    #mu should be a numpy array of length d
    outputs[0][0] = np.array(mu)

with pm.Model() as model:
    xs = pm.Uniform('theta_x', upper=70, lower=0)
    ys = pm.Uniform('theta_y', upper=105, lower=0)
    zs = pm.Uniform('theta_z', upper=7, lower=0)
    qs = pm.Uniform('theta_q', upper=200, lower=0)

mu_sim = pm.Deterministic('mu_sim',theano_wrapper_H(xs,ys,zs,qs));
res = pm.MvNormal('res',
                  mu=mu_sim,
                  cov=case_2681829.R,
                  observed=case_2681829.mu);
step = pm.Metropolis()
trace = pm.sample(10000, step)

```

My core function H require that arguments xs,ys,zs,qs are scalars, not vectors. Is this code should be ok? Thanks

---

<div class="post-metadata">

**Author:** ![sammosummo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/sammosummo/32/96_2.png) [@sammosummo](https://discourse.pymc.io/u/sammosummo)\
**Post date:** [March 4, 2020, 1:00pm UTC](https://discourse.pymc.io/t/how-to-define-model-depending-on-the-output-from-the-external-program/4605/4 "2020-03-04T13:00:44Z")

</div>

Besides the indentations, this looks reasonable.

---

<div class="post-metadata">

**Author:** ![Konstantin\_Kuznetsov](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/konstantin_kuznetsov/32/2535_2.png) [@Konstantin\_Kuznetsov](https://discourse.pymc.io/u/Konstantin_Kuznetsov)\
**Post date:** [March 5, 2020, 2:22pm UTC](https://discourse.pymc.io/t/how-to-define-model-depending-on-the-output-from-the-external-program/4605/5 "2020-03-05T14:22:10Z")

</div>

Thanks a lot for your help. This case works now. But I have the other issue: it is very slow it shows me 20 hours to calculate 4 chains for 1000 samples. The dimension of MvNormal is 48. Could you give me a possible ways to decreasing time of PyMC3 calculations?

---

<div class="post-metadata">

**Author:** ![sammosummo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/sammosummo/32/96_2.png) [@sammosummo](https://discourse.pymc.io/u/sammosummo)\
**Post date:** [March 5, 2020, 2:25pm UTC](https://discourse.pymc.io/t/how-to-define-model-depending-on-the-output-from-the-external-program/4605/6 "2020-03-05T14:25:24Z")

</div>

Probably the slowdown is the time it takes to evaluate `H`. If so, there’s nothing you can do except re-write `H` yourself, possibly in terms of native theano elements, to be more efficient.
