3
\$\begingroup\$

The profile tells me it took ~15s to run, but without telling me more.

Tue Aug 19 20:55:38 2014    Profile.prof

3 function calls in 15.623 seconds

Ordered by: internal time

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    1   15.623   15.623   15.623   15.623 {singleLoan.genLoan}
    1    0.000    0.000   15.623   15.623 <string>:1(<module>)
    1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
import numpy as np
cimport numpy as np
from libc.stdlib cimport malloc, free
from libc.stdlib cimport rand, srand, RAND_MAX
import cython
cimport cython
import StringIO

cdef extern from "math.h":
    int floor(double x)
    double pow(double x, double y)
    double exp(double x)

cdef double[:] zeros = np.zeros(360)
cdef double[:] stepCoupons = np.array([2.0,60,3.0,12.0,4.0,12.0,5.0])
cdef double[:,:] zeros2 = np.empty(shape=(999,2))


paraC2P = StringIO.StringIO('''1    10  0   0   (Intercept) 0   -4.981792
1   10  0   0   lv  50  0.55139
1   10  0   0   lv  51  0.53667
1   10  0   0   lv  52  0.52194
1   10  0   0   lv  53  0.50722
1   10  0   0   lv  54  0.49249
1   10  0   0   lv  55  0.47776
1   10  0   0   lv  56  0.46301
1   10  0   0   lv  57  0.44825
1   10  0   0   lv  58  0.43347
1   10  0   0   lv  59  0.41867
1   10  0   0   lv  60  0.40384
1   10  0   0   lv  61  0.38897
1   10  0   0   lv  62  0.37405
1   10  0   0   lv  63  0.35908
1   10  0   0   lv  64  0.34406
1   10  0   0   lv  65  0.32897
1   10  0   0   lv  66  0.31381
1   10  0   0   lv  67  0.29856
1   10  0   0   lv  68  0.28322
1   10  0   0   lv  69  0.26778
1   10  0   0   lv  70  0.25224
1   10  0   0   lv  71  0.23657
1   10  0   0   lv  72  0.22078
1   10  0   0   lv  73  0.20486
1   10  0   0   lv  74  0.18879
1   10  0   0   lv  75  0.17258
1   10  0   0   lv  76  0.1562
1   10  0   0   lv  77  0.13966
1   10  0   0   lv  78  0.12294
1   10  0   0   lv  79  0.10604
1   10  0   0   lv  80  0.08896
1   10  0   0   lv  81  0.07167
1   10  0   0   lv  82  0.05419
1   10  0   0   lv  83  0.0365
1   10  0   0   lv  84  0.0186
1   10  0   0   lv  85  0.00048
1   10  0   0   lv  86  -0.01785
1   10  0   0   lv  87  -0.03641
1   10  0   0   lv  88  -0.0552
1   10  0   0   lv  89  -0.07422
1   10  0   0   lv  90  -0.09347
1   10  0   0   lv  91  -0.11295
1   10  0   0   lv  92  -0.13267
1   10  0   0   lv  93  -0.15263
1   10  0   0   lv  94  -0.17282
1   10  0   0   lv  95  -0.19325
1   10  0   0   lv  96  -0.21392
1   10  0   0   lv  97  -0.23482
1   10  0   0   lv  98  -0.25596
1   10  0   0   lv  99  -0.27734
1   10  0   0   lv  100 -0.29895
1   10  0   0   lv  101 -0.32078
1   10  0   0   lv  102 -0.34285
1   10  0   0   lv  103 -0.36513
1   10  0   0   lv  104 -0.38764
1   10  0   0   lv  105 -0.41037
1   10  0   0   lv  106 -0.43331
1   10  0   0   lv  107 -0.45646
1   10  0   0   lv  108 -0.47982
1   10  0   0   lv  109 -0.50338
1   10  0   0   lv  110 -0.52713
1   10  0   0   lv  111 -0.55107
1   10  0   0   lv  112 -0.5752
1   10  0   0   lv  113 -0.59952
1   10  0   0   lv  114 -0.624
1   10  0   0   lv  115 -0.64866
1   10  0   0   lv  116 -0.67348
1   10  0   0   lv  117 -0.69846
1   10  0   0   lv  118 -0.72359
1   10  0   0   lv  119 -0.74887
1   10  0   0   lv  120 -0.77429
1   10  0   0   lv  121 -0.79985
1   10  0   0   lv  122 -0.82554
1   10  0   0   lv  123 -0.85135
1   10  0   0   lv  124 -0.87728
1   10  0   0   lv  125 -0.90332
1   10  0   0   lv  126 -0.92947
1   10  0   0   lv  127 -0.95572
1   10  0   0   lv  128 -0.98206
1   10  0   0   lv  129 -1.0085
1   10  0   0   lv  130 -1.03502
1   10  0   0   lv  131 -1.06162
1   10  0   0   lv  132 -1.0883
1   10  0   0   lv  133 -1.11504
1   10  0   0   lv  134 -1.14185
1   10  0   0   lv  135 -1.16872
1   10  0   0   lv  136 -1.19565
1   10  0   0   lv  137 -1.22263
1   10  0   0   lv  138 -1.24965
1   10  0   0   lv  139 -1.27672
1   10  0   0   lv  140 -1.30382
1   10  0   0   lv  141 -1.33097
1   10  0   0   lv  142 -1.35814
1   10  0   0   lv  143 -1.38534
1   10  0   0   lv  144 -1.41257
1   10  0   0   lv  145 -1.43982
1   10  0   0   lv  146 -1.46709
1   10  0   0   lv  147 -1.49437
1   10  0   0   lv  148 -1.52167
1   10  0   0   lv  149 -1.54898
1   10  0   0   lv  150 -1.57629
1   10  0   0   dollarSaving    -50 -1.10708
1   10  0   0   dollarSaving    -48 -1.08948
1   10  0   0   dollarSaving    -46 -1.07188
1   10  0   0   dollarSaving    -44 -1.05429
1   10  0   0   dollarSaving    -42 -1.03669
1   10  0   0   dollarSaving    -40 -1.0191
1   10  0   0   dollarSaving    -38 -1.0015
1   10  0   0   dollarSaving    -36 -0.98391
1   10  0   0   dollarSaving    -34 -0.96632
1   10  0   0   dollarSaving    -32 -0.94873
1   10  0   0   dollarSaving    -30 -0.93115
1   10  0   0   dollarSaving    -28 -0.91357
1   10  0   0   dollarSaving    -26 -0.896
1   10  0   0   dollarSaving    -24 -0.87843
1   10  0   0   dollarSaving    -22 -0.86087
1   10  0   0   dollarSaving    -20 -0.84331
1   10  0   0   dollarSaving    -18 -0.82577
1   10  0   0   dollarSaving    -16 -0.80823
1   10  0   0   dollarSaving    -14 -0.79071
1   10  0   0   dollarSaving    -12 -0.7732
1   10  0   0   dollarSaving    -10 -0.7557
1   10  0   0   dollarSaving    -8  -0.73821
1   10  0   0   dollarSaving    -6  -0.72074
1   10  0   0   dollarSaving    -4  -0.70329
1   10  0   0   dollarSaving    -2  -0.68586
1   10  0   0   dollarSaving    0   -0.66844
1   10  0   0   dollarSaving    2   -0.65105
1   10  0   0   dollarSaving    4   -0.63368
1   10  0   0   dollarSaving    6   -0.61634
1   10  0   0   dollarSaving    8   -0.59901
1   10  0   0   dollarSaving    10  -0.58172
1   10  0   0   dollarSaving    12  -0.56446
1   10  0   0   dollarSaving    14  -0.54722
1   10  0   0   dollarSaving    16  -0.53002
1   10  0   0   dollarSaving    18  -0.51285
1   10  0   0   dollarSaving    20  -0.49572
1   10  0   0   dollarSaving    22  -0.47862
1   10  0   0   dollarSaving    24  -0.46157
1   10  0   0   dollarSaving    26  -0.44455
1   10  0   0   dollarSaving    28  -0.42757
1   10  0   0   dollarSaving    30  -0.41064
1   10  0   0   dollarSaving    32  -0.39376
1   10  0   0   dollarSaving    34  -0.37692
1   10  0   0   dollarSaving    36  -0.36014
1   10  0   0   dollarSaving    38  -0.3434
1   10  0   0   dollarSaving    40  -0.32672
1   10  0   0   dollarSaving    42  -0.31009
1   10  0   0   dollarSaving    44  -0.29352
1   10  0   0   dollarSaving    46  -0.27701
1   10  0   0   dollarSaving    48  -0.26056
1   10  0   0   dollarSaving    50  -0.24417
1   10  0   0   dollarSaving    52  -0.22785
1   10  0   0   dollarSaving    54  -0.21159
1   10  0   0   dollarSaving    56  -0.1954
1   10  0   0   dollarSaving    58  -0.17928
1   10  0   0   dollarSaving    60  -0.16323
1   10  0   0   dollarSaving    62  -0.14725
1   10  0   0   dollarSaving    64  -0.13135
1   10  0   0   dollarSaving    66  -0.11553
1   10  0   0   dollarSaving    68  -0.09978
1   10  0   0   dollarSaving    70  -0.08411
1   10  0   0   dollarSaving    72  -0.06853
1   10  0   0   dollarSaving    74  -0.05303
1   10  0   0   dollarSaving    76  -0.03761
1   10  0   0   dollarSaving    78  -0.02229
1   10  0   0   dollarSaving    80  -0.00704
1   10  0   0   dollarSaving    82  0.00811
1   10  0   0   dollarSaving    84  0.02317
1   10  0   0   dollarSaving    86  0.03813
1   10  0   0   dollarSaving    88  0.05301
1   10  0   0   dollarSaving    90  0.06778
1   10  0   0   dollarSaving    92  0.08246
1   10  0   0   dollarSaving    94  0.09704
1   10  0   0   dollarSaving    96  0.11152
1   10  0   0   dollarSaving    98  0.1259
1   10  0   0   dollarSaving    100 0.14018
1   10  0   0   dollarSaving    102 0.15435
1   10  0   0   dollarSaving    104 0.16841
1   10  0   0   dollarSaving    106 0.18237
1   10  0   0   dollarSaving    108 0.19623
1   10  0   0   dollarSaving    110 0.20997
1   10  0   0   dollarSaving    112 0.2236
1   10  0   0   dollarSaving    114 0.23712
1   10  0   0   dollarSaving    116 0.25053
1   10  0   0   dollarSaving    118 0.26383
1   10  0   0   dollarSaving    120 0.27701
1   10  0   0   dollarSaving    122 0.29007
1   10  0   0   dollarSaving    124 0.30302
1   10  0   0   dollarSaving    126 0.31585
1   10  0   0   dollarSaving    128 0.32857
1   10  0   0   dollarSaving    130 0.34116
1   10  0   0   dollarSaving    132 0.35364
1   10  0   0   dollarSaving    134 0.36599
1   10  0   0   dollarSaving    136 0.37822
1   10  0   0   dollarSaving    138 0.39034
1   10  0   0   dollarSaving    140 0.40233
1   10  0   0   dollarSaving    142 0.4142
1   10  0   0   dollarSaving    144 0.42594
1   10  0   0   dollarSaving    146 0.43756
1   10  0   0   dollarSaving    148 0.44906
1   10  0   0   dollarSaving    150 0.46043
1   10  0   0   dollarSaving    152 0.47168
1   10  0   0   dollarSaving    154 0.4828
1   10  0   0   dollarSaving    156 0.49379
1   10  0   0   dollarSaving    158 0.50466
1   10  0   0   dollarSaving    160 0.51541
1   10  0   0   dollarSaving    162 0.52602
1   10  0   0   dollarSaving    164 0.53651
1   10  0   0   dollarSaving    166 0.54687
1   10  0   0   dollarSaving    168 0.55711
1   10  0   0   dollarSaving    170 0.56722
1   10  0   0   dollarSaving    172 0.5772
1   10  0   0   dollarSaving    174 0.58705
1   10  0   0   dollarSaving    176 0.59678
1   10  0   0   dollarSaving    178 0.60637
1   10  0   0   dollarSaving    180 0.61584
1   10  0   0   dollarSaving    182 0.62519
1   10  0   0   dollarSaving    184 0.6344
1   10  0   0   dollarSaving    186 0.64349
1   10  0   0   dollarSaving    188 0.65245
1   10  0   0   dollarSaving    190 0.66129
1   10  0   0   dollarSaving    192 0.67
1   10  0   0   dollarSaving    194 0.67858
1   10  0   0   dollarSaving    196 0.68704
1   10  0   0   dollarSaving    198 0.69537
1   10  0   0   dollarSaving    200 0.70358
1   10  0   0   dollarSaving    202 0.71167
1   10  0   0   dollarSaving    204 0.71963
1   10  0   0   dollarSaving    206 0.72746
1   10  0   0   dollarSaving    208 0.73517
1   10  0   0   dollarSaving    210 0.74276
1   10  0   0   dollarSaving    212 0.75023
1   10  0   0   dollarSaving    214 0.75758
1   10  0   0   dollarSaving    216 0.7648
1   10  0   0   dollarSaving    218 0.77191
1   10  0   0   dollarSaving    220 0.77889
1   10  0   0   dollarSaving    222 0.78576
1   10  0   0   dollarSaving    224 0.79251
1   10  0   0   dollarSaving    226 0.79914
1   10  0   0   dollarSaving    228 0.80565
1   10  0   0   dollarSaving    230 0.81205
1   10  0   0   dollarSaving    232 0.81833
1   10  0   0   dollarSaving    234 0.8245
1   10  0   0   dollarSaving    236 0.83055
1   10  0   0   dollarSaving    238 0.8365
1   10  0   0   dollarSaving    240 0.84233
1   10  0   0   dollarSaving    242 0.84805
1   10  0   0   dollarSaving    244 0.85366
1   10  0   0   dollarSaving    246 0.85916
1   10  0   0   dollarSaving    248 0.86455
1   10  0   0   dollarSaving    250 0.86984
1   10  0   0   dollarSaving    252 0.87502
1   10  0   0   dollarSaving    254 0.8801
1   10  0   0   dollarSaving    256 0.88507
1   10  0   0   dollarSaving    258 0.88994
1   10  0   0   dollarSaving    260 0.89471
1   10  0   0   dollarSaving    262 0.89938
1   10  0   0   dollarSaving    264 0.90395
1   10  0   0   dollarSaving    266 0.90843
1   10  0   0   dollarSaving    268 0.91281
1   10  0   0   dollarSaving    270 0.91709
1   10  0   0   dollarSaving    272 0.92128
1   10  0   0   dollarSaving    274 0.92539
1   10  0   0   dollarSaving    276 0.9294
1   10  0   0   dollarSaving    278 0.93332
1   10  0   0   dollarSaving    280 0.93715
1   10  0   0   dollarSaving    282 0.9409
1   10  0   0   dollarSaving    284 0.94457
1   10  0   0   dollarSaving    286 0.94815
1   10  0   0   dollarSaving    288 0.95165
1   10  0   0   dollarSaving    290 0.95507
1   10  0   0   dollarSaving    292 0.95842
1   10  0   0   dollarSaving    294 0.96169
1   10  0   0   dollarSaving    296 0.96488
1   10  0   0   dollarSaving    298 0.96801
1   10  0   0   dollarSaving    300 0.97106
1   10  0   0   dollarSaving    302 0.97404
1   10  0   0   dollarSaving    304 0.97696
1   10  0   0   dollarSaving    306 0.97981
1   10  0   0   dollarSaving    308 0.9826
1   10  0   0   dollarSaving    310 0.98532
1   10  0   0   dollarSaving    312 0.98799
1   10  0   0   dollarSaving    314 0.9906
1   10  0   0   dollarSaving    316 0.99315
1   10  0   0   dollarSaving    318 0.99565
1   10  0   0   dollarSaving    320 0.99809
1   10  0   0   dollarSaving    322 1.00049
1   10  0   0   dollarSaving    324 1.00283
1   10  0   0   dollarSaving    326 1.00513
1   10  0   0   dollarSaving    328 1.00738
1   10  0   0   dollarSaving    330 1.00959
1   10  0   0   dollarSaving    332 1.01176
1   10  0   0   dollarSaving    334 1.01389
1   10  0   0   dollarSaving    336 1.01598
1   10  0   0   dollarSaving    338 1.01803
1   10  0   0   dollarSaving    340 1.02005
1   10  0   0   dollarSaving    342 1.02204
1   10  0   0   dollarSaving    344 1.02399
1   10  0   0   dollarSaving    346 1.02592
1   10  0   0   dollarSaving    348 1.02782
1   10  0   0   dollarSaving    350 1.02969
1   10  0   0   dollarSaving    352 1.03154
1   10  0   0   dollarSaving    354 1.03337
1   10  0   0   dollarSaving    356 1.03518
1   10  0   0   dollarSaving    358 1.03696
1   10  0   0   dollarSaving    360 1.03873
1   10  0   0   dollarSaving    362 1.04049
1   10  0   0   dollarSaving    364 1.04222
1   10  0   0   dollarSaving    366 1.04395
1   10  0   0   dollarSaving    368 1.04566
1   10  0   0   dollarSaving    370 1.04736
1   10  0   0   dollarSaving    372 1.04906
1   10  0   0   dollarSaving    374 1.05074
1   10  0   0   dollarSaving    376 1.05242
1   10  0   0   dollarSaving    378 1.05409
1   10  0   0   dollarSaving    380 1.05576
1   10  0   0   dollarSaving    382 1.05742
1   10  0   0   dollarSaving    384 1.05908
1   10  0   0   dollarSaving    386 1.06073
1   10  0   0   dollarSaving    388 1.06238
1   10  0   0   dollarSaving    390 1.06403
1   10  0   0   dollarSaving    392 1.06568
1   10  0   0   dollarSaving    394 1.06733
1   10  0   0   dollarSaving    396 1.06898
1   10  0   0   dollarSaving    398 1.07063
1   10  0   0   dollarSaving    400 1.07228
1   10  0   0   fo  500 0.58651
1   10  0   0   fo  501 0.58111
1   10  0   0   fo  502 0.57571
1   10  0   0   fo  503 0.57031
1   10  0   0   fo  504 0.56492
1   10  0   0   fo  505 0.55952
1   10  0   0   fo  506 0.55412
1   10  0   0   fo  507 0.54872
1   10  0   0   fo  508 0.54332
1   10  0   0   fo  509 0.53793
1   10  0   0   fo  510 0.53253
1   10  0   0   fo  511 0.52713
1   10  0   0   fo  512 0.52173
1   10  0   0   fo  513 0.51633
1   10  0   0   fo  514 0.51093
1   10  0   0   fo  515 0.50554
1   10  0   0   fo  516 0.50014
1   10  0   0   fo  517 0.49474
1   10  0   0   fo  518 0.48934
1   10  0   0   fo  519 0.48394
1   10  0   0   fo  520 0.47855
1   10  0   0   fo  521 0.47315
1   10  0   0   fo  522 0.46775
1   10  0   0   fo  523 0.46235
1   10  0   0   fo  524 0.45695
1   10  0   0   fo  525 0.45156
1   10  0   0   fo  526 0.44616
1   10  0   0   fo  527 0.44076
1   10  0   0   fo  528 0.43536
1   10  0   0   fo  529 0.42996
1   10  0   0   fo  530 0.42457
1   10  0   0   fo  531 0.41917
1   10  0   0   fo  532 0.41377
1   10  0   0   fo  533 0.40837
1   10  0   0   fo  534 0.40297
1   10  0   0   fo  535 0.39758
1   10  0   0   fo  536 0.39218
1   10  0   0   fo  537 0.38678
1   10  0   0   fo  538 0.38138
1   10  0   0   fo  539 0.37598
1   10  0   0   fo  540 0.37059
1   10  0   0   fo  541 0.36519
1   10  0   0   fo  542 0.35979
1   10  0   0   fo  543 0.35439
1   10  0   0   fo  544 0.34899
1   10  0   0   fo  545 0.34359
1   10  0   0   fo  546 0.3382
1   10  0   0   fo  547 0.3328
1   10  0   0   fo  548 0.3274
1   10  0   0   fo  549 0.322
1   10  0   0   fo  550 0.3166
1   10  0   0   fo  551 0.31121
1   10  0   0   fo  552 0.30581
1   10  0   0   fo  553 0.30041
1   10  0   0   fo  554 0.29501
1   10  0   0   fo  555 0.28961
1   10  0   0   fo  556 0.28422
1   10  0   0   fo  557 0.27882
1   10  0   0   fo  558 0.27342
1   10  0   0   fo  559 0.26802
1   10  0   0   fo  560 0.26262
1   10  0   0   fo  561 0.25723
1   10  0   0   fo  562 0.25183
1   10  0   0   fo  563 0.24643
1   10  0   0   fo  564 0.24103
1   10  0   0   fo  565 0.23563
1   10  0   0   fo  566 0.23024
1   10  0   0   fo  567 0.22484
1   10  0   0   fo  568 0.21944
1   10  0   0   fo  569 0.21404
1   10  0   0   fo  570 0.20864
1   10  0   0   fo  571 0.20325
1   10  0   0   fo  572 0.19785
1   10  0   0   fo  573 0.19245
1   10  0   0   fo  574 0.18705
1   10  0   0   fo  575 0.18165
1   10  0   0   fo  576 0.17625
1   10  0   0   fo  577 0.17086
1   10  0   0   fo  578 0.16546
1   10  0   0   fo  579 0.16006
1   10  0   0   fo  580 0.15466
1   10  0   0   fo  581 0.14926
1   10  0   0   fo  582 0.14387
1   10  0   0   fo  583 0.13847
1   10  0   0   fo  584 0.13307
1   10  0   0   fo  585 0.12767
1   10  0   0   fo  586 0.12227
1   10  0   0   fo  587 0.11688
1   10  0   0   fo  588 0.11148
1   10  0   0   fo  589 0.10608
1   10  0   0   fo  590 0.10068
1   10  0   0   fo  591 0.09528
1   10  0   0   fo  592 0.08989
1   10  0   0   fo  593 0.08449
1   10  0   0   fo  594 0.07909
1   10  0   0   fo  595 0.07369
1   10  0   0   fo  596 0.06829
1   10  0   0   fo  597 0.0629
1   10  0   0   fo  598 0.0575
1   10  0   0   fo  599 0.0521
1   10  0   0   fo  600 0.0467
1   10  0   0   fo  601 0.0413
1   10  0   0   fo  602 0.03591
1   10  0   0   fo  603 0.03051
1   10  0   0   fo  604 0.02511
1   10  0   0   fo  605 0.01972
1   10  0   0   fo  606 0.01433
1   10  0   0   fo  607 0.00895
1   10  0   0   fo  608 0.00357
1   10  0   0   fo  609 -0.0018
1   10  0   0   fo  610 -0.00716
1   10  0   0   fo  611 -0.01251
1   10  0   0   fo  612 -0.01784
1   10  0   0   fo  613 -0.02316
1   10  0   0   fo  614 -0.02846
1   10  0   0   fo  615 -0.03374
1   10  0   0   fo  616 -0.03899
1   10  0   0   fo  617 -0.04422
1   10  0   0   fo  618 -0.04942
1   10  0   0   fo  619 -0.05459
1   10  0   0   fo  620 -0.05972
1   10  0   0   fo  621 -0.06482
1   10  0   0   fo  622 -0.06988
1   10  0   0   fo  623 -0.07489
1   10  0   0   fo  624 -0.07986
1   10  0   0   fo  625 -0.08478
1   10  0   0   fo  626 -0.08964
1   10  0   0   fo  627 -0.09445
1   10  0   0   fo  628 -0.09921
1   10  0   0   fo  629 -0.1039
1   10  0   0   fo  630 -0.10853
1   10  0   0   fo  631 -0.11309
1   10  0   0   fo  632 -0.11758
1   10  0   0   fo  633 -0.122
1   10  0   0   fo  634 -0.12634
1   10  0   0   fo  635 -0.1306
1   10  0   0   fo  636 -0.13478
1   10  0   0   fo  637 -0.13888
1   10  0   0   fo  638 -0.14288
1   10  0   0   fo  639 -0.1468
1   10  0   0   fo  640 -0.15063
1   10  0   0   fo  641 -0.15436
1   10  0   0   fo  642 -0.15799
1   10  0   0   fo  643 -0.16152
1   10  0   0   fo  644 -0.16494
1   10  0   0   fo  645 -0.16826
1   10  0   0   fo  646 -0.17147
1   10  0   0   fo  647 -0.17457
1   10  0   0   fo  648 -0.17756
1   10  0   0   fo  649 -0.18043
1   10  0   0   fo  650 -0.18319
1   10  0   0   fo  651 -0.18582
1   10  0   0   fo  652 -0.18834
1   10  0   0   fo  653 -0.19073
1   10  0   0   fo  654 -0.19299
1   10  0   0   fo  655 -0.19513
1   10  0   0   fo  656 -0.19714
1   10  0   0   fo  657 -0.19901
1   10  0   0   fo  658 -0.20076
1   10  0   0   fo  659 -0.20237
1   10  0   0   fo  660 -0.20385
1   10  0   0   fo  661 -0.20519
1   10  0   0   fo  662 -0.20639
1   10  0   0   fo  663 -0.20746
1   10  0   0   fo  664 -0.20838
1   10  0   0   fo  665 -0.20917
1   10  0   0   fo  666 -0.20981
1   10  0   0   fo  667 -0.21031
1   10  0   0   fo  668 -0.21067
1   10  0   0   fo  669 -0.21088
1   10  0   0   fo  670 -0.21095
1   10  0   0   fo  671 -0.21087
1   10  0   0   fo  672 -0.21065
1   10  0   0   fo  673 -0.21028
1   10  0   0   fo  674 -0.20977
1   10  0   0   fo  675 -0.20911
1   10  0   0   fo  676 -0.20831
1   10  0   0   fo  677 -0.20736
1   10  0   0   fo  678 -0.20626
1   10  0   0   fo  679 -0.20502
1   10  0   0   fo  680 -0.20363
1   10  0   0   fo  681 -0.2021
1   10  0   0   fo  682 -0.20042
1   10  0   0   fo  683 -0.1986
1   10  0   0   fo  684 -0.19664
1   10  0   0   fo  685 -0.19453
1   10  0   0   fo  686 -0.19228
1   10  0   0   fo  687 -0.1899
1   10  0   0   fo  688 -0.18737
1   10  0   0   fo  689 -0.1847
1   10  0   0   fo  690 -0.1819
1   10  0   0   fo  691 -0.17896
1   10  0   0   fo  692 -0.17588
1   10  0   0   fo  693 -0.17267
1   10  0   0   fo  694 -0.16933
1   10  0   0   fo  695 -0.16586
1   10  0   0   fo  696 -0.16226
1   10  0   0   fo  697 -0.15853
1   10  0   0   fo  698 -0.15468
1   10  0   0   fo  699 -0.1507
1   10  0   0   fo  700 -0.1466
1   10  0   0   fo  701 -0.14238
1   10  0   0   fo  702 -0.13805
1   10  0   0   fo  703 -0.1336
1   10  0   0   fo  704 -0.12903
1   10  0   0   fo  705 -0.12436
1   10  0   0   fo  706 -0.11957
1   10  0   0   fo  707 -0.11468
1   10  0   0   fo  708 -0.10969
1   10  0   0   fo  709 -0.1046
1   10  0   0   fo  710 -0.0994
1   10  0   0   fo  711 -0.09412
1   10  0   0   fo  712 -0.08874
1   10  0   0   fo  713 -0.08326
1   10  0   0   fo  714 -0.0777
1   10  0   0   fo  715 -0.07206
1   10  0   0   fo  716 -0.06633
1   10  0   0   fo  717 -0.06053
1   10  0   0   fo  718 -0.05465
1   10  0   0   fo  719 -0.04869
1   10  0   0   fo  720 -0.04267
1   10  0   0   fo  721 -0.03658
1   10  0   0   fo  722 -0.03042
1   10  0   0   fo  723 -0.02421
1   10  0   0   fo  724 -0.01793
1   10  0   0   fo  725 -0.0116
1   10  0   0   fo  726 -0.00522
1   10  0   0   fo  727 0.00121
1   10  0   0   fo  728 0.00769
1   10  0   0   fo  729 0.01421
1   10  0   0   fo  730 0.02077
1   10  0   0   fo  731 0.02737
1   10  0   0   fo  732 0.034
1   10  0   0   fo  733 0.04066
1   10  0   0   fo  734 0.04735
1   10  0   0   fo  735 0.05407
1   10  0   0   fo  736 0.06081
1   10  0   0   fo  737 0.06758
1   10  0   0   fo  738 0.07436
1   10  0   0   fo  739 0.08115
1   10  0   0   fo  740 0.08797
1   10  0   0   fo  741 0.09479
1   10  0   0   fo  742 0.10162
1   10  0   0   fo  743 0.10846
1   10  0   0   fo  744 0.11531
1   10  0   0   fo  745 0.12216
1   10  0   0   fo  746 0.12902
1   10  0   0   fo  747 0.13588
1   10  0   0   fo  748 0.14274
1   10  0   0   fo  749 0.1496
1   10  0   0   fo  750 0.15646
1   10  0   0   xcumC   0   -0.64115
1   10  0   0   xcumC   1   -0.622
1   10  0   0   xcumC   2   -0.60285
1   10  0   0   xcumC   3   -0.5837
1   10  0   0   xcumC   4   -0.56455
1   10  0   0   xcumC   5   -0.54541
1   10  0   0   xcumC   6   -0.52628
1   10  0   0   xcumC   7   -0.50716
1   10  0   0   xcumC   8   -0.48805
1   10  0   0   xcumC   9   -0.46896
1   10  0   0   xcumC   10  -0.4499
1   10  0   0   xcumC   11  -0.43086
1   10  0   0   xcumC   12  -0.41186
1   10  0   0   xcumC   13  -0.39291
1   10  0   0   xcumC   14  -0.374
1   10  0   0   xcumC   15  -0.35515
1   10  0   0   xcumC   16  -0.33636
1   10  0   0   xcumC   17  -0.31764
1   10  0   0   xcumC   18  -0.29899
1   10  0   0   xcumC   19  -0.28044
1   10  0   0   xcumC   20  -0.26197
1   10  0   0   xcumC   21  -0.24361
1   10  0   0   xcumC   22  -0.22536
1   10  0   0   xcumC   23  -0.20722
1   10  0   0   xcumC   24  -0.18921
1   10  0   0   xcumC   25  -0.17133
1   10  0   0   xcumC   26  -0.1536
1   10  0   0   xcumC   27  -0.13602
1   10  0   0   xcumC   28  -0.11859
1   10  0   0   xcumC   29  -0.10134
1   10  0   0   xcumC   30  -0.08426
1   10  0   0   xcumC   31  -0.06736
1   10  0   0   xcumC   32  -0.05066
1   10  0   0   xcumC   33  -0.03416
1   10  0   0   xcumC   34  -0.01786
1   10  0   0   xcumC   35  -0.00179
''')



