WillianFuks / tfcausalimpact

Python Causal Impact Implementation Based on Google's R Package. Built using TensorFlow Probability.
Apache License 2.0
610 stars 72 forks source link

is there a way to check the coefficient of regression part ? #69

Closed Berlyli866 closed 1 year ago

Berlyli866 commented 1 year ago

Is there a way we can check the Beta of the regressors we have in the model? if we have multiple regressors in the model and want to see the contribution of each regressor to the accumulative impact, how do we process it? the idea is to see what regressors' importance rank and how much the impact is for each regressor for a significant intervention effect on y

thanks for your insights.

WillianFuks commented 1 year ago

Hi @Berlyli866 ,

Yes, there is. Please refer to the getting_started.ipnb on section "2.5 Understanding Results", there's some discussion about this subject.

As a reference, here's an example of getting the average weights when the optimization technique is default variational inference:

tf.reduce_mean(ci.model.components_by_name['SparseLinearRegression/'].params_to_weights(
    ci.model_samples['SparseLinearRegression/_global_scale_variance'],
    ci.model_samples['SparseLinearRegression/_global_scale_noncentered'],
    ci.model_samples['SparseLinearRegression/_local_scale_variances'],
    ci.model_samples['SparseLinearRegression/_local_scales_noncentered'],
    ci.model_samples['SparseLinearRegression/_weights_noncentered'],
), axis=0)

I think TFP already offers some functions for performing this operation but couldn't update the notebook so far.

Let me know if this helps you.

Berlyli866 commented 1 year ago

Hey @WillianFuks , i used same code and got error

ci.model.components_by_name
tf.reduce_mean(ci.model.components_by_name['SparseLinearRegression/'].params_to_weights(
    ci.model_samples['SparseLinearRegression/_global_scale_variance'],
    ci.model_samples['SparseLinearRegression/_global_scale_noncentered'],
    ci.model_samples['SparseLinearRegression/_local_scale_variances'],
    ci.model_samples['SparseLinearRegression/_local_scales_noncentered'],
    ci.model_samples['SparseLinearRegression/_weights_noncentered'],
), axis=0)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_12815/3091180208.py in <module>
      1 tf.reduce_mean(ci.model.components_by_name['SparseLinearRegression/'].params_to_weights(
----> 2     ci.model_samples['SparseLinearRegression/_global_scale_variance'],
      3     ci.model_samples['SparseLinearRegression/_global_scale_noncentered'],
      4     ci.model_samples['SparseLinearRegression/_local_scale_variances'],
      5     ci.model_samples['SparseLinearRegression/_local_scales_noncentered'],

TypeError: list indices must be integers or slices, not str

i checked the ci.model_samples it's a list of array instead of a dictionary. do you know how to know the order of ci.model_samples['SparseLinearRegression/_global_scale_variance'], ci.model_samples['SparseLinearRegression/_global_scale_noncentered'], ci.model_samples['SparseLinearRegression/_local_scale_variances'], ci.model_samples['SparseLinearRegression/_local_scales_noncentered'], ci.model_samples['SparseLinearRegression/_weights_noncentered'], in the array ? the ci.model_samples total has 7 elements

Screenshot 2023-01-12 at 11 06 55
WillianFuks commented 1 year ago

Looks like you optimized using hmc, in this case use the code discussed in the next cell:

def get_param_index(model, name):
    for i, v in enumerate(model.parameters):
        if v.name == name:
            return i

tf.reduce_mean(ci.model.components_by_name['SparseLinearRegression/'].params_to_weights(
    ci.model_samples[get_param_index(ci.model, 'SparseLinearRegression/_global_scale_variance')],
    ci.model_samples[get_param_index(ci.model, 'SparseLinearRegression/_global_scale_noncentered')],
    ci.model_samples[get_param_index(ci.model, 'SparseLinearRegression/_local_scale_variances')],
    ci.model_samples[get_param_index(ci.model, 'SparseLinearRegression/_local_scales_noncentered')],
    ci.model_samples[get_param_index(ci.model, 'SparseLinearRegression/_weights_noncentered')],
), axis=0)
Berlyli866 commented 1 year ago

thanks !!! yeah i found it in the starter notebook. thank you !