I am trying to change my Y-Axis on a histogram I have created for a Monte Carlo Simulation. The Y-Axis is currently outputting number of occurrences opposed to percents. I have my code below and I am assuming there needs to be a function with "For Y in Range", but I am not sure the exact way to do it.code
Asked
Active
Viewed 72 times
0
-
Please consider providing more information in your question, including the actual code you're using to calculate the hist, instead of just a link. This [SO link](https://stackoverflow.com/questions/17874063/is-there-a-parameter-in-matplotlib-pandas-to-have-the-y-axis-of-a-histogram-as-p) may be helpful – frederick-douglas-pearce Jan 12 '22 at 06:17
-
Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jan 20 '22 at 14:32
1 Answers
0
The main idea is to use weights to give each observation an equal weight or contribution to the bin of 0.001. Given a thousand trials you then obtain the relative frequency.
import pandas_datareader as pdr
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')
# I adjusted the stock to another index due to api limiations
prices = pdr.DataReader('sp500', 'fred', '2020-11-2', '2021-11-02'
)['sp500']
returns = prices.pct_change()
last_price = prices[-1]
num_simulations = 1000
simulation_df = pd.DataFrame()
for x in range(num_simulations):
count = 0
daily_vol = returns.std()
price_series = []
price = last_price * (1 + np.random.normal(0, daily_vol))
price_series.append(price)
simulation_df[x] = price_series
(fig, ax) = plt.subplots(facecolor='w')
plt.xlabel('Price', Color='k')
plt.ylabel('Percent', Color='k')
ax.tick_params(labelcolor='k')
ax.set_facecolor('w')
# create equal weights e. g. 0.001 for thousand prices
weights = weights = np.ones_like(simulation_df.loc[0]) / simulation_df.shape[1]
# assign weights to obtain relative frequencies
plt.hist(simulation_df, bins=50, ec='black', color='#0054A3', weights=weights)
Output:

KarelZe
- 1,466
- 1
- 11
- 21