matParaC2P = np.genfromtxt(paraC2P, names=  ['pt1','ct','mods','mbas','para','x','y'], dtype=['<f8','<f8','<f8','<f8','|S30','<f8','<f8'], delimiter='\t')


cdef packed struct paraArray:
    double pt1
    double ct
    double mods
    double mbas
    char[30] para
    double x
    double y

@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
cpdef genc2p(paraArray [:] mat_p = matParaC2P, double pt_p= 1.0, double ct_p=10.0, double mbas_p =0.0, double mods_p=0.0, double count_c_p=24.0, double lv_p=60.0, double fo_p=740.0, double incentive_p = 200.0):
    cdef double[:,:] lvsubmat = getSubParaMat(input_s = mat_p, para_s='lv', ct_s = ct_p, pt1_s=pt_p, mbas_s=mbas_p,mods_s=0.0)
    cdef double lv0 = interp2d(lvsubmat[:,0], lvsubmat[:,1], lv_p)
    cdef double[:,:] fosubmat = getSubParaMat(input_s = mat_p, para_s='fo', ct_s = ct_p, pt1_s=pt_p, mbas_s=mbas_p,mods_s=0.0)
    cdef double fo0 = interp2d(fosubmat[:,0], fosubmat[:,1], fo_p)
    cdef double[:,:] cumCsubmat = getSubParaMat(input_s = mat_p, para_s='xcumC', ct_s = ct_p, pt1_s=pt_p, mbas_s=mbas_p,mods_s=0.0)
    cdef double cumC0 = interp2d(cumCsubmat[:,0], cumCsubmat[:,1], count_c_p)
    cdef double[:,:] incsubmat = getSubParaMat(input_s= mat_p, para_s='dollarSaving', ct_s = ct_p, pt1_s=pt_p, mbas_s=mbas_p,mods_s=mods_p)
    cdef double inc0 = interp2d(incsubmat[:,0], incsubmat[:,1], incentive_p)
    cdef double[:,:] intercept = getSubParaMat(input_s= mat_p, para_s='(Intercept)', ct_s = ct_p, pt1_s=pt_p, mbas_s=mbas_p,mods_s=0.0)
    cdef double intercept0 = intercept[0,1]
    return genLogit(lv0+fo0+cumC0+inc0+intercept0)
    #return intercept0


