def DetModel(string): from dcprogs.likelihood import DeterminantEq string = string.lower().rstrip().lstrip() if 'transpose' in string: return DetModel(string.replace('transpose', '')).transpose() if string == "classic": return DeterminantEq(QMat(string), 1e-4) if string == "ch82": return DeterminantEq(QMat(string), 0.2) if string == "cb": return DeterminantEq(QMat(string), 0.2) if string == "cks": return DeterminantEq(QMat(string), 0.2) else: raise Exception("Unknown eG model {0}".format(string))
def step(context): from dcprogs.likelihood import QMatrix, DeterminantEq qmatrix = QMatrix( [[ -1.89907444e+02, 0.00000000e+00, 2.17917781e+01, 1.68115666e+02, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00 ], [ 0.00000000e+00, -5.59197168e+02, 0.00000000e+00, 0.00000000e+00, 2.28813670e+01, 5.36315801e+02, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00 ], [ 1.70657017e+04, 0.00000000e+00, -1.70657017e+04, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00 ], [ 6.93510518e+04, 0.00000000e+00, 0.00000000e+00, -7.70252433e+04, 0.00000000e+00, 0.00000000e+00, 7.67419154e+03, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00 ], [ 0.00000000e+00, 1.62124166e+04, 0.00000000e+00, 0.00000000e+00, -1.62124166e+04, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00 ], [ 0.00000000e+00, 7.15538758e+04, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, -1.06690747e+06, 9.95292553e+05, 0.00000000e+00, 6.10438859e+01, 0.00000000e+00, 0.00000000e+00 ], [ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.45242879e+04, 0.00000000e+00, 3.69900307e+05, -3.87202655e+05, 2.77806009e+03, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00 ], [ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 2.01908451e+03, -2.28998927e+03, 2.70904758e+02, 0.00000000e+00, 0.00000000e+00 ], [ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 2.87990000e+04, 0.00000000e+00, 1.09766807e+02, -2.90893700e+04, 1.80603172e+02, 0.00000000e+00 ], [ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 2.19533614e+02, -3.09835200e+02, 9.03015859e+01 ], [ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 3.29300422e+02, -3.29300422e+02 ]], 3) context.determinant = DeterminantEq(qmatrix, 4e-5).transpose()
def step(context, tau): from sys import exc_info from dcprogs.likelihood import DeterminantEq try: context.determinant = DeterminantEq(context.qmatrix, tau) except: context.initialization_exception = exc_info()
def step(context): from sys import exc_info from dcprogs.likelihood import DeterminantEq print(context.matrix) try: context.determinant = DeterminantEq(context.matrix, context.nopen, context.tau) except: context.initialization_exception = exc_info()
def step(context, n): from dcprogs.likelihood import DeterminantEq from dcprogs.likelihood.random import qmatrix as random_qmatrix context.matrices = [] context.equations = [] while len(context.equations) < n: try: matrix = random_qmatrix() equation = DeterminantEq(matrix, 1e-4) except: continue else: context.matrices.append(matrix) context.equations.append(equation)
from numpy import all, abs, arange from dcprogs.likelihood import QMatrix, DeterminantEq, MissedEventsG # Define parameters. qmatrix = QMatrix([ [-3050, 50, 3000, 0, 0], [2./3., -1502./3., 0, 500, 0], [ 15, 0, -2065, 50, 2000], [ 0, 15000, 4000, -19000, 0], [ 0, 0, 10, 0, -10] ], 2) tau = 1e-4 # Create eG from prior knowledge of roots determinant_eq = DeterminantEq(qmatrix, tau) af_roots = [( -3045.285776037674, 1), (-162.92946543451328, 1)] fa_roots = [(-17090.192769236815, 1), (-2058.0812921673496, 1), (-0.24356535498785126, 1)] eG_from_roots = MissedEventsG(determinant_eq, af_roots, determinant_eq.transpose(), fa_roots) # Create eG automaticallye eG_automatic = MissedEventsG(qmatrix, tau) # Checks the three initialization are equivalent at tau assert all(abs(eG_from_roots.af(tau) - eG_automatic.af(tau)) < 1e-8) assert all(abs(eG_from_roots.fa(tau) - eG_automatic.fa(tau)) < 1e-8) # Checks the three initialization are equivalent at different times # The functions can be applied to arrays. times = arange(tau, 10*tau, 0.1*tau) assert eG_from_roots.af(times).shape == (len(times), 2, 3) assert eG_from_roots.fa(times).shape == (len(times), 3, 2) assert all(abs(eG_from_roots.af(times) - eG_automatic.af(times)) < 1e-8) assert all(abs(eG_from_roots.fa(times) - eG_automatic.fa(times)) < 1e-8)
from numpy import abs, all, array from dcprogs import internal_dtype from dcprogs.likelihood import DeterminantEq, QMatrix # Define parameters. matrix = [ [-3050, 50, 3000, 0, 0], [2./3., -1502./3., 0, 500, 0], [ 15, 0, -2065, 50, 2000], [ 0, 15000, 4000, -19000, 0], [ 0, 0, 10, 0, -10] ] qmatrix = QMatrix(matrix, 2) tau = 1e-4 det0 = DeterminantEq(qmatrix, 1e-4); det1 = DeterminantEq(matrix, 2, 1e-4); print("{0!s}\n\n{1!s}".format(det0, det1)) x = [0, -1, -1e2] assert all(abs(det0(x) - det1(x)) < 1e-8) roots = array([-3045.285776037674, -162.92946543451328]) assert all(abs(det0(roots)) < 1e-6 * abs(roots)) transpose = det0.transpose() roots = array( [-17090.192769236815, -2058.0812921673496, -0.24356535498785126], dtype=internal_dtype ) assert all(abs(transpose(roots)) < 1e-6 * abs(roots)) print(" * H({0}):\n{1}\n".format(0, det0.H(0))) print(" * H({0}):\n{1}\n".format(-1e2, det0.H(-1e2)))
def step(context, event, s, tau): import numpy from dcprogs.likelihood import DeterminantEq s = eval(s, globals().copy(), numpy.__dict__.copy()) if event == "open": context.result = DeterminantEq(context.qmatrix, tau)(s) else: context.result = DeterminantEq(context.qmatrix.transpose(), tau)(s)
def step(context, s): from dcprogs.likelihood import DeterminantEq context.result = DeterminantEq(context.matrix, context.nopen, context.tau)(s)
from numpy import all from dcprogs.likelihood import eig from dcprogs.likelihood import find_upper_bound_for_roots, find_lower_bound_for_roots, \ find_root_intervals, brentq, find_roots, QMatrix, DeterminantEq qmatrix = QMatrix([ [-3050, 50, 3000, 0, 0], [2./3., -1502./3., 0, 500, 0], [ 15, 0, -2065, 50, 2000], [ 0, 15000, 4000, -19000, 0], [ 0, 0, 10, 0, -10] ], 2) det = DeterminantEq(qmatrix, 1e-4); upper_bound = find_upper_bound_for_roots(det); lower_bound = find_lower_bound_for_roots(det); get_eigenvalues = lambda s: eig(det.H(s))[0].T assert all(get_eigenvalues(lower_bound) > lower_bound) assert all(get_eigenvalues(upper_bound) < upper_bound) print("Root Determination\n" \ "==================\n\n" \ " * Interval containing roots: {lower}, {upper}\n" \ " * Eigenvalues of H at lower bound: {eiglow}\n" \ " * Eigenvalues of H at upper bound: {eigup}\n" \ .format( lower=lower_bound, upper=upper_bound, eiglow = get_eigenvalues(lower_bound), eigup = get_eigenvalues(upper_bound) )) intervals = find_root_intervals(det, lower_bound, upper_bound)
def step(context, doopen, qmatrix, tau): from dcprogs.likelihood import DeterminantEq context.equation = DeterminantEq(qmatrix, tau) if doopen != "open": context.equation = context.equation.transpose() print(context.equation)
from numpy import abs, all, array from dcprogs import internal_dtype from dcprogs.likelihood import DeterminantEq, QMatrix # Define parameters. matrix = [[-3050, 50, 3000, 0, 0], [2. / 3., -1502. / 3., 0, 500, 0], [15, 0, -2065, 50, 2000], [0, 15000, 4000, -19000, 0], [0, 0, 10, 0, -10]] qmatrix = QMatrix(matrix, 2) tau = 1e-4 det0 = DeterminantEq(qmatrix, 1e-4) det1 = DeterminantEq(matrix, 2, 1e-4) print("{0!s}\n\n{1!s}".format(det0, det1)) x = [0, -1, -1e2] assert all(abs(det0(x) - det1(x)) < 1e-8) roots = array([-3045.285776037674, -162.92946543451328]) assert all(abs(det0(roots)) < 1e-6 * abs(roots)) transpose = det0.transpose() roots = array([-17090.192769236815, -2058.0812921673496, -0.24356535498785126], dtype=internal_dtype) assert all(abs(transpose(roots)) < 1e-6 * abs(roots)) print(" * H({0}):\n{1}\n".format(0, det0.H(0))) print(" * H({0}):\n{1}\n".format(-1e2, det0.H(-1e2))) print(" * d[sI-H(s)]/ds for s={0}:\n{1}\n".format(0, det0.s_derivative(0))) print(" * d[sI-H(s)]/ds for s={0}:\n{1}\n".format(-1e2,
from numpy import all, abs, arange from dcprogs.likelihood import QMatrix, DeterminantEq, MissedEventsG # Define parameters. qmatrix = QMatrix([[-3050, 50, 3000, 0, 0], [2. / 3., -1502. / 3., 0, 500, 0], [15, 0, -2065, 50, 2000], [0, 15000, 4000, -19000, 0], [0, 0, 10, 0, -10]], 2) tau = 1e-4 # Create eG from prior knowledge of roots determinant_eq = DeterminantEq(qmatrix, tau) af_roots = [(-3045.285776037674, 1), (-162.92946543451328, 1)] fa_roots = [(-17090.192769236815, 1), (-2058.0812921673496, 1), (-0.24356535498785126, 1)] eG_from_roots = MissedEventsG(determinant_eq, af_roots, determinant_eq.transpose(), fa_roots) # Create eG automaticallye eG_automatic = MissedEventsG(qmatrix, tau) # Checks the three initialization are equivalent at tau assert all(abs(eG_from_roots.af(tau) - eG_automatic.af(tau)) < 1e-8) assert all(abs(eG_from_roots.fa(tau) - eG_automatic.fa(tau)) < 1e-8) # Checks the three initialization are equivalent at different times # The functions can be applied to arrays. times = arange(tau, 10 * tau, 0.1 * tau) assert eG_from_roots.af(times).shape == (len(times), 2, 3) assert eG_from_roots.fa(times).shape == (len(times), 3, 2) assert all(abs(eG_from_roots.af(times) - eG_automatic.af(times)) < 1e-8) assert all(abs(eG_from_roots.fa(times) - eG_automatic.fa(times)) < 1e-8)