Skip to content

Make convex solver more robust for weekday correction #1223

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

Merged
merged 3 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doctor_visits/delphi_doctor_visits/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ def run_module(params):
weekday=weekday,
se=params["indicator"]["se"]
)
if sensor is None:
logging.error("No sensors calculated, no output will be produced")
continue
# write out results
out_name = "smoothed_adj_cli" if weekday else "smoothed_cli"
if params["indicator"]["se"]:
Expand Down
3 changes: 3 additions & 0 deletions doctor_visits/delphi_doctor_visits/update_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ def update_sensor(

# handle if we need to adjust by weekday
params = Weekday.get_params(data) if weekday else None
if weekday and np.any(np.all(params == 0,axis=1)):
# Weekday correction failed for at least one count type
return None

# handle explicitly if we need to use Jeffreys estimate for binomial proportions
jeffreys = bool(se)
Expand Down
26 changes: 17 additions & 9 deletions doctor_visits/delphi_doctor_visits/weekday.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
Created: 2020-05-06
"""

# standard packages
import logging

# third party
import cvxpy as cp
import numpy as np
Expand Down Expand Up @@ -82,15 +85,20 @@ def get_params(data):
penalty = (
lmbda * cp.norm(cp.diff(b[6:], 3), 1) / (X.shape[0] - 2)
) # L-1 Norm of third differences, rewards smoothness
try:
prob = cp.Problem(cp.Minimize(-ll + lmbda * penalty))
_ = prob.solve()
except:
# If the magnitude of the objective function is too large, an error is
# thrown; Rescale the objective function
prob = cp.Problem(cp.Minimize((-ll + lmbda * penalty) / 1e5))
_ = prob.solve()
params[i, :] = b.value
scales = [1, 1e5, 1e10, 1e15]
for scale in scales:
try:
prob = cp.Problem(cp.Minimize((-ll + lmbda * penalty) / scale))
_ = prob.solve()
params[i,:] = b.value
break
except:
# If the magnitude of the objective function is too large, an error is
# thrown; Rescale the objective function by going through loop
pass
else:
# Leaving params[i,:] = 0 is equivalent to not performing weekday correction
logging.error("Unable to calculate weekday correction")

return params

Expand Down