cpdef double[:,:] getSubParaMat(paraArray [:] input_s = matParaC2P, double pt1_s =1.0, double ct_s=10.0, double mods_s=0.0, double mbas_s=0.0, char[30] para_s = 'lv'):
    cdef int start = 0, end =0, k=0
    cdef double[:,:] output = zeros2
    for i from 0<=i<input_s.size:
        if input_s[i].pt1 == pt1_s and input_s[i].ct == ct_s and input_s[i].mods==mods_s and input_s[i].mbas==mbas_s and input_s[i].para[:]==para_s[:] and start==0:
            start = i
            end = i
            while end<input_s.size and input_s[end].pt1 == pt1_s and input_s[end].ct == ct_s and input_s[end].mods==mods_s and input_s[end].mbas==mbas_s and input_s[end].para[:]==para_s[:]:
                (output[k,0], output[k, 1]) = (input_s[end].x, input_s[end].y)
                end += 1
                k+=1
            break
    return output[:k, :]


cpdef double interp2d(double[:] x, double[:] y, double new_x, int ex = 0):
    cdef int nx = x.shape[0]-1
    cdef int ny = y.shape[0]-1
    cdef double new_y
    cdef int steps=0
    if ex==0 and new_x<x[0]:
        new_x = x[0]
    elif ex==0 and new_x>x[nx]:
        new_x = x[nx]
    if new_x<=x[0]:
        new_y = (new_x - x[0])*(y[0]-y[1])/(x[0] - x[1]) + y[0]
    elif new_x>=x[nx]:
        new_y = (new_x - x[nx])*(y[ny] - y[ny-1])/(x[nx] - x[nx-1]) + y[ny]
    else:
        while new_x>x[steps]:
            steps +=1
        new_y = (new_x - x[steps-1])*(y[steps] - y[steps-1])/(x[steps] - x[steps-1]) + y[steps-1]
    return new_y


