如何判断所有列的最后一个值是否大于n

How to determine if the last value in all columns is greater than n

我有一个模拟器,目前可以获取股票代码并将其潜在价格点绘制在 matplotlib 图表上:

ticker = 'PDN'
style.use('ggplot')

start = dt.datetime(2016, 6, 21)
end = dt.datetime.today()
prices = web.DataReader(ticker, 'yahoo', start, end)['Close']
returns = prices.pct_change()
last_price = prices[-1]

number_of_simulations = 500
num_days = 252
simulation_df = pd.DataFrame()

for x in range(number_of_simulations):
    counter = 0
    daily_vol = returns.std()

    price_series = []

    price = last_price * (1 + np.random.normal(0, daily_vol))
    price_series.append(price)

    for y in range(num_days):
        if counter == 251:
            break

        price = price_series[counter] * (1 + np.random.normal(0, daily_vol))
        price_series.append(price)
        counter += 1

    simulation_df[x] = price_series


fig = plt.figure()
fig.suptitle('Monte Carlo Sim: ' + ticker)
plt.plot(simulation_df)
plt.axhline(y = last_price, color = 'r', linestyle = '-')
plt.xlabel('Day')
plt.ylabel('Price')

plt.show()

如何显示上面出现的模拟量 last_price

编辑:

我正在尝试根据模拟结束时高于 last_price 的价格数量获得概率。

例如,如果 last_price 是 4 并且我 运行 模拟了 500 次,我想知道 price_series 中超过 4 的价格在完成时的数量模拟。

因此,如果 price_series 中的 250 个价格高于 last_price,我希望它打印 250 prices above last price

  • 使用 .gt 到 return 一个 Boolean 的值大于 last_price
  • 使用.iloc[-1, :]
  • 获取所有列的最后一行
  • 使用.value_counts得到TrueFalse
  • 的计数
res = simulation_df.iloc[-1, :].gt(last_price).value_counts()

[res]:
False    259
True     241
Name: 251, dtype: int64

fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(14, 6))
simulation_df.plot(ax=ax1, xlabel='Day', ylabel='Price', title=f'Monte Carlo Sim: {ticker}', legend=False)
ax1.axhline(y = last_price, color = 'r', linestyle = '-')

res.plot(kind='bar', rot=0, title=f'Simulations with Final Price \nGreater Than {last_price:0.2f}', ax=ax2)
ax2.bar_label(ax.containers[0], label_type='edge')

  • 在这里我们可以看到每个模拟的值和Boolean
simulation_df.iloc[-1:, :]
[out]:
           0          1          2          3          4          5          6          7          8          9          10         11         12         13         14         15         16         17         18         19        20         21         22         23         24         25         26         27         28         29         30         31         32         33         34         35         36         37         38         39         40        41         42         43         44        45        46        47         48         49         50         51         52         53         54         55        56         57         58         59         60         61         62         63         64         65         66         67         68         69         70         71         72         73         74         75         76        77         78         79         80        81         82         83         84        85        86         87         88         89         90         91         92         93         94         95         96         97         98         99         100        101        102       103        104        105        106        107        108        109        110        111        112        113        114        115        116        117        118        119        120        121        122        123       124        125        126        127        128        129       130        131        132        133        134        135        136        137        138        139        140        141        142        143        144        145        146        147       148        149        150        151        152        153        154        155        156        157      158       159        160        161        162        163        164        165        166        167        168        169        170        171        172        173        174        175        176        177        178        179        180        181        182        183        184        185        186        187        188        189        190        191        192        193        194        195        196        197        198        199        200       201       202        203        204        205        206        207        208        209        210        211       212        213        214        215        216        217        218        219        220        221        222        223        224        225        226        227        228        229        230        231       232        233        234        235        236        237        238        239        240       241        242        243        244        245        246        247       248        249        250       251       252        253        254       255        256        257        258       259        260      261        262        263        264        265        266        267        268        269        270        271        272        273        274        275        276        277        278        279        280        281       282        283        284        285        286        287        288        289        290        291        292        293        294        295        296        297        298        299        300       301        302        303       304        305        306        307        308        309        310        311        312        313        314        315        316        317        318        319        320        321        322        323        324        325        326        327        328       329       330        331        332        333        334        335        336        337        338        339        340        341        342        343        344        345        346        347        348        349        350        351        352        353        354        355        356        357        358        359        360        361        362        363        364        365        366      367       368        369        370        371        372        373        374        375        376       377        378        379       380        381        382        383        384        385        386        387        388       389        390        391        392        393        394        395        396        397        398        399        400        401        402        403        404        405        406        407        408        409        410        411        412        413        414        415        416        417        418        419        420        421        422        423        424        425        426        427        428        429        430        431        432        433        434        435        436        437        438        439        440        441        442        443        444        445        446        447        448        449        450        451        452        453        454        455       456        457        458        459       460        461        462        463        464        465        466        467        468        469        470        471       472       473        474        475       476        477        478        479        480        481        482        483        484        485       486        487        488        489        490        491        492        493        494       495        496        497        498        499
