Closed fsaad closed 6 years ago
The following decorator will suffice for pure functions, should test it against methods.
def simulate_many(simulate):
def simulate_wrapper(*args, **kwargs):
N = kwargs.get('N', None)
if N is None:
return simulate(*args, **kwargs)
return [simulate(*args, **kwargs) for _i in xrange(N)]
return simulate_wrapper
Another question worth asking is, if this pattern is indeed the common case, whether simulate requires an N parameter in the first place. My sense is yes, since some CGPMs will be able to meaningfully optimize sampling N iid in a single simulate call.
This decorator will not work for positional arguments. Should instead check if the length of args is equal to the number of supported parameters from CGPM simulate and retrieving the last one.
For CGPMs which cannot generate N independent samples in a particularly special way other than calling
simulate
N times, a common pattern in thesimulate
withN not None
is to call itself N times.Example: https://github.com/probcomp/cgpm/blob/9237e19a7d4523a1984f4380f77d55ba7ac4cbdf/src/primitives/normal.py#L84-L86
This code should wrapped in decorator.