cpdef double genLogit(double total):
    return 1.0/(1.0+exp(-1.0*(total)))


cpdef genLoan():
    for j from 0<=j<100:
        for i from 1<=i<240:
            prob_p = genc2p(mat_p = matParaC2P)
\$\endgroup\$
  • \$\begingroup\$ I don't use cython, but that's a terrible profiler if I've ever seen one. Is there a different one you can use? \$\endgroup\$ – Schism Aug 20 '14 at 3:16
  • 1
    \$\begingroup\$ I'll note that genc2p is called 24 000 times, so interp2d is being called 96 000 times and getSubParaMat is called 120 000 times. You're also iterating through input_s in each call of getSubParaMat, and you have nested while loops in each of those. I'm sorry I can't help any more though. \$\endgroup\$ – Schism Aug 20 '14 at 3:20
  • \$\begingroup\$ It would be really helpful if you could prune this example down a bit. Also, the example string input you give is not copy-pasteable, since SE converts tabs to spaces. \$\endgroup\$ – ali_m Aug 20 '14 at 12:36
4
\$\begingroup\$

There's a lot of code here, so I'm just going to review interp2d.

  1. There's no docstring. What does this function do? How am I supposed to call it? Are there any constraints on the parameters (for example, does the x array need to be sorted)?.

  2. The function seems to be misnamed: it interpolates the function that takes x to y, but this is a function of one argument, so surely interp1d or just interp would be better names. (Compare numpy.interp, which does something very similar to your function, but is documentated as "one-dimensional linear interpolation".)

  3. The parameter ex is opaquely named. What does it mean? Reading the code, it seems that it controls whether or not to extrapolate for values of x outside the given range. So it should be named extrapolate and it should be a Boolean (True or False) not a number.

  4. Python allows indexing from the end of an array, so you can write x[-1] for the last element of an array, instead of the clusmy x[x.shape[0] - 1].

  5. You find the interval in which to do the interpolation by a linear search over the array x, which takes time proportional to the size of x and so will be slow when x is large. Since x needs to be sorted in order for this algorithm to make sense, you should use binary search (for example, numpy.searchsorted) so that the time taken is logarithmic in the size of x.

  6. You have failed to vectorize this function. The whole point of NumPy is that it provides fast operations on arrays of fixed-size numbers. If you find yourself writing a function that operates on just one value at a time (here, the single value new_x) then you probably aren't getting much, if any, benefit from NumPy.

  7. Putting all this together, I'd write something like this:

    def interp1d(x, y, a, extrapolate=False):
        """Interpolate a 1-D function.
    
        x is a 1-dimensional array sorted into ascending order.
        y is an array whose first axis has the same length as x.
        a is an array of interpolants.
        If extrapolate is False, clamp all interpolants to the range of x.
    
        Let f be the piecewise-linear function such that f(x) = y.
        Then return f(a).
    
        >>> x = np.arange(0, 10, 0.5)
        >>> y = np.sin(x)
        >>> interp1d(x, y, np.pi)
        0.0018202391415163
    
        """
        if not extrapolate:
            a = np.clip(a, x[0], x[-1])
        i = np.clip(np.searchsorted(x, a), 1, len(x) - 1)
        j = i - 1
        xj, yj = x[j], y[j]
        return yj + (a - xj) * (y[i] - yj) / (x[i] - xj)
    

    (The rest of the program may need to be reorganized to pass arrays of values instead of one value at a time.)

  8. Finally, what was wrong with scipy.interpolate.interp1d? It doesn't provide quite the same handling of points outside the range, but you could call numpy.clip yourself before calling it.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.