Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to extract LR coefficients #1565

Open
jwretham opened this issue Apr 18, 2024 · 2 comments · May be fixed by #1597
Open

Unable to extract LR coefficients #1565

jwretham opened this issue Apr 18, 2024 · 2 comments · May be fixed by #1597

Comments

@jwretham
Copy link

jwretham commented Apr 18, 2024

Hi, I'm using NP as part of a forecasting tool I'm building.

I'd really like to be able to access the linear regression coefficient estimates for my additional regressors.

In Prophet I'm able to do this using the regressor_coefficients() function, however I can't find anything in the NP documentation about how to do this.

Any help/suggestions appreciated

Thanks

Joe

@Constantin343
Copy link
Contributor

Constantin343 commented Jun 21, 2024

Hi Joe,

I was able to get the coefficients like this. Not sure if you are only interested in future regressors, but I also added lagged and event just in case:

def get_all_regressor_coefficients(model):
    # Access the TimeNet model 
    time_net = model.model
    coefficients = {}

    # Future Regressors
    if time_net.config_regressors is not None and time_net.config_regressors.regressors is not None:
        for name, config in time_net.config_regressors.regressors.items():
            regressor_component = time_net.future_regressors
            if config.mode == 'additive':
                coefficients[name] = regressor_component.regressor_params['additive'].data.cpu().numpy()
            elif config.mode == 'multiplicative':
                coefficients[name] = regressor_component.regressor_params['multiplicative'].data.cpu().numpy()
    
    # Lagged Regressors
    if time_net.config_lagged_regressors is not None:
        for name, config in time_net.config_lagged_regressors.items():
            layer_weights = []
            for layer in time_net.covar_net:
                if isinstance(layer, nn.Linear):
                    layer_weights.append(layer.weight.data.cpu().numpy())
            if layer_weights:
                coefficients[name] = np.array(layer_weights)

    
    # Event Regressors
    if time_net.config_events is not None:
        for event, event_config in time_net.config_events.items():
            if event_config.mode == 'additive':
                coefficients[event] = time_net.event_params['additive'].data.cpu().numpy()
            elif event_config.mode == 'multiplicative':
                coefficients[event] = time_net.event_params['multiplicative'].data.cpu().numpy()

    return coefficients

# Get the regressor coefficients
coefficients = get_all_regressor_coefficients(m)
print("Coefficients:", temp_coefficients)

I can also create a PR to make the coefficients more easily accessible. @ourownstory would be great to know what you think Oskar.

Hope this helps!

Constantin

@Constantin343 Constantin343 linked a pull request Jun 23, 2024 that will close this issue
3 tasks
@ourownstory ourownstory linked a pull request Jun 25, 2024 that will close this issue
3 tasks
@jwretham
Copy link
Author

Hi Constantin - so sorry, I absolutely missed that you'd responded on this. Thanks for taking the time to put this together - let me give it a try and I'll get back to you.

Joe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants