示例#1
0
文件: usage.py 项目: miklou/pycogent
    def fixNegsConstrainedOpt(self, to_minimize=norm_diff, badness=1e6):
        """Uses constrained minimization to find approx q matrix.

        to_minimize: metric for comparing orig result and new result.

        badness: scale factor for penalizing negative off-diagonal values.
        """
        if not sum_neg_off_diags(self._data):
            return self
        q = ravel(without_diag(self._data))
        p = expm(self._data)(t=1)

        def err_f(q):
            new_q = reshape(array(q), (4, 3))
            new_q = with_diag(new_q, -sum(new_q, 1))
            p_new = expm(new_q)(t=1)
            result = to_minimize(ravel(p), ravel(p_new))
            if q.min() < 0:
                result += -q.min() * badness
            return result

        a = array(q)
        xmin = fmin(func=err_f, x0=a, disp=0)
        r = reshape(xmin, (4, 3))
        new_q = with_diag(r, -sum(r, 1))
        return self.__class__(new_q, self.Alphabet)
示例#2
0
文件: usage.py 项目: miklou/pycogent
    def _timeForSimilarity_naive(self, similarity, freqs=None):
        """Returns time exponent so that exp(q*time) diverges to right distance.

        Takes symbol freqs into account if specified; otherwise assumes equal.

        freqs: vector of frequencies, applied to each row successively.

        WARNING: Factor of 5 slower than timeForSimilarity. Included for 
        testing that results are identical.
        """
        q = self._data
        if freqs is None:
            def similarity_f(t):
                return abs(average(diagonal(expm(q)(t)))-similarity)
        else:
            def similarity_f(t):
                return abs(sum(diagonal(expm(q)(t)*freqs)) - similarity)
        initial_guess = array([1.0])
        result = fmin(similarity_f, initial_guess, disp=0)
        #disp=0 turns off fmin messages
        return result
示例#3
0
    def _timeForSimilarity_naive(self, similarity, freqs=None):
        """Returns time exponent so that exp(q*time) diverges to right distance.

        Takes symbol freqs into account if specified; otherwise assumes equal.

        freqs: vector of frequencies, applied to each row successively.

        WARNING: Factor of 5 slower than timeForSimilarity. Included for 
        testing that results are identical.
        """
        q = self._data
        if freqs is None:
            def similarity_f(t):
                return abs(average(diagonal(expm(q)(t)))-similarity)
        else:
            def similarity_f(t):
                return abs(sum(diagonal(expm(q)(t)*freqs)) - similarity)
        initial_guess = array([1.0])
        result = fmin(similarity_f, initial_guess, disp=0)
        #disp=0 turns off fmin messages
        return result
def fit_function(x_vals, y_vals, func, n_params, iterations=2):
    """ Fit any function to any array of values of x and y.
   :Parameters:
       x_vals : array
           Values for x to fit the function func.
       y_vals : array
           Values for y to fit the function func.
       func : callable ``f(x, a)``
           Objective function (model) to be fitted to the data. This function 
           should return either an array for models that are not a constant, 
           i.e. f(x)=exp(a[0]+x*a[1]), or a single value for models that are a
           cosntant, i.e. f(x)=a[0]
       n_params : int
           Number of parameters to fit in func
       iterations : int
           Number of iterations to fit func

   :Returns: param_guess

       param_guess : array
           Values for each of the arguments to fit func to x_vals and y_vals

   :Notes:

       Fit a function to a given array of values x and y using simplex to
       minimize the error.

   """

    # internal function to minimize the error
    def f2min(a):
        #sum square deviation
        return ((func(x_vals, a) - y_vals)**2).sum()

    param_guess = array(range(n_params))
    for i in range(iterations):
        xopt = fmin(f2min, param_guess, disp=0)
        param_guess = xopt

    return xopt
示例#5
0
def fit_function(x_vals, y_vals, func, n_params, iterations=2):
   """ Fit any function to any array of values of x and y.
   :Parameters:
       x_vals : array
           Values for x to fit the function func.
       y_vals : array
           Values for y to fit the function func.
       func : callable ``f(x, a)``
           Objective function (model) to be fitted to the data. This function 
           should return either an array for models that are not a constant, 
           i.e. f(x)=exp(a[0]+x*a[1]), or a single value for models that are a
           cosntant, i.e. f(x)=a[0]
       n_params : int
           Number of parameters to fit in func
       iterations : int
           Number of iterations to fit func

   :Returns: param_guess

       param_guess : array
           Values for each of the arguments to fit func to x_vals and y_vals

   :Notes:

       Fit a function to a given array of values x and y using simplex to
       minimize the error.

   """

   # internal function to minimize the error
   def f2min(a):
       #sum square deviation
       return ((func(x_vals, a) - y_vals)**2).sum()

   param_guess = array(range(n_params))
   for i in range(iterations):
       xopt = fmin(f2min, param_guess, disp=0)
       param_guess = xopt

   return xopt
示例#6
0
文件: usage.py 项目: miklou/pycogent
    def fixNegsConstrainedOpt(self, to_minimize=norm_diff, badness=1e6):
        """Uses constrained minimization to find approx q matrix.

        to_minimize: metric for comparing orig result and new result.

        badness: scale factor for penalizing negative off-diagonal values.
        """
        if not sum_neg_off_diags(self._data):
            return self
        q = ravel(without_diag(self._data))
        p = expm(self._data)(t=1)
        def err_f(q):
            new_q = reshape(array(q), (4,3))
            new_q = with_diag(new_q, -sum(new_q, 1))
            p_new = expm(new_q)(t=1)
            result = to_minimize(ravel(p), ravel(p_new))
            if q.min() < 0:
                result += -q.min() * badness
            return result
        a = array(q)
        xmin = fmin(func=err_f, x0=a, disp=0)
        r = reshape(xmin, (4,3))
        new_q = with_diag(r, -sum(r, 1))
        return self.__class__(new_q, self.Alphabet)
示例#7
0
 def _minimise(self, f, x, **kw):
     return fmin(f, x, **kw)
示例#8
0
 def _minimise(self, f, x, **kw):
     return fmin(f, x, **kw)