251  40.855915  25.640301  50.693303  44.023614  29.942804  41.920965  39.430077  30.287864  46.901486  40.062016  34.413895  29.344321  38.129262  35.562871  43.878346  36.285428  40.344195  38.597264  45.799781  35.118803  32.83081  35.600166  31.418348  35.756048  34.056794  32.377976  33.453087  26.727197  45.164206  40.158221  49.263849  24.238894  39.240774  42.817753  45.943722  30.942606  40.726633  26.540864  38.005849  25.825681  39.991136  30.09845  47.677947  36.321087  42.869729  36.40385  36.78647  32.70447  44.153209  44.789974  29.362195  42.138189  37.631511  33.924148  30.591147  44.016539  42.98707  25.007736  34.115459  47.054218  53.145284  40.189995  44.731036  40.570369  44.762215  46.457627  39.830706  33.202047  35.433265  42.063941  39.102714  41.064516  25.851312  48.018641  42.969446  46.504681  38.108579  46.28159  35.738122  37.231233  27.252866  35.53253  38.031299  36.126276  40.563356  40.54352  34.04269  39.809459  40.288753  36.269328  37.791311  43.832391  36.447865  26.920694  38.421288  39.042681  31.215561  34.815419  53.112216  45.066397  36.169979  40.886545  42.582102  38.72763  48.431439  31.814367  35.740232  49.684038  34.213517  37.605503  28.643752  34.568333  29.841837  36.203857  36.425014  38.918877  49.890237  28.826627  52.237877  40.793962  38.614708  38.122995  37.598773  40.947161  33.51841  31.823795  33.236405  31.779156  37.249006  46.192083  32.72734  52.978747  43.900892  25.323094  41.483599  50.010067  33.590545  43.348554  40.043626  27.539256  54.355179  35.651842  33.592941  29.638326  44.659418  41.700115  39.957892  38.863896  34.16594  43.371738  46.344883  33.523403  31.234028  40.628669  28.829392  32.199562  48.291964  33.784433  31.7044  48.56218  38.596052  38.025864  32.670343  38.086486  49.829264  35.210974  52.233563  30.255959  41.567659  47.571851  34.832187  33.141691  41.156277  53.633788  34.710377  35.350705  30.378631  31.906148  44.135636  41.575441  45.154519  30.471229  31.179558  41.406688  49.135382  49.786557  50.232863  35.446094  39.563712  50.208991  36.253971  35.040563  36.600378  37.112624  33.848333  46.733337  36.944805  34.331206  34.944138  39.723268  39.659194  37.88296  32.45357  36.154124  29.074326  42.737439  36.284944  40.678968  41.146663  44.093906  33.126406  45.824828  41.37231  34.441153  33.495475  37.618837  29.569943  22.647538  33.444178  60.173991  26.842564  51.167586  40.185917  45.180057  41.342309  31.781825  41.612493  40.117101  39.535119  27.371335  28.184017  33.962494  29.39672  32.117928  39.411364  39.268301  45.013207  45.649696  39.823967  31.780107  33.742755  43.23369  35.877269  29.668803  32.718458  35.220299  41.197486  46.445858  41.99952  53.128441  42.868252  42.73364  41.16791  35.707717  41.756247  33.38655  32.007061  44.104624  48.931682  31.24579  56.493701  27.6561  38.950942  34.029482  34.857725  40.179171  35.110987  31.525206  36.252121  27.403918  35.832982  39.875417  38.057676  40.688748  32.400066  32.730502  34.775845  32.959225  34.084925  36.780572  31.888236  25.841151  42.76338  40.626112  53.101078  33.808038  39.023637  37.868076  29.315238  53.661011  41.102963  45.444108  42.141841  33.043901  31.653144  25.713386  32.363687  31.517243  51.831122  41.678572  30.390078  31.37178  34.106836  35.269581  44.72275  47.347668  35.176777  39.662001  40.351962  41.767312  30.630819  40.547984  39.728632  39.911709  46.810056  44.472718  35.706436  31.800223  46.617443  39.138519  46.172246  39.162627  39.846782  36.249235  36.023444  27.743788  33.152005  39.706168  31.118545  38.35658  44.03662  30.500257  41.087599  54.213662  42.207963  31.046056  24.262964  46.519919  38.608789  42.230698  43.133583  39.824389  44.945088  35.102184  43.778711  38.458147  29.468663  40.146654  35.785838  26.282946  32.012363  45.123286  33.567002  37.308091  31.785537  41.515803  38.514655  34.188416  46.942958  39.491936  35.421798  52.588823  23.719545  29.011848  38.014521  42.558341  41.823499  45.0463  44.76837  37.289019  28.106492  37.934772  31.001834  45.393037  31.439607  36.820191  36.579662  39.91516  32.837042  48.398255  32.27708  42.606464  40.813862  31.247971  48.819663  40.972179  49.028832  43.711345  41.011618  33.65084  33.060376  35.276123  39.268903  34.282186  42.466964  34.532674  47.015342  37.052616  28.151835  42.695932  37.244537  39.238347  31.272434  62.710279  49.628861  35.112308  35.881238  36.516698  33.808105  36.561232  40.935168  35.305627  34.337012  32.214898  51.598472  36.718695  46.546647  32.769513  36.272245  33.216237  42.610709  30.385475  51.857045  45.530473  24.058967  33.723272  44.112363  51.182444  49.708666  47.038852  37.247625  26.486865  51.113427  44.462653  33.523164  36.112279  34.999821  45.791838  43.472233  33.405719  39.206421  25.295449  29.965772  36.976642  32.327498  37.549199  41.718816  42.771669  38.155392  36.436803  35.317047  33.275542  34.469672  41.227772  40.807429  25.825658  47.18341  39.141284  45.039577  49.116326  37.17708  36.892309  32.776463  34.499641  44.079595  26.026153  34.850848  46.515759  33.491574  47.712159  66.767126  28.815782  38.25052  35.89885  43.677117  45.624847  39.02742  39.236109  40.590124  55.893069  39.629833  65.119539  33.180381  31.056857  37.401845  48.850696  32.05405  34.450518  40.810174  39.074104  37.322891  35.385588  35.677069  31.185545  61.429976  40.00021  24.436327  27.219327  39.769648  42.179461

