def PdfFromME(alpha, A, x): """ Returns the probability density function of a matrix- exponential distribution. Parameters ---------- alpha : vector, shape (1,M) The initial vector of the matrix-exponential distribution. A : matrix, shape (M,M) The matrix parameter of the matrix-exponential distribution. x : vector of doubles The density function will be computed at these points prec : double, optional Numerical precision to check if the input ME distribution is valid. The default value is 1e-14. Returns ------- pdf : column vector of doubles The values of the density function at the corresponding "x" values """ if butools.checkInput and not CheckMERepresentation(alpha, A): raise Exception("PdfFromME: Input is not a valid ME representation!") y = [np.sum(alpha * expm(A * xv) * (-A)) for xv in x] return np.array(y)
def CdfFromME(alpha, A, x): """ Returns the cummulative distribution function of a matrix-exponential distribution. Parameters ---------- alpha : matrix, shape (1,M) The initial vector of the matrix-exponential distribution. A : matrix, shape (M,M) The matrix parameter of the matrix-exponential distribution. x : vector of doubles The cdf will be computed at these points Returns ------- cdf : column vector of doubles The values of the cdf at the corresponding "x" values """ if butools.checkInput and not CheckMERepresentation(alpha, A): raise Exception("CdfFromME: Input is not a valid ME representation!") return np.array([1.0 - np.sum(alpha * expm(A.A * xv)) for xv in x])
def CanonicalFromPH3(alpha, A, prec=1e-14): """ Returns the canonical form of an order-3 phase-type distribution. Parameters ---------- alpha : matrix, shape (1,3) Initial vector of the phase-type distribution A : matrix, shape (3,3) Transient generator of the phase-type distribution prec : double, optional Numerical precision, default value is 1e-14 Returns ------- beta : matrix, shape (1,3) The initial probability vector of the canonical form B : matrix, shape (3,3) Transient generator matrix of the canonical form Notes ----- This procedure calculates 5 moments of the input and calls 'PH3From5Moments'. """ if butools.checkInput and not CheckMERepresentation(alpha, A): raise Exception( "CanonicalFromPH3: Input isn''t a valid ME distribution!") if A.shape[0] != 3: raise Exception("CanonicalFromPH3: Dimension is not 3!") return PH3From5Moments(MomentsFromPH(alpha, A, 5), prec)
def PHFromME (alpha, A, precision=1e-14): """ Obtains a Markovian representation of a matrix exponential distribution of the same size, if possible. Parameters ---------- alpha : vector, shape (1,M) The initial vector of the matrix-exponential distribution. A : matrix, shape (M,M) The matrix parameter of the matrix-exponential distribution. precision : double, optional A representation is considered to be a Markovian one if it is closer than the precision. The default value is 1e-14. Returns ------- beta : vector, shape (1,M) The initial probability vector of the Markovian monocyclic representation B : matrix, shape (M,M) Transient generator matrix of the Markovian monocyclic representation References ---------- .. [1] G Horváth, M Telek, "A minimal representation of Markov arrival processes and a moments matching method," Performance Evaluation 64:(9-12) pp. 1153-1168. (2007) """ def transfun (orep, B): ao, Ao = orep return (ao*B, la.inv(B)*Ao*B) def evalfun (orep, k=0): ao, Ao = orep av= np.sum(-Ao,1) Ad = Ao-np.diag(np.diag(Ao)) if k%2 == 0: return -min(np.min(ao), np.min(av), np.min(Ad)) else: return -np.sum(ao[ao<0]) - np.sum(av[av<0]) - np.sum(Ad[Ad<0]) if butools.checkInput and not CheckMERepresentation (alpha, A): raise Exception("PHFromME: Input is not a valid ME representation!") return FindMarkovianRepresentation ((alpha,A), transfun, evalfun, precision)
def CheckMEPositiveDensity (alpha, A, maxSize=100, prec=None): """ Checks if the given matrix-exponential distribution has positive density. Parameters ---------- alpha : matrix, shape (1,M) Initial vector of the matrix-exponential distribution to check A : matrix, shape (M,M) Matrix parameter of the matrix-exponential distribution to check maxSize : int, optional The procedure tries to transform the ME distribution to phase-type up to order maxSize. The default value is 100. prec : double, optional Numerical precision. The default value is 1e-14. Returns ------- r : bool True, if the given matrix-exponential distribution has a positive density Notes ----- This procedure calls MonocyclicPHFromME, and can be time consuming. """ if prec==None: prec=butools.checkPrecision try: beta, B = MonocyclicPHFromME (alpha, A, maxSize, prec) r = CheckMERepresentation (beta, B, prec) except Exception: r = False return r
def MomentsFromME(alpha, A, K=0): """ Returns the first K moments of a matrix-exponential distribution. Parameters ---------- alpha : vector, shape (1,M) The initial vector of the matrix-exponential distribution. A : matrix, shape (M,M) The matrix parameter of the matrix-exponential distribution. K : int, optional Number of moments to compute. If K=0, 2*M-1 moments are computed. The default value is K=0. prec : double, optional Numerical precision for checking the input. The default value is 1e-14. Returns ------- moms : row vector of doubles The vector of moments. """ if butools.checkInput and not CheckMERepresentation(alpha, A): raise Exception( "MomentsFromME: Input is not a valid ME representation!") if K == 0: K = 2 * np.size(alpha, 1) - 1 return [ math.factorial(i) * np.sum(alpha * la.inv(-A)**i) for i in range(1, K + 1) ]
def MEOrder(alpha, A, kind="moment", prec=1e-10): """ Returns the order of the ME distribution (which is not necessarily equal to the size of the representation). Parameters ---------- alpha : vector, shape (1,M) The initial vector of the matrix-exponential distribution. A : matrix, shape (M,M) The matrix parameter of the matrix-exponential distribution. kind : {'obs', 'cont', 'obscont', 'moment'}, optional Determines which order is computed. Possibilities: 'obs': observability, 'cont': controllability, 'obscont': the minimum of observability and controllability order, 'moment': moment order (which is the default). prec : double, optional Precision used to detect if the determinant of the Hankel matrix is zero (in case of kind="moment" only), or the tolerance for the rank calculation. The default value is 1e-10. Returns ------- order : int The order of ME distribution References ---------- .. [1] P. Buchholz, M. Telek, "On minimal representation of rational arrival processes." Madrid Conference on Qeueuing theory (MCQT), June 2010. """ if butools.checkInput and not CheckMERepresentation(alpha, A): raise Exception("MEOrder: Input is not a valid ME representation!") N = alpha.shape[1] if kind == "cont": re = np.zeros((N, N)) for n in range(N): re[n, :] = np.sum(A.T**n, 0) return nla.matrix_rank(re, prec) elif kind == "obs": re = np.zeros((N, N)) for n in range(N): re[n, :] = alpha * A**n return nla.matrix_rank(re, prec) elif kind == "obscont": re = np.zeros((N, N)) for n in range(N): re[n, :] = alpha * A**n obsOrder = nla.matrix_rank(re, prec) re = np.zeros((N, N)) for n in range(N): re[n, :] = np.sum(A.T**n, 0) contOrder = nla.matrix_rank(re, prec) return min(obsOrder, contOrder) elif kind == "moment": return MEOrderFromMoments(MomentsFromME(alpha, A), prec) else: raise Exception("Invalid 'kind' parameter!")
def MinimalRepFromME(alpha, A, how="moment", precision=1e-12): """ Returns the minimal representation of the given ME distribution. Parameters ---------- alpha : vector, shape (1,M) The initial vector of the matrix-exponential distribution. A : matrix, shape (M,M) The matrix parameter of the matrix-exponential distribution. how : {"obs", "cont", "obscont", "moment"}, optional Determines how the representation is minimized. Possibilities: 'obs': observability, 'cont': controllability, 'obscont': the minimum of observability and controllability order, 'moment': moment order (which is the default). precision : double, optional Precision used by the Staircase algorithm. The default value is 1e-12. Returns ------- beta : vector, shape (1,N) The initial vector of the minimal representation B : matrix, shape (N,N) The matrix parameter of the minimal representation References ---------- .. [1] P. Buchholz, M. Telek, "On minimal representation of rational arrival processes." Madrid Conference on Qeueuing theory (MCQT), June 2010. """ if butools.checkInput and not CheckMERepresentation(alpha, A): raise Exception( "MinimalRepFromME: Input is not a valid ME representation!") if how == "cont": H0 = A H1 = np.sum(-A, 1) * alpha B, n = MStaircase([H0, H1], ml.ones((A.shape[0], 1)), precision) return ((alpha * B)[0, 0:n], (la.inv(B) * A * B)[0:n, 0:n]) elif how == "obs": H0 = A H1 = np.sum(-A, 1) * alpha G = [H0.T, H1.T] B, n = MStaircase(G, alpha.T, precision) return ((alpha * B)[:, 0:n], (la.inv(B) * A * B)[0:n, 0:n]) elif how == "obscont": alphav, Av = MinimalRepFromME(alpha, A, "cont", precision) return MinimalRepFromME(alphav, Av, "obs", precision) elif how == "moment": N = MEOrder(alpha, A, "moment", precision) moms = MomentsFromME(alpha, A, 2 * N - 1) return MEFromMoments(moms)