#-------------------------------------------------------------------------------
# Name:        Calibrate gravity model 
# Purpose:     Calibrates the coefficient for the gravity model with -ve exp friction factor
#              given an average trip length, skim and P/A vectors
# Author:      Chetan Joshi, Portland OR
#
# Created:     5/6/2015
#              
# Copyright:   (c) Chetan Joshi 2015
# Licence:     Permission is hereby granted, free of charge, to any person obtaining a copy
#              of this software and associated documentation files (the "Software"), to deal
#              in the Software without restriction, including without limitation the rights
#              to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#              copies of the Software, and to permit persons to whom the Software is
#              furnished to do so, subject to the following conditions:
#
#              The above copyright notice and this permission notice shall be included in all
#              copies or substantial portions of the Software.
#
#              THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#              IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#              FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#              AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#              LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#              OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#              SOFTWARE.
#-------------------------------------------------------------------------------
import numpy

def CalibrateSinglyConstrained(P, A, avg_trip_len, skim, c = -0.1, alpha = 2, max_iter = 10):
    ProdA = P
    AttrA = A
    num_zones = len(P)
    trips = numpy.zeros((num_zones, num_zones))
    fric_mat = numpy.exp(c*skim)
    
    for Iter in range(0, max_iter):
        for i in range(0, num_zones):
            trips[i,:] = ProdA[i]*AttrA*fric_mat[i,:]/max(0.000001, sum(AttrA * fric_mat[i,:]))

        model_trip_len = (trips*skim).sum() / trips.sum()
        c = c*(model_trip_len / avg_trip_len)**alpha
        fric_mat = numpy.exp(c*skim)

        print ('iteration: ', Iter, ' coefficient: ', c, ' average trip length (model): ', model_trip_len)
        
    print ('target average trip length (observed): ', avg_trip_len) 
    print ('final average trip length (model): ', model_trip_len)
    print ('final logit scaling factor: ', c)
    return trips   #returns final trip matrix corresponding to calibrated coefficient

@@ -112,17 +112,16 @@
 ty model
- 
 %0A# Purpo
@@ -340,30 +340,16 @@
 6/2015%0A#
-              
 %0A# Copyr
@@ -1721,16 +1721,22 @@
 rt numpy
+ as np
 %0A%0Adef Ca
@@ -1887,20 +1887,17 @@
 rips = n
-umpy
+p
 .zeros((
@@ -1927,36 +1927,33 @@
     fric_mat = n
-umpy
+p
 .exp(c*skim)%0A   
@@ -1949,20 +1949,16 @@
 c*skim)%0A
-    
 %0A    for
@@ -2078,19 +2078,26 @@
 at%5Bi,:%5D/
-max
+np.maximum
 (0.00000
@@ -2099,16 +2099,19 @@
 000001, 
+np.
 sum(Attr
@@ -2262,20 +2262,17 @@
 _mat = n
-umpy
+p
 .exp(c*s
@@ -2384,24 +2384,16 @@
 ip_len)%0A
-        
 %0A    pri
@@ -2452,17 +2452,16 @@
 rip_len)
- 
 %0A    pri

Update CalibrateGravity.py