tensorflow / probability

Probabilistic reasoning and statistical analysis in TensorFlow
https://www.tensorflow.org/probability/
Apache License 2.0
4.25k stars 1.1k forks source link

Unable to fit regression with positively constrained weights in tfp.sts #940

Closed TimKreienkamp closed 4 years ago

TimKreienkamp commented 4 years ago

Hi All,

I have a structural timeseries model, where, for business reasons, I would like to constrain the LinearRegression component to have positive coefficients. I tried to use the weights_prior argument in tfp.sts.LinearRegression to utilize prior distributions with only positive support, like tfd.Exponential() and tfd.HalfNormal():

tfp.sts.LinearRegression(design_matrix, name=name, weights_prior=tfd.Exponential(rate=1.0, validate_args=True))

but this causes the following error:

tensorflow.python.framework.errors_impl.InvalidArgumentError: assertion failed: [Sample must be non-negative.] [Condition x >= 0 did not hold element-wise:] [x (monte_carlo_variational_loss/build_factored_surrogate_posterior_JointDistributionNamed/sample/build_factored_surrogate_posterior_ctrl_static__weights_posterior/sample/Reshape:0) = ] [[0.930389822 -0.337865472]] [[{{node monte_carlo_variational_loss/expectation/ctrl_static_ctrl_static_identityExponential/log_prob/Exponential_2/log_prob/assert_non_negative/assert_less_equal/Assert/AssertGuard/else/_1/Assert}}]] [Op:__inference_train_loop_body_19396]

so somehow, negative samples are generated, despite the positive prior. Am I doing something wrong here?

davmre commented 4 years ago

Hi Tim, that's a great question. You've hit on a deficiency of the current implementation: priors with constrained support will often raise errors, because the inference algorithm doesn't 'know' about the constraint, so it will sometimes propose values that violate it.

I just submitted a patch that I think will fix this issue, by using the recently-added 'default bijector' property of the prior to determine a reasonable constraining transformation to apply: https://github.com/tensorflow/probability/commit/5e32e08d2ee77047665f5f4cfdabc34a578c80cf This should be included in tonight's tfp-nightly package--let me know if it helps with your issue!

Dave

TimKreienkamp commented 4 years ago

Works like a charm Dave, thank you so much for handling this so quickly!