Page MenuHomec4science

custom_fit.py
No OneTemporary

File Metadata

Created
Mon, May 6, 17:50

custom_fit.py

# Copyright (C) 2018-2020 by the RROMPy authors
#
# This file is part of RROMPy.
#
# RROMPy is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RROMPy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with RROMPy. If not, see <http://www.gnu.org/licenses/>.
#
import numpy as np
from rrompy.utilities.exception_manager import RROMPyWarning, RROMPyAssert
__all__ = ["customFit"]
def customFit(van, y, rcond=-1, full=False, w=None):
van, y = np.array(van), np.array(y)
RROMPyAssert(len(van), len(y), "Number of samples for fitting")
lhs, rhs = van.T, y.T
if rcond < 0: rcond = len(van) * np.finfo(van.dtype).eps
if isinstance(w, (str,)) and w.upper() == "AUTO":
# Determine the norms of the design matrix rows.
w = np.sum(np.abs(van) ** 2., axis = 1) ** .5
w[w == 0] = 1
w = w ** -1.
elif w is not None:
w = np.array(w)
else:
w = 1.
lhs, rhs = lhs * w, rhs * w
scl = np.sum(np.abs(lhs) ** 2., axis = 1) ** .5
scl[scl == 0] = 1
c, resids, rank, s = np.linalg.lstsq(lhs.T / scl, rhs.T, rcond)
c = (c.T / scl).T
if rank != van.shape[1] and not full:
RROMPyWarning("The fit may be poorly conditioned.")
if full:
return c, [resids, rank, s, rcond]
else:
return c

Event Timeline