Black box code for computation of a mean

I have a black box code which returns the mean of a multivariate normal distribution. Here’s a working notebook for a small example of what I’m trying to do.

The problem is that when I replace cell 6 with a library call to my simulation code, I receive type errors, I think because my model wants a numpy input and it’s being passed a different type.

I read https://docs.pymc.io/notebooks/blackbox_external_likelihood.html, but it seems to be more than I need, since I don’t have a custom likelihood, just a custom way of computing the mean. I’d still like to use pm.Normal as the likelihood.

Substituting a more complicated model from a library import results in failure at declaration of mu, giving the following error:

You can find the C code in this temporary file: /var/folders/x4/gy7q6p_d2z5bwzbgppz6_l480000gn/T/theano_compilation_error_ykb8bzjm
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-9-00aab8136ec5> in <module>
      5 
      6     # Priors for unknown model parameters
----> 7     Zi = pm.Normal('theta', mu=0, sigma=1, shape=[N_obs, N_KL])
      8 
      9     # this is where we inject our model since Y = u(θ) + noise

~/opt/anaconda3/envs/6435_proj/lib/python3.7/site-packages/pymc3/distributions/distribution.py in __new__(cls, name, *args, **kwargs)
     45             total_size = kwargs.pop('total_size', None)
     46             dist = cls.dist(*args, **kwargs)
---> 47             return model.Var(name, dist, data, total_size)
     48         else:
     49             raise TypeError("Name needs to be a string but got: {}".format(name))

~/opt/anaconda3/envs/6435_proj/lib/python3.7/site-packages/pymc3/model.py in Var(self, name, dist, data, total_size)
    920                 with self:
    921                     var = FreeRV(name=name, distribution=dist,
--> 922                                  total_size=total_size, model=self)
    923                 self.free_RVs.append(var)
    924             else:

~/opt/anaconda3/envs/6435_proj/lib/python3.7/site-packages/pymc3/model.py in __init__(self, type, owner, index, name, distribution, total_size, model)
   1368             self.tag.test_value = np.ones(
   1369                 distribution.shape, distribution.dtype) * distribution.default()
-> 1370             self.logp_elemwiset = distribution.logp(self)
   1371             # The logp might need scaling in minibatches.
   1372             # This is done in `Factor`.

~/opt/anaconda3/envs/6435_proj/lib/python3.7/site-packages/pymc3/distributions/continuous.py in logp(self, value)
    516         mu = self.mu
    517 
--> 518         return bound((-tau * (value - mu)**2 + tt.log(tau / np.pi / 2.)) / 2.,
    519                      sigma > 0)
    520 

~/opt/anaconda3/envs/6435_proj/lib/python3.7/site-packages/theano/tensor/var.py in __sub__(self, other)
    145         # and the return value in that case
    146         try:
--> 147             return theano.tensor.basic.sub(self, other)
    148         except (NotImplementedError, AsTensorError):
    149             return NotImplemented

~/opt/anaconda3/envs/6435_proj/lib/python3.7/site-packages/theano/gof/op.py in __call__(self, *inputs, **kwargs)
    668                 # compute output value once with test inputs to validate graph
    669                 thunk = node.op.make_thunk(node, storage_map, compute_map,
--> 670                                            no_recycling=[])
    671                 thunk.inputs = [storage_map[v] for v in node.inputs]
    672                 thunk.outputs = [storage_map[v] for v in node.outputs]

~/opt/anaconda3/envs/6435_proj/lib/python3.7/site-packages/theano/gof/op.py in make_thunk(self, node, storage_map, compute_map, no_recycling, impl)
    953             try:
    954                 return self.make_c_thunk(node, storage_map, compute_map,
--> 955                                          no_recycling)
    956             except (NotImplementedError, utils.MethodNotDefined):
    957                 # We requested the c code, so don't catch the error.

~/opt/anaconda3/envs/6435_proj/lib/python3.7/site-packages/theano/gof/op.py in make_c_thunk(self, node, storage_map, compute_map, no_recycling)
    856         _logger.debug('Trying CLinker.make_thunk')
    857         outputs = cl.make_thunk(input_storage=node_input_storage,
--> 858                                 output_storage=node_output_storage)
    859         thunk, node_input_filters, node_output_filters = outputs
    860 

~/opt/anaconda3/envs/6435_proj/lib/python3.7/site-packages/theano/gof/cc.py in make_thunk(self, input_storage, output_storage, storage_map, keep_lock)
   1215         cthunk, module, in_storage, out_storage, error_storage = self.__compile__(
   1216             input_storage, output_storage, storage_map,
-> 1217             keep_lock=keep_lock)
   1218 
   1219         res = _CThunk(cthunk, init_tasks, tasks, error_storage, module)

~/opt/anaconda3/envs/6435_proj/lib/python3.7/site-packages/theano/gof/cc.py in __compile__(self, input_storage, output_storage, storage_map, keep_lock)
   1155                                             output_storage,
   1156                                             storage_map,
