问题描述
我正在尝试使用 RQuantLib 包来计算使用 EuropeanImpliedVolatility 函数的数据帧中期权的隐含波动率。该函数返回一个基类并得到错误 library(tidyverse)
library(RQuantLib)
book <- data.frame(
type=c("call","call","put","put"),strike=c(27800,27600,27400,24800,24600,24400),last=c(240,280,320,310,270,230),underlying=rep(26425,6),dividendYield=rep(0,riskFreeRate=rep(0.015,maturity=rep(39/365,6)
) %>%
rowwise() %>%
mutate(
impvol= EuropeanoptionImpliedVolatility(type,value=last,underlying,strike,dividendYield,riskFreeRate,maturity,volatility=0.4)
)
。
@client.command()
async def play(ctx,url : str):
song_there = os.path.isfile("song.mp3")
try:
if song_there:
os.remove("song.mp3")
except PermissionError:
await ctx.send("Wait for the current playing music to end or use the 'stop' command")
return
voiceChannel = ctx.message.author.voice.channel
await voiceChannel.connect()
voice = discord.utils.get(client.voice_clients,guild=ctx.guild)
SAVE_PATH = "C:\\Users\\Myname\\Desktop\\Projet_Python\\discord_Bot\\"
ydl_opts = {
'format': 'bestaudio/best','postprocessors': [{
'key': 'FFmpegExtractAudio','preferredcodec': 'mp3','preferredquality': '192',}],'outtmpl': SAVE_PATH + '/%(title)s.%(ext)s',}
with youtube_dl.YoutubedL(ydl_opts) as ydl:
ydl.download([url])
for file in os.listdir("./"):
if file.endswith(".m4a"):
os.rename(file,"song.mp3")
try:
voice.play(discord.FFmpegPCMAudio((SAVE_PATH + "song.mp3")))
except Exception as e:
print(e)
解决方法
有一些属性会导致问题。我们可以用 as.numeric
library(dplyr)
library(RQuantLib)
book %>%
rowwise %>%
mutate(impvol = as.numeric(EuropeanOptionImpliedVolatility(type,value=last,underlying,strike,dividendYield,riskFreeRate,maturity,volatility = 0.4)))
-输出
# A tibble: 6 x 8
# Rowwise:
# type strike last underlying dividendYield riskFreeRate maturity impvol
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 call 27800 240 26425 0 0.015 0.107 0.204
#2 call 27600 280 26425 0 0.015 0.107 0.202
#3 call 27400 320 26425 0 0.015 0.107 0.197
#4 put 24800 310 26425 0 0.015 0.107 0.275
#5 put 24600 270 26425 0 0.015 0.107 0.277
#6 put 24400 230 26425 0 0.015 0.107 0.278
或包裹在 list
中,如果我们想保持这样的属性
book %>%
rowwise %>%
mutate(impvol = list(EuropeanOptionImpliedVolatility(type,volatility=0.4)))
-输出
# A tibble: 6 x 8
# Rowwise:
# type strike last underlying dividendYield riskFreeRate maturity impvol
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <list>
#1 call 27800 240 26425 0 0.015 0.107 <ErpnOpIV [1]>
#2 call 27600 280 26425 0 0.015 0.107 <ErpnOpIV [1]>
#3 call 27400 320 26425 0 0.015 0.107 <ErpnOpIV [1]>
#4 put 24800 310 26425 0 0.015 0.107 <ErpnOpIV [1]>
#5 put 24600 270 26425 0 0.015 0.107 <ErpnOpIV [1]>
#6 put 24400 230 26425 0 0.015 0.107 <ErpnOpIV [1]>
或者带有 pmap
library(purrr)
book %>%
mutate(impvol = pmap_dbl(.,~ EuropeanOptionImpliedVolatility(..1,value = ..3,..4,..2,..5,..6,..7,volatility = 0.4)))
# type strike last underlying dividendYield riskFreeRate maturity impvol
#1 call 27800 240 26425 0 0.015 0.1068493 0.2044270
#2 call 27600 280 26425 0 0.015 0.1068493 0.2019167
#3 call 27400 320 26425 0 0.015 0.1068493 0.1973822
#4 put 24800 310 26425 0 0.015 0.1068493 0.2745614
#5 put 24600 270 26425 0 0.015 0.1068493 0.2773029
#6 put 24400 230 26425 0 0.015 0.1068493 0.2780447
数据
book <- data.frame(
type=c("call","call","put","put"),strike=c(27800,27600,27400,24800,24600,24400),last=c(240,280,320,310,270,230),underlying=rep(26425,6),dividendYield=rep(0,riskFreeRate=rep(0.015,maturity=rep(39/365,6)
)