simulation_df.iloc[-1:, :].gt(last_price)
[out]:
      0      1     2     3      4     5     6      7     8     9      10     11     12     13    14     15    16    17    18     19     20     21     22     23     24     25     26     27    28    29    30     31    32    33    34     35    36     37     38     39    40     41    42     43    44     45     46     47    48    49     50    51     52     53     54    55    56     57     58    59    60    61    62    63    64    65    66     67     68    69    70    71     72    73    74    75     76    77     78     79     80     81     82     83    84    85     86    87    88     89     90    91     92     93    94    95     96     97    98    99     100   101   102   103   104    105    106   107    108    109    110    111    112    113    114   115   116    117   118   119   120    121    122   123    124    125    126    127    128   129    130   131   132    133   134   135    136   137   138    139   140    141    142    143   144   145   146   147    148   149   150    151    152   153    154    155   156    157    158   159   160    161    162    163   164    165   166    167   168   169    170    171   172   173    174    175    176    177   178   179   180    181    182   183   184   185   186    187   188   189    190    191    192    193    194   195    196    197    198   199   200    201    202    203    204   205    206   207   208   209    210   211   212    213    214    215    216    217    218   219    220   221   222   223   224    225   226   227   228    229    230    231    232    233   234   235   236   237   238    239    240   241    242    243    244    245   246   247   248   249   250   251   252    253   254    255    256   257   258    259   260    261   262    263    264   265    266    267    268    269    270   271    272   273    274    275    276    277    278    279    280    281   282   283   284    285   286    287    288   289   290   291   292    293    294    295    296    297   298   299    300    301    302    303   304   305    306   307   308   309    310   311   312   313   314   315    316    317   318   319   320   321   322    323    324    325    326   327    328   329   330    331   332   333   334    335    336   337   338   339   340   341   342    343   344   345    346   347    348    349    350   351    352    353    354   355   356    357   358   359    360   361    362    363    364   365   366   367   368    369    370    371    372   373    374    375    376   377    378   379    380   381   382    383   384   385   386   387   388    389    390    391   392    393   394    395   396    397    398   399    400   401    402   403   404    405    406    407    408    409   410    411    412    413   414    415   416    417    418    419   420    421   422   423    424    425   426   427   428   429    430    431   432   433    434    435    436   437   438    439   440    441    442    443    444    445   446   447    448    449    450    451    452   453   454    455   456   457   458   459    460    461    462    463   464    465    466   467    468   469   470    471    472    473   474   475   476   477   478   479   480   481    482    483    484   485    486    487   488   489    490    491    492    493   494   495    496    497   498   499
