如何在 python 上求解求和方程

问题描述 投票:0回答:0

这是我第一次尝试在 Python 上求和方程。使用哪个 Python 库来求解以下方程式?示例代码是否可用于展示如何处理此类方程式?我不会写方程式,所以我上传了它的截图。

这是我的一段代码:

import numpy as np
from datetime import datetime

# Set the base date and strategy value
t_0 = datetime(2021, 1, 1)
I_0 = 100

# Define the component prices for each day
prices = {
    datetime(2020, 1, 31): [np.nan, np.nan, np.nan, np.nan],
    datetime(2020, 2, 3): [197.063, 253.2231, 652.3695, 652.3759],
    datetime(2020, 2, 4): [196.6896, 252.9168, 649.9793, 649.9858],
    datetime(2020, 2, 5): [197.3429, 252.8294, 653.5655, 653.5588],
    datetime(2020, 2, 6): [197.4554, 252.7901, 652.3171, 652.3172],
    datetime(2020, 2, 7): [196.7969, 252.944, 653.5571, 653.555],
    datetime(2020, 2, 10): [196.9404, 253.3696, 656.0398, 656.0355],
    datetime(2020, 2, 11): [196.9457, 253.3767, 653.6765, 653.6793],
    datetime(2020, 2, 12): [196.8736, 253.1046, 653.1065, 653.1106]
}

# Calculate the strategy value for each day
I_t = {}
for date, price_list in prices.items():
    if np.isnan(price_list).any():
        # Skip the first day, which has missing prices
        I_t[date] = np.nan
    else:
        # Calculate the strategy value for this day
        n = (date - max(prices.keys())).days
        N_t = np.array(price_list)
        N_t_minus_1 = np.array(prices[date - pd.Timedelta(days=1)])
        ratio = N_t / N_t_minus_1
        adj_ratio = ratio * (1 - 0.5/100 * n/365)
        I_t[date] = I_0 * np.prod(1 + adj_ratio)

# Print the results
for date, value in I_t.items():
    print(f"{date.strftime('%d-%b-%Y')}: {value:.4f}")
python pandas numpy scipy quantitative-finance
© www.soinside.com 2019 - 2024. All rights reserved.