我正在尝试使用指数样条拟合函数通过 QuantLib 构建美国财政部曲线样条。但是,我得到了非常奇怪的结果,无法弄清楚原因。即,生成的贴现曲线的负利率,其中输入都是正收益率,前端收益率存在巨大差异(下图)。
我正在为整个债券领域提供函数 > 1Y,这会导致一些重复的到期日(对于在不同时间发行并沿曲线向下滚动的债券),所以我不确定这是否会导致一些拟合问题。我正在使用的整个数据框(在代码中命名为 df)显示在底部。
这是我的代码来拟合曲线和下面的相应数据。(所述“YrsToMat”列由产生YrsToMat<- sapply(maturities, yearFraction, startDates = Sys.Date(), dayCounters = 9) #9 = ActualActual.Bond
,其中maturities
是所有的键期限的列表)。
谢谢你的帮助!
library(RQuantLib)
YrsToMat <- df$YrsToMat
marketQuotes <- df$Prices
cpn_rates <- df$Coupons
dateparams <- list(settlementDays=1, period="Semiannual",
dayCounter="ActualActual.Bond",
businessDayConvention ="ModifiedFollowing")
curveparams <- list(method="ExponentialSplinesFitting",
origDate = Sys.Date())
curve <- FittedBondCurve(curveparams, YrsToMat, cpn_rates, marketQuotes, dateparams)
Run Code Online (Sandbox Code Playgroud)
structure(list(ISIN = structure(1:261, .Label = c("US912810EM63",
"US912810EN47", "US912810EP94", "US912810EQ77", "US912810ES34",
"US912810ET17", "US912810EV62", "US912810EW46", "US912810EX29",
"US912810EY02", "US912810EZ76", "US912810FA17", "US912810FB99",
"US912810FE39", "US912810FF04", "US912810FG86", "US912810FJ26",
"US912810FM54", "US912810FP85", "US912810FT08", "US912810PT97", …
Run Code Online (Sandbox Code Playgroud) I'm trying to just highlight a single point on a Seaborn jointplot, but I'm coming up a little short. I'm able to plot the point from matplotlib - it's just not showing up in the same chart. What am I doing wrong? Thanks!
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd
#make some sample data
test_x = [i for i in range(10)]
test_y = [j for j in range(10)]
df = pd.DataFrame({'xs':test_x,
'ys':test_y})
#make …
Run Code Online (Sandbox Code Playgroud) int fun(int n) {
int count = 0;
for (int i = n; i > 0; i /= 2)
for (int j = 0; j < i; j++)
count += 1;
return count;
}
Run Code Online (Sandbox Code Playgroud)
我是时间复杂度计算的新手.对于这个算法,我得到的答案是O(nlogn),但答案显然是O(n).
我的逻辑是外部循环具有指数下降并且将发生log_base2_(N)次.内环将成为几何和总共运行N次(第一次迭代是N/2次,然后是N/4,然后是N/8 ......).如果我将这些放在一起并将它们作为嵌套循环的结果加倍,那就是我想出的O(NlogN).我错过了一些明显的东西吗
我能够为国库券市场建立折价曲线。但是,我希望以此来查找单个债券(以及最终债券组合)的关键利率风险。
我要寻找的主要利率风险是,如果我有一个30年期债券,而我们将用于折价该债券的1年期利率转移了,而将其他利率保持不变,那么债券价格会发生多少变化?对期限(例如2Y,5Y,7Y等)重复此操作,并对结果求和,可以使您了解债券的总期限,但可以更好地了解风险敞口如何分解。
http://www.investinganswers.com/financial-dictionary/bonds/key-rate-duration-6725
是否有人知道任何演示该操作方法的文档?谢谢。
我是Python的新手,遇到了一个问题,我觉得应该有一个相对直截了当的答案,而我还不太清楚。
我需要通过这种方式进行作业,因为我们的老师希望我们的GitHub文件夹在一个文件夹中包含所有代码,而在另一个文件夹中包含所有图像。
由于运行Python文件时我将位于Code文件夹中,因此有没有办法我可以引用Image文件夹?在本地,我知道我可以放置完整路径,但是由于这是通过GitHub的命令行完成的,因此我不确定完整路径甚至是引用该文件夹的方式。
+ Main GitHub Directory
+ Code Folder
-My Python File
+ Image Folder
-Images I want to use
Run Code Online (Sandbox Code Playgroud)
这只是我想做的一个示例程序:
from PIL import Image
image = Image.open('WeatherIcons/Code32.PNG')
image.show()
Run Code Online (Sandbox Code Playgroud)
如果“ WeatherIcons”是的子文件夹,那么它将起作用Code Folder
,但是在同一级别时,则不会。有什么解决方案可以解决这个问题?
谢谢!
有一个有趣的问题,其中有一个字体格式如下:
test = {'A': [(1,2),(3,4)],
'B': [(1,2),(5,6),(7,8)]}
Run Code Online (Sandbox Code Playgroud)
对于字典中的每个键,它们共享一些值(首先总是),但每个列表中有不同数量的元组.
最终,我的目标是以这种格式表示数据:
1 3 5 7
A 2 4 - -
B 2 - 6 8
Run Code Online (Sandbox Code Playgroud)
有没有一种智能的方法将字典值中的这些常见元素转换为数据框的列?
我想让列表长度相等的是以下内容:
#get all the unique first elements of the dictionary
unique=[]
for i in test.values():
for j in i:
unique.append(j[0])
unique = set(unique)
values_of_A = test['A']
#I thought this would loop through each tuple in the list and check if its
#first value is in the list of unique elements; otherwise, it will print
#0. However, …
Run Code Online (Sandbox Code Playgroud) 我是 Kivy 的新手,但正在努力掌握它的窍门。我正在以“在 Kivy 中创建应用程序”中的示例作为模板,但我什至在第一章中遇到了困难。由于某种原因,我的按钮被推到了应用程序的底部。我是否错过了他们出现在下面的一些关键内容?
我的Python文件:
import kivy
from kivy.app import App
from kivy.uix.button import Label
from kivy.uix.boxlayout import BoxLayout
# In the kv file, everything to the right of the colon is pure python
# for loading python module in kv file, use format of #: import keyword module_name
class WeatherWidget(BoxLayout):
def printing_test(self):
print('This is a test')
class DailyViewApp(App):
def build(self):
return WeatherWidget()
if __name__ == '__main__':
DailyViewApp().run()
Run Code Online (Sandbox Code Playgroud)
我的 kv 文件:
<WeatherWidget>:
orientation: "vertical"
BoxLayout:
height: "30dp"
size_hint_y: None …
Run Code Online (Sandbox Code Playgroud) 我确信这是很好理解的,但即使是我看到的例子我也无法理解如何使用特定类中定义的函数.
我做的简单示例如下(创建一个函数add_one
,将给定输入数字加1,然后在另一个函数中使用该函数add_two
):
class TestPassingFunctions:
def __init__(self, number):
self.number = number
def add_one(self, number):
return number + 1
def add_two(self, number):
new_value = self.add_one(number) + 1
return new_value
TestPassingFunctions.add_two(2)
Run Code Online (Sandbox Code Playgroud)
返回:
TypeError: add_two() missing 1 required positional argument: 'number'
Run Code Online (Sandbox Code Playgroud)
从我读过的内容来看,该类正在将其解释2
为self
参数.很明显,我不完全理解何时/如何使用初始化__init__
.到目前为止,我认为它应该用于通过类传播变量值以供不同函数使用,但是我的使用中显然存在一些缺陷.
谢谢你的帮助!
我试图在类中创建变量的实例,而不是使用全局变量,因为它似乎是最佳实践.如何在类中的其他函数中引用此变量?我原本以为这Test.running_sum
会起作用或者至少running_sum
在其中test_function
,但我也没有任何运气.非常感谢!
class Test:
def __init__(self):
self.root = None
running_sum = 0
def test_function(self):
print(Test.running_sum)
return
x = Test()
x.test_function()
Run Code Online (Sandbox Code Playgroud)
错误:
Traceback (most recent call last):
File "so.py", line 1, in <module>
class Test:
File "so.py", line 10, in Test
x = Test()
NameError: name 'Test' is not defined
Run Code Online (Sandbox Code Playgroud) python ×6
class ×2
quantlib ×2
algorithm ×1
dictionary ×1
kivy ×1
matplotlib ×1
pandas ×1
python-3.x ×1
r ×1
seaborn ×1