ASSIGNMENT-12
43.
1.Historical data analysis:
historical_data = {
"-$5.00": 1,
"-$1.00": 20,
"-$0.50": 55,
"-$0.38": 95,
"$0.00": 140,
"$0.38": 100,
"-$0.50": 73,
"$1.00": 10,
"$2.00": 6,
}
Based on above data, we create a dictionary called historical data that
shows how often daily stock price changes happen. The keys show the
possible price changes, and the numbers show how often those changes
happen.
2.Model the historical data
price_changes, frequencies = zip(*historical_data.items())
frequencies = np.array(frequencies)
The new historical data dictionary is used to get the price changes and
rates, which are then turned into a NumPy array.
3.Generate random price changes
def generate_random_price_change():
return np.random.choice(price_changes,
p=frequencies /frequencies. Sum())
The method generate_random_price_change has been changed to use the
new, correct past data. It makes a chance change in the price based on the
most recent past spread.
4. Update stock prices
def simulate_stock_prices(initial_price, days):
prices = [initial_price]
for _ in range(days):
price_change = generate_random_price_change()
current_price = prices[-1] + float(price_change[1:])
prices.append(current_price)
return prices
The method that simulates stock prices over a certain number of trade
days using the new random price changes has not changed.
5. Repeat the multiple trials
num_trials = 100
final_prices = []
for _ in range(num_trials):
simulated_prices = simulate_stock_prices(9.75, 90)
final_prices.append(simulated_prices[-1])
The exercise is run 100 times by the code, which records the end stock
prices and makes a new set of random price changes for each run.
6. Collect results
final_prices = np.array(final_prices)
The list of final stock prices is turned into a NumPy collection so that it
can be studied further.
7.Analyze results
mean_price = np.mean(final_prices)
median_price = np.median(final_prices)
std_deviation = np.std(final_prices)
NumPy methods are used in the code to find the mean, median, and
standard deviation of the end stock prices.
8. Interpret results
print(f"Mean Final Stock Price: ${mean_price:.2f}")
print(f"Median Final Stock Price: ${median_price:.2f}")
print(f"Standard Deviation of Final Stock Price: ${std_deviation:.2f}")
By this the results are printed and provide the potential range of ending
stock prices .
18.
Bring in Libraries:
Numpy is what we use to work with numbers and make random numbers.
We can make maps with matplotlib.pyplot.
Given the following:
To model, you need to set the stock price at the start, the mean daily
change, the daily variance, and the number of days.
Loop for simulations:
Make random daily returns from a normal distribution where the mean and
standard deviation are given.
Add up all of the daily returns to get the total returns over time.
Find the prices of stocks:
To find the virtual stock prices for the next 30 days, use the total results.
Making a plan:
Make a line plot to see how the stock price changed in the simulation.
For reference, the first price of the stock is shown by a thin red line.
Show the Story:
Use plt.show() to show the plot.
If you have numpy and matplotlib loaded, you can run this Python code to
see how the stock price changes in a simulation over the next 30 days.
Change the settings as needed for your situation.
Python code for above data:
import numpy as np
import matplotlib.pyplot as plt
# Given parameters
current_price = 53
mean_daily_change = 0.003227
daily_volatility = 0.026154
num_days = 30
# Simulation loop
daily_returns = np.random.normal(loc=mean_daily_change,
scale=daily_volatility, size=num_days)
cumulative_returns = np.cumsum(daily_returns)
# Calculate stock prices
stock_prices = current_price * np.exp(cumulative_returns)
# Plot the stock price movement
days = np.arange(1, num_days + 1)
plt.plot(days, stock_prices, label='Simulated Stock Price', marker='o')
plt.axhline(y=current_price, color='r', linestyle='--', label='Initial Stock
Price')
# Add labels and title
plt.xlabel('Days')
plt.ylabel('Stock Price')
plt.title('Simulation of Stock Price Over the Next 30 Days')
plt.legend()
# Show the plot
plt.show()