Skip to content

Fundamentals

Plotting Share Prices

Your friend has written a simulation to predict the share prices of a company. Now, they would like your help to visualize the data.

Here is the simulation they have so far:

stock_prediction.py
from random import random
from datetime import datetime, timedelta

# Initial parameters
value_count = 365                 # How many days to predict
start_day = datetime(2020, 1, 1)  # Start day of the prediction
price = 50 * (1 + random())       # Initial share price

# Prediction data
share_prices = [
    price := price - 0.05 + random() / 10
    for _ in range(value_count)
]

# Dates assiciated with the data
days = [
    start_day + timedelta(days=current_value)
    for current_value in range(value_count)
]
  1. Copy the code into a new script price_prediction.py.
  2. Do a simple plot of the share_prices over the days.
  3. Add labels and a title to your plot
    1. Label the x-axis as Trade day
    2. Label the y-axis as Share Price in €
    3. Title the plot as Profit Inc. Share Prices (predicted)
  4. Find average value for the share_prices.
    1. Draw a dashed, light gray axhline(…) at the average share price
  5. Find the minimum and maximum share prices and the days on which they occur
    1. Draw a solid red vlines(…) at the minimum share price day from the average value to the minimum share price
    2. Draw a solid green vlines(…) at the maximum share price day from the average value to the maximum share price
    3. Fill in the area between the share price curve and the average line, between the minimum and maximum share price day. Consider that the minimum may occur either before or after the maximum. Use light gray as the fill color as well.
Example Output

How your plot might look

Coffee consumption

Oliver is responsible for our office coffee machine. He set up a list where everyone makes a mark when they drink a coffee so he can do the accounting later. Since he is pretty busy, he asked you to help with the accounting for this month.

Here is the data provided to you as a python dictionary:

# Coffee consumption - February 2024
coffee_consumption = {
    "Christian": 10,
    "David": 17,
    "Fredo": 22,
    "Julia": 2,
    "Lokamani": 18,
    "Nina": 3,
    "Norman": 1,
    "Oliver": 24,
    "Tobias": 14,
    "Uwe": 6
}
# Account balance end of 2024-01
balance = {
    "Christian": 9.25,
    "David": 3.00,
    "Fredo": -4.50,
    "Julia": 22.75,
    "Lokamani": 10.50,
    "Nina": -0.25,
    "Oliver": 50.00,
    "Tobias": 9.75,
    "Uwe": 4.50
}
  1. Create a new script coffe_consumption.py and copy the data into it.
  2. Turn the dictionary into a pandas-Series. Note, that when providing a dictionary as the data, the keys will automatically become the index.
    1. Assign the Series with the name Coffee 2024-02
  3. Find out who is drinking the most coffee and how much. Use the .max()- and .idxmax()-methods of the Series data type for this purpose.
  4. Oliver paid 24,98€ in this month to buy coffee beans. Calculate the minimal price per cup that is required to break even.
  5. To offset the cost and collect money for a new coffee machine, the current price is set to 0,35€ per cup. From your existing series, generate a new one containing the outstanding debt of each person. Remember that you can apply mathematical operation on the whole series at once.
    1. Give this new series the name Debt 2024-02
  6. The balance from the earlier month is given in the second tab above. Copy the data into your code as well.
    1. Turn the balance into a series as well.
    2. Subtract the debt from the balance. Note how some entries will result in NaN-values. This happens when the previous balance or the debt for a given person is not known and thus no meaningful calculation can be made.
    3. Add an initial balance of 0,00€ for the missing person on the balance sheet before calculating the final balance.
Example Output
Most coffee consumed by Oliver with 24 cups
Minimal cup price: 0.2135042735042735
New balance:
Christian     5.75
David        -2.95
Fredo       -12.20
Julia        22.05
Lokamani      4.20
Nina         -1.30
Norman       -0.35
Oliver       41.60
Tobias        4.85
Uwe           2.40
Name: Account balance 2024-02, dtype: float64