Skip to content
Open
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
17 changes: 17 additions & 0 deletions pygam/penalties.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,22 @@ def concave(n, coef):
# P = sp.sparse.vstack([row, sp.sparse.csc_matrix((n-2, n)), row[::-1]])
# return P.tocsc()

def positive_only(n, coef):
"""
Builds a penalty matrix for P-Splines with positive features.
Penalizes violation of a positive feature function.

Parameters
----------
n : int
number of splines
coef : array-like, coefficients of the feature function

Returns
-------
penalty matrix : sparse csc matrix of shape (n,n)
"""
return sp.sparse.csc_matrix(np.diag((coef.ravel() < 0).astype(float)))

def none(n, coef):
"""
Expand Down Expand Up @@ -362,5 +378,6 @@ def sparse_diff(array, n=1, axis=-1):
'concave': concave,
'monotonic_inc': monotonic_inc,
'monotonic_dec': monotonic_dec,
'positive_only': positive_only,
'none': none,
}
Loading