鱼C论坛

 找回密码
 立即注册
查看: 2397|回复: 1

函数问题

[复制链接]
发表于 2022-12-17 21:48:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
请 根据模板要求,使用 python、numpy、scipy 和 pandas 完成下列函数的定义,给出函
数的说明和注释,并在 if __name__=”__main__”:下给出实例进行测试和验证,给出函数测试
的曲线图。
要求,根据数学定义和公式计算,定义下列函数的参数、代码实现和返回值。


WMA Weighted Moving Average
函数参考格式:WMA(close, timeperiod=30)
模板如下,但是我只写了一点点,实在写不下去了,代码有点乱。

求求有人帮我一下,实在没看懂,跟我提供一下思路也行
########################################################################
#
# Functions for calculating Total Return, Annualized Returns, etc.
#
########################################################################
#
# This file is part of Big data Analysis with Python Course:
#
# Published under the UIBE License. See the file LICENSE for details.
#
# Copyright 2022 by San Zhang
########################################################################

import numpy as np
import pandas as pd
from numba import jit
import data
from data_keys import *

########################################################################
# Constants.

# Average number of days in a year. Normal years have 365 days,
# but every 4th year is a leap-year with 366 days.
DAYS_PER_YEAR = 365.25

# Average number of business- or trading-days in a year.
BDAYS_PER_YEAR = 251.67

########################################################################
# Public functions.
#函数介绍:WMA函数:以不同权重对股价赋予一定的权重再相加
#用dataframe创建过去35天的股票价格记录

data={"日期":pd.Series(range(1,35))}
share_data = pd.DataFrame(data)
def WMA(close, timeperiod=30):
    #设置权重
    weights = np.array(range(1, n + 1))

    sum_weights = np.sum(weights)
    weight1 = weights/sum_weights
    # 公式计算:以日期从新到旧权重依次下降,且所选数据权重和为1,分别乘上当期收盘股票价格来预测下期股票价格


    Total_Return[t] = Total_Return[t - 1] * (Dividend[t] + Share_Price[t]) / Share_Price[t - 1]
    res = close.rolling(window=n).apply(lambda x: np.sum(weights * x) / sum_weights, raw=False)
    return res



def total_return(df):
    """
    Calculate the "Total Return" of a stock when dividends are
    reinvested in the stock.

    The formula is:

    Total_Return[t] = Total_Return[t-1] * (Dividend[t] + Share_Price[t]) / Share_Price[t-1]

    :param df:
        Pandas data-frame assumed to contain SHARE_PRICE and DIVIDEND.
    :return:
        Pandas series with the Total Return.
    """

    # Copy the relevant data so we don't change it.
    df2 = df[[SHARE_PRICE, DIVIDEND]].copy()

    # Fill NA-values in the Dividend-column with zeros.
    df2[DIVIDEND].fillna(0, inplace=True)

    # Calculate the daily Total Return.
    tot_ret_daily = (df2[DIVIDEND] + df2[SHARE_PRICE]) / df2[SHARE_PRICE].shift(1)

    # Calculate the cumulative Total Return.
    tot_ret = tot_ret_daily.cumprod()

    # Replace the first row's NA with 1.0
    tot_ret.iloc[0] = 1.0

    return tot_ret


def annualized_returns(series, years):
    """
    Calculate the annualized returns for all possible
    periods of the given number of years.

    For example, given the Total Return of a stock we want
    to know the annualized returns of all holding-periods
    of 10 years.

    :param series:
        Pandas series e.g. with the Total Return of a stock.
        Assumed to be daily data.
    :param years:
        Number of years in each period.
    :return:
        Pandas series of same length as the input series. Each
        day has the annualized return of the period starting
        that day and for the given number of years. The end of
        the series has NA for the given number of years.
    """

    # Number of days to shift data. All years have 365 days
    # except leap-years which have 366 and occur every 4th year.
    # So on average a year has 365.25 days.
    days = int(years * 365.25)

    # Calculate annualized returns for all periods of this length.
    # Note: It is important we have daily (interpolated) data,
    # otherwise the series.shift(365) would shift much more than
    # a year, if the data only contains e.g. 250 days per year.
    ann_return = (series.shift(-days) / series) ** (1 / years) - 1.0

    return ann_return


def prepare_ann_returns(df, years, key=PSALES, subtract=None):
    """
    Prepare annualized returns e.g. for making a scatter-plot.
    The x-axis is given by the key (e.g. PSALES) and the y-axis
    would be the annualized returns.

    :param df:
        Pandas DataFrame with columns named key and TOTAL_RETURN.
    :param years:
        Number of years for annualized returns.
    :param key:
        Name of the data-column for x-axis e.g. PSALES or PBOOK.
    :param subtract:
        Pandas Series to be subtracted from ann-returns
        to adjust for e.g. growth in sales-per-share.
    :return:
        (x, y) Pandas Series with key and adjusted ANN_RETURN.
    """

    # Create a new data-frame so we don't modify the original.
    # We basically just use this to sync the data we are
    # interested in for the common dates and avoid NA-data.
    df2 = pd.DataFrame()

    # Copy the key-data e.g. PSALES.
    df2[key] = df[key]

    # Calculate all annualized returns for all periods of
    # the given number of years using the Total Return.
    ann_return = annualized_returns(series=df[TOTAL_RETURN], years=years)

    if subtract is None:
        # Add the ann-returns to the new data-frame.
        df2[ANN_RETURN] = ann_return
    else:
        # Calculate all annualized returns for the series
        # that must be subtracted e.g. sales-per-share.
        ann_return_subtract = annualized_returns(series=subtract, years=years)

        # Subtract the ann. returns for the total return
        # and the adjustment (e.g. sales-per-share).
        # Then add the result to the new data-frame.
        df2[ANN_RETURN] = ann_return - ann_return_subtract

    # Drop all rows with NA.
    df2.dropna(axis=0, how='any', inplace=True)

    # Retrieve the relevant data.
    x = df2[key]
    y = df2[ANN_RETURN]

    return x, y


if __name__ == "__main__":
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-16 17:05:37 | 显示全部楼层
1 if name=main这里没有调用主函数
2 python变量名支持中文,就算你用英文变量名也就算了,注释为什么要英文?
3 对程序、函数描述的注释用3双引号,也不应该用#啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-16 12:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表