251  True  False  True  True  False  True  True  False  True  True  False  False  False  False  True  False  True  True  True  False  False  False  False  False  False  False  False  False  True  True  True  False  True  True  True  False  True  False  False  False  True  False  True  False  True  False  False  False  True  True  False  True  False  False  False  True  True  False  False  True  True  True  True  True  True  True  True  False  False  True  True  True  False  True  True  True  False  True  False  False  False  False  False  False  True  True  False  True  True  False  False  True  False  False  True  True  False  False  True  True  False  True  True  True  True  False  False  True  False  False  False  False  False  False  False  True  True  False  True  True  True  False  False  True  False  False  False  False  False  True  False  True  True  False  True  True  False  True  True  False  True  False  False  False  True  True  True  True  False  True  True  False  False  True  False  False  True  False  False  True  True  False  False  False  True  False  True  False  True  True  False  False  True  True  False  False  False  False  True  True  True  False  False  True  True  True  True  False  True  True  False  False  False  False  False  True  False  False  False  True  True  False  False  False  False  True  False  True  True  True  False  True  True  False  False  False  False  False  False  True  False  True  True  True  True  False  True  True  True  False  False  False  False  False  True  True  True  True  True  False  False  True  False  False  False  False  True  True  True  True  True  True  True  False  True  False  False  True  True  False  True  False  True  False  False  True  False  False  False  False  False  True  False  True  False  False  False  False  False  False  False  False  True  True  True  False  True  False  False  True  True  True  True  False  False  False  False  False  True  True  False  False  False  False  True  True  False  True  True  True  False  True  True  True  True  True  False  False  True  True  True  True  True  False  False  False  False  True  False  True  True  False  True  True  True  False  False  True  True  True  True  True  True  False  True  True  False  True  False  False  False  True  False  False  False  True  True  False  True  True  False  True  False  False  False  True  True  True  True  False  False  False  False  True  False  False  False  True  False  True  False  True  True  False  True  True  True  True  True  False  False  False  True  False  True  False  True  False  False  True  False  True  False  True  True  False  False  False  False  False  True  False  False  False  True  False  True  False  False  False  True  False  True  True  False  False  True  True  True  True  False  False  True  True  False  False  False  True  True  False  True  False  False  False  False  False  True  True  False  False  False  False  False  True  True  False  True  True  True  True  False  False  False  False  True  False  False  True  False  True  True  False  False  False  True  True  True  True  True  True  True  True  False  False  False  True  False  False  True  True  False  False  False  False  True  True  False  False  True  True