Skip to content

Commit

Permalink
add degree of freedom computation
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengp0 committed Feb 14, 2024
1 parent bfcf989 commit b998dc7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = limetr
version = 0.0.7
version = 0.0.8
description =
long_description = file: README.rst
long_description_content_type = text/x-rst
Expand Down
28 changes: 28 additions & 0 deletions src/limetr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,3 +584,31 @@ def get_R2(model: LimeTr):

def get_rmse(model: LimeTr):
return np.sqrt(get_marginal_rvar(model))


def get_degree_of_freedom(model: LimeTr):
k = model.k_total

c_mat = np.empty(shape=(0, k))
c_vec = np.empty(shape=(0,))
if model.use_uprior:
c_mat_sub = np.identity(k)
c_mat = np.vstack([c_mat, -c_mat_sub, c_mat_sub])
c_vec = np.hstack([c_vec, -model.uprior[0], model.uprior[1]])
if model.use_lprior or model.use_constraints:
c_mat_sub = model.jacobian(model.soln)
c_mat = np.vstack([c_mat, -c_mat_sub, c_mat_sub])
c_vec = np.hstack([c_vec, -model.cl, model.cu])

# determine active constraints
index_active = np.isclose(c_mat.dot(model.soln), c_vec)
c_mat_active = c_mat[index_active]

if c_mat_active.size == 0:
return k

lb, ub = c_mat_active.min(axis=0), c_mat_active.max(axis=0)
lb[np.isclose(lb, 0.0)] = 0.0
ub[np.isclose(ub, 0.0)] = 0.0

return k - 0.5 * ((lb < 0) + (ub > 0)).sum()
16 changes: 15 additions & 1 deletion tests/test_limetr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import numpy as np
import pytest
from limetr import LimeTr, get_conditional_R2, get_marginal_R2, get_rmse
from limetr import (
LimeTr,
get_conditional_R2,
get_marginal_R2,
get_rmse,
get_degree_of_freedom,
)
from scipy.linalg import block_diag


Expand Down Expand Up @@ -347,3 +353,11 @@ def test_performance():
assert isinstance(conditional_r2, float)
assert isinstance(marginal_r2, float)
assert isinstance(rmse, float)


def test_degree_of_freedom():
model = lmtr_test_problem()
model.optimize()

k = get_degree_of_freedom(model)
assert k == model.k

0 comments on commit b998dc7

Please sign in to comment.