-> 1157                                             keep_lock=keep_lock)
   1158         return (thunk,
   1159                 module,

~/opt/anaconda3/envs/6435_proj/lib/python3.7/site-packages/theano/gof/cc.py in cthunk_factory(self, error_storage, in_storage, out_storage, storage_map, keep_lock)
   1622                 node.op.prepare_node(node, storage_map, None, 'c')
   1623             module = get_module_cache().module_from_key(
-> 1624                 key=key, lnk=self, keep_lock=keep_lock)
   1625 
   1626         vars = self.inputs + self.outputs + self.orphans

~/opt/anaconda3/envs/6435_proj/lib/python3.7/site-packages/theano/gof/cmodule.py in module_from_key(self, key, lnk, keep_lock)
   1187             try:
   1188                 location = dlimport_workdir(self.dirname)
-> 1189                 module = lnk.compile_cmodule(location)
   1190                 name = module.__file__
   1191                 assert name.startswith(location)

~/opt/anaconda3/envs/6435_proj/lib/python3.7/site-packages/theano/gof/cc.py in compile_cmodule(self, location)
   1525                 lib_dirs=self.lib_dirs(),
   1526                 libs=libs,
-> 1527                 preargs=preargs)
   1528         except Exception as e:
   1529             e.args += (str(self.fgraph),)

~/opt/anaconda3/envs/6435_proj/lib/python3.7/site-packages/theano/gof/cmodule.py in compile_str(module_name, src_code, location, include_dirs, lib_dirs, libs, preargs, py_module, hide_symbols)
   2409             # difficult to read.
   2410             raise Exception('Compilation failed (return status=%s): %s' %
-> 2411                             (status, compile_stderr.replace('\n', '. ')))
   2412         elif config.cmodule.compilation_warning and compile_stderr:
   2413             # Print errors just below the command line.

Exception: ("Compilation failed (return status=1): /Users/corbin/.theano/compiledir_Darwin-19.4.0-x86_64-i386-64bit-i386-3.7.7-64/tmpkoi5sfqo/mod.cpp:506:27: error: non-constant-expression cannot be narrowed from type 'npy_intp' (aka 'long') to 'int' in initializer list [-Wc++11-narrowing].     int init_totals[2] = {V1_n0, V3_n1};.                           ^~~~~. /Users/corbin/.theano/compiledir_Darwin-19.4.0-x86_64-i386-64bit-i386-3.7.7-64/tmpkoi5sfqo/mod.cpp:506:27: note: insert an explicit cast to silence this issue.     int init_totals[2] = {V1_n0, V3_n1};.                           ^~~~~.                           static_cast<int>( ). /Users/corbin/.theano/compiledir_Darwin-19.4.0-x86_64-i386-64bit-i386-3.7.7-64/tmpkoi5sfqo/mod.cpp:506:34: error: non-constant-expression cannot be narrowed from type 'npy_intp' (aka 'long') to 'int' in initializer list [-Wc++11-narrowing].     int init_totals[2] = {V1_n0, V3_n1};.                                  ^~~~~. /Users/corbin/.theano/compiledir_Darwin-19.4.0-x86_64-i386-64bit-i386-3.7.7-64/tmpkoi5sfqo/mod.cpp:506:34: note: insert an explicit cast to silence this issue.     int init_totals[2] = {V1_n0, V3_n1};.                                  ^~~~~.                                  static_cast<int>( ). /Users/corbin/.theano/compiledir_Darwin-19.4.0-x86_64-i386-64bit-i386-3.7.7-64/tmpkoi5sfqo/mod.cpp:518:12: error: non-constant-expression cannot be narrowed from type 'ssize_t' (aka 'long') to 'int' in initializer list [-Wc++11-narrowing].         0, V3_stride1, .            ^~~~~~~~~~. /Users/corbin/.theano/compiledir_Darwin-19.4.0-x86_64-i386-64bit-i386-3.7.7-64/tmpkoi5sfqo/mod.cpp:518:12: note: insert an explicit cast to silence this issue.         0, V3_stride1, .            ^~~~~~~~~~.            static_cast<int>( ). /Users/corbin/.theano/compiledir_Darwin-19.4.0-x86_64-i386-64bit-i386-3.7.7-64/tmpkoi5sfqo/mod.cpp:520:1: error: non-constant-expression cannot be narrowed from type 'ssize_t' (aka 'long') to 'int' in initializer list [-Wc++11-narrowing]. V1_stride0, V1_stride1. ^~~~~~~~~~. /Users/corbin/.theano/compiledir_Darwin-19.4.0-x86_64-i386-64bit-i386-3.7.7-64/tmpkoi5sfqo/mod.cpp:520:1: note: insert an explicit cast to silence this issue. V1_stride0, V1_stride1. ^~~~~~~~~~. static_cast<int>( ). /Users/corbin/.theano/compiledir_Darwin-19.4.0-x86_64-i386-64bit-i386-3.7.7-64/tmpkoi5sfqo/mod.cpp:520:13: error: non-constant-expression cannot be narrowed from type 'ssize_t' (aka 'long') to 'int' in initializer list [-Wc++11-narrowing]. V1_stride0, V1_stride1.             ^~~~~~~~~~. /Users/corbin/.theano/compiledir_Darwin-19.4.0-x86_64-i386-64bit-i386-3.7.7-64/tmpkoi5sfqo/mod.cpp:520:13: note: insert an explicit cast to silence this issue. V1_stride0, V1_stride1.             ^~~~~~~~~~.             static_cast<int>( ). 5 errors generated.. ", '[Elemwise{sub,no_inplace}(theta, <TensorType(float64, (True, True))>)]')

Is there a simple way to handle this?

This has been asked on stack overflow before. It seems like theano’s problem to compile graphs on OS X machines.