sbenthall / SHARKFin

Simulating Heterogeneous Agents with Finance
6 stars 5 forks source link

Market Stopped - invalid value encountered #242

Closed wjt5121 closed 1 year ago

wjt5121 commented 1 year ago

20.230.0.191 sparksharkrun13838 7432 25 4 60 ClientRPCMarket InferentialExpectations LUCAS0 3.0 0.99 1.000203 0.011983 0.005 5000 0.1 0.1 60.0 60.0 0.3 0 0 0 None ClosingPrice subjective_return: 0.9999130331113283 Q-0 waiting for response... response received:{'BuyTarget': 1, 'SellTarget': 1, 'ClosingPrice': 1, 'MarketState': 'Stopped: Hit market maker price range, shutting market down. ABC MarketMaker Best Ask and Best Bid is outside range 934 , -65 at 2017-01-02 10:37:42.312818\n', 'NextDividend': 1} The market stopped! Do something! Ending simulation Market stopped Ran for 1 days Ran for 1 days Ran for 1 days Problem computing stylized facts Found array with 0 sample(s) (shape=(0, 1)) while a minimum of 1 is required by LinearRegression. Ending the run_any_simulation.py script

basic_cancel - consumer not found: ctag1.bfd4b92c656d40cb9ed0fb7c97ad7fe0 /shared/home/ammpssharkfin/.local/lib/python3.9/site-packages/scipy/stats/_stats_mstats_common.py:196: RuntimeWarning: invalid value encountered in sqrt t = r np.sqrt(df / ((1.0 - r + TINY)(1.0 + r + TINY))) /usr/local/lib64/python3.9/site-packages/pandas/core/internals/blocks.py:2538: RuntimeWarning: invalid value encountered in cast values = values.astype(str)

sbenthall commented 1 year ago

@mesalas this looks like an AMMPS error

wjt5121 commented 1 year ago

This is from the ammps log.

started with the following arguments: RunConfFromFile /usr/simulation/test_conf3838.xlsx /shared/home/ammpssharkfin/output/sparksharkrun13838out --number 3838 --compression true --rabbitMQ-host 20.230.0.191 --rabbitMQ-queue sparksharkrun13838 --prefix lshark Connecting to external SHARKFin market on 20.230.0.191 using queue sparksharkrun13838 starting simulation 3838. configuration /usr/simulation/test_conf3838.xlsx, start date: 1/1/2017 9:30:00 AM end date: 12/1/2017 4:30:00 PM Connected and awaiting RPC requests 5:58:04 PM 0 got data: {"bl": 0, "sl": 0, "dividend": 0.008671740465735442, "end_simulation": false, "market_broker_arg": "0"} Hit market maker price range, shutting market down. ABC MarketMaker Best Ask and Best Bid is outside range 934 , -65 at 2017-01-02 10:37:42.312818

Hit market maker price range, shutting market down. ABC MarketMaker Best Ask and Best Bid is outside range 934 , -65 at 2017-01-02 10:37:42.312818

[ammpssharkfin@finsim-scheduler ammps]$

mesalas commented 1 year ago

Yes its the market Maker stopping because its posting a negative bid. It's also posting a ask at about 900$. So a bid ask spread of 1000$ indicating a very high volatility.

Is it a single occurrence or an error happening across the board?

@wjt5121 Could you attach the config we are passing to ammps?

wjt5121 commented 1 year ago

@mesalas Here is the command I passed to the ammps_config_generator but the .xlsx files are no longer on the system.

'ammps_config_cmd': '/usr/bin/python3 /usr/simulation/ammps_config_generator/acg/simulations/make_lucas_shark_config.py --seed 7432 --name test_conf3838.xlsx --days 240 --mm_lucas_factor 0.05 --inst_val_std 0.1 --out-dir /usr/simulation/ ',
 'ammps_config_gen': '{"parameters": "{\\"seed\\": \\"7432\\", \\"name\\": \\"test_conf3838.xlsx\\", \\"days\\": 240, \\"mm_lucas_factor\\": 0.05, \\"inst_val_std\\": 0.1, \\"out-dir\\": \\"/usr/simulation/\\"}"}

I'm also attaching the simulation grid with the results. We had total number of simulation is 25920 and 958 simulations completed exiting with status 0 and simulating all 240 days successfully about 3%. 24703 simulations exited with status code 1 but did complete a total of 401874 days. 182 simulations exited with status code 3. Simulations existing with code 3 completed 6515.0 days The total number of simulated days is 638309.0 which is 10% percent of the total.

sparksharkrun1parametergridRESULTS.csv.zip

mesalas commented 1 year ago

Would you be able to send me the ammps logs for run 3838?

Im not at my computer right now, so I can't check but if we are changing the growth rate and std we need to pass those parameters to the traders in ammps that use them.

wjt5121 commented 1 year ago

Here is a breakdown of the success rates base on input parameter.

202800.0 days simulated when dphm is 1000 or 3.26% 20160.0 days simulated when dphm is 5000 or 0.32% 6960.0 days simulated when dphm is 10000 or 0.11% 62400.0 days simulated when zeta is 0.0 or 1.00% 56640.0 days simulated when zeta is 0.3 or 0.91% 54960.0 days simulated when zeta is 0.7 or 0.88% 55920.0 days simulated when zeta is 1.0 or 0.90% 211680.0 days simulated when mmlucasfactor is 0.0 or 3.40% 18240.0 days simulated when mmlucasfactor is 0.05 or 0.29% 0.0 days simulated when mmlucasfactor is 1.0 or 0.00% 0.0 days simulated when mmlucasfactor is 0.15 or 0.00% 105840.0 days simulated when attention is 0.005 or 1.70% 84000.0 days simulated when attention is 0.015 or 1.35% 40080.0 days simulated when attention is 0.05 or 0.64% 60960.0 days simulated when inst_val_std is 0.1 or 0.98% 73920.0 days simulated when inst_val_std is 0.15 or 1.19% 95040.0 days simulated when inst_val_std is 0.2 or 1.53% 132001.0 days simulated when pop_aNrmInitMean is 5 or 2.12% 132001.0 days simulated when pop_aNrmInitMean is 6 or 2.12% 132001.0 days simulated when pop_aNrmInitMean is 7 or 2.12%

mesalas commented 1 year ago

Hmm... that's not good. It's a very low success rate no matter the inputs. I'll see what happens when I run it through the debugger.

wjt5121 commented 1 year ago

Here is the outputs for run 3838. sparksharkrun13838out.tar.gz

mesalas commented 1 year ago

Ok The problem is that the agents that are using lucas asset pricing are not calculating the correct prices. sometime even negative prices.

with a dividend of 0.0086717404657354422

and discountFactor=0.98999999999999999 crra = 5 dividendGrowthRate = 1.000203 dividendStd = 0.011983000000000001 daysPerQuarter = 90

im getting a price of -16.943300368819926

where price = dividend * PriceToDividendRatio(discountFactor, crra, dividendGrowthRate, dividendStd, daysPerQuarter)

and

PriceToDividendRatio(double discountFactor, double crra, double dividendGrowthRate, double dividendStd, double daysPerQuarter)
    {
        // Pricing according to https://github.com/sbenthall/SHARKFin/issues/197#event-9057079456
        // Convert discount factor to daily
        var dailyDiscountFactor = Math.Pow(discountFactor, 1.0 / daysPerQuarter);

        var dividendShockStd = dividendStd / Math.Sqrt(dividendGrowthRate);

        var subjectiveReturn = Math.Pow(dividendGrowthRate, (1.0 - crra))
                                * dailyDiscountFactor * Math.Pow(Math.Pow(dividendShockStd, 2.0) + 1.0, crra * (crra - 1.0)/2.0);

        return subjectiveReturn / (1 - subjectiveReturn);
    }

which should be similar to https://github.com/sbenthall/SHARKFin/blob/e7484f76f893b2cf9fa0c662aabc8a938e7394da/sharkfin/utilities.py#L121

this means that subjectiveReturn > 1 @sbenthall can you help me double check the formula?

mesalas commented 1 year ago

I now understand what's happening.

we have changed the defaults of dividendGrowthRate, dividendStd and crra.

Setting them to 1.000203 0.011983 and 3 Gives correct prices (very close to 100 for the initial price as we would expect)

mesalas commented 1 year ago

I have updated the ammps config generator to use these values. I need to double check and confirm that things are ok but it's late here. ill keep you posted

mesalas commented 1 year ago

Using the latest ammps_config_generator, ammps and sharkfin are happily running together. @wjt5121 and @sbenthall do we want to rerun the big grid, or just do a small test to verify and then redo the whole thing?

sbenthall commented 1 year ago

Thank you @mesalas

I think: test on tiny grid, then redo with big grid, is a good plan!

On Thu, Jun 22, 2023, 5:32 AM Nicholas @.***> wrote:

Using the latest ammps_config_generator, ammps and sharkfin are happily running together. @wjt5121 https://github.com/wjt5121 and @sbenthall https://github.com/sbenthall do we want to rerun the big grid, or just do a small test to verify and then redo the whole thing?

— Reply to this email directly, view it on GitHub https://github.com/sbenthall/SHARKFin/issues/242#issuecomment-1602317509, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAQZECW2HZESM3EC74XWG3XMQGJNANCNFSM6AAAAAAZNUWOVU . You are receiving this because you were mentioned.Message ID: @.***>

wjt5121 commented 1 year ago

Ok great! I'm spinning up the cluster now and I will run a single seed on the grid. sparksharkrun2parametergrid.csv

wjt5121 commented 1 year ago

Similar results. Only about 3% of the days simulated. I'm attaching the results csv and the logs for simid 383. sparksharkrun2383out.tar.gz shark_sparksharkrun2383_out.log ammps_sparksharkrun2383_out.log ammps_confsparksharkrun2383_out.log sparksharkrun2parametergridRESULTS.csv

2:06:40 PM 0 got data: {"bl": 0, "sl": 0, "dividend": 0.008754777224170868, "end_simulation": false, "market_broker_arg": "0"} Hit market maker price range, shutting market down. ABC MarketMaker Best Ask and Best Bid is outside range 2099 , -184 at 2017-01-02 09:55:58.522598

Hit market maker price range, shutting market down. ABC MarketMaker Best Ask and Best Bid is outside range 2099 , -184 at 2017-01-02 09:55:58.522598

19184 days simulated when dphm is 1000 or 6.17% 10000 days simulated when dphm is 5000 or 3.22% 8440 days simulated when dphm is 10000 or 2.71% 9882 days simulated when zeta is 0.0 or 3.18% 9178 days simulated when zeta is 0.7 or 2.95% 9344 days simulated when zeta is 1.0 or 3.00% 9220 days simulated when zeta is 0.3 or 2.96% 36245 days simulated when mmlucasfactor is 0.0 or 11.65% 1379 days simulated when mmlucasfactor is 0.05 or 0.44% 0 days simulated when mmlucasfactor is 1.0 or 0.00% 0 days simulated when mmlucasfactor is 0.15 or 0.00% 16868 days simulated when attention is 0.005 or 5.42% 8928 days simulated when attention is 0.05 or 2.87% 11828 days simulated when attention is 0.015 or 3.80% 11338 days simulated when inst_val_std is 0.1 or 3.65% 12921 days simulated when inst_val_std is 0.15 or 4.15% 13365 days simulated when inst_val_std is 0.2 or 4.30% 8340 days simulated when pop_aNrmInitMean is 5 or 2.68% 8340 days simulated when pop_aNrmInitMean is 6 or 2.68% 8340 days simulated when pop_aNrmInitMean is 7 or 2.68%

mesalas commented 1 year ago

@wjt5121 383 is running fine on my end. We need to double check that you are pulling the latest version of ammps_config_generator (https://github.com/mesalas/ammps_config_generator)

If you have any way of saving the ammps config (the excel file generated by ammps_config_generator that we parse) this would help me to figure out what's going on.

wjt5121 commented 1 year ago

@mesalas I ssh'ed into a host while it was running a simulation and was able to capture the xlsx config file which is attached. I also check the version of the config_gen repo and I think it's correct.

ammpssharkfin@finsim-htc-1 ammps_config_generator]$ sudo git log -1
commit 78aa52682abf46210e5c8f9144019a00b5ddd8ab (HEAD -> master, origin/master, origin/HEAD)
Author: Nicholas <nskg@pm.me>
Date:   Wed Jun 21 23:43:34 2023 +0200

    updated default values
[ammpssharkfin@finsim-htc-1 ammps_config_generator]$ sudo git branch
* master

test_conf383.xlsx

mesalas commented 1 year ago

Thanks for the config file. The values in the excel file are the old ones which is causing the error as the agents in ammps price the asset incorrectly.

From the git output the version of config repo seems correct though. It may make sense to try and ssh into the host and see what's in acg/baseconfiguration/base_agent_configuration.py and acg/simulations/make_lucas_shark_config.py

wjt5121 commented 1 year ago

Uninstalling the acg package resolved the issue. Completed run2a with 1296 grid pointswith 88% success rate. Attached results csv. sparksharkrun2aparametergridRESULTS.csv