我可以在 Power Query M 中添加带有线性插值的新列吗?

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

我正在努力从期货市场价格中提取利率曲线,并在电力查询中创建一个包含以下列的表(表 1):

- BusinessDays: 代表从今天到每个未来合约到期的 nr o 个工作日

- 利率: 代表从今天到期货合约到期的利率

Table 1

第二个表(表2)是不同工作日到期的内部理财产品ID。

- InstrumentID: 金融机构销售的金融产品的唯一内部ID

- BusinessDays: 代表从今天到各金融产品到期的nr o 个工作日

Table 2

我在使用M语言时遇到了一些问题,不幸的是这个特定的计算必须在Excel中执行,所以我仅限于Power Query M。

我无法执行的具体步骤是:

  • 在 power query 中创建一个函数,在表 2 中添加一个新列,其中包含每个金融产品的插值利率。

我正在寻找的最终结果看起来像这样

End result

powerquery linear-interpolation
2个回答
0
投票

有多种方法可以解决此问题,但无论如何,您需要进行某种查找来确定与您的

BusinessDays
值匹配的括号,以便您可以计算插值。

我认为生成一个包含所有天数与利率的列表,然后执行

Join
来提取匹配项会更简单。

Name第一个查询

intRates
并扩展了利率表:

let

//Get the interest rate/business day table
    Source = Excel.CurrentWorkbook(){[Name="intRates"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"BusinessDays", Int64.Type}, {"InterestRate", Percentage.Type}}),

    //Add two columns which are the interest rate and business day columns offset by one
    //It is faster to subtract this way than by adding an Index column
    offset= 
        Table.FromColumns(
            Table.ToColumns(#"Changed Type") 
                & {List.RemoveFirstN(#"Changed Type"[BusinessDays]) & {null}}
                & {(List.RemoveFirstN(#"Changed Type"[InterestRate])) & {null}},
            type table[BusinessDays=Int64.Type, InterestRate=Percentage.Type, shifted BusDays=Int64.Type, shifted IntRate=Percentage.Type]),

//Add a column with a list of the interest rates for each data interpolated between the segments
    #"Added Custom" = Table.AddColumn(offset, "IntList", each let
            sbd=[shifted BusDays], 
            intRateIncrement = ([shifted IntRate]-[InterestRate])/([shifted BusDays]-[BusinessDays]),
            Lists= List.Generate(
                ()=>[d=[BusinessDays],i=[InterestRate]],
                each [d]< sbd,
                each [d=[d]+1, i = [i]+intRateIncrement],
                each [i])
        in Lists),

//add another column with a list of days corresponding to the interest rates
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "dayList", each {[BusinessDays]..[shifted BusDays]-1}),

//remove the last row as it will have an error
    remErrRow = Table.RemoveLastN(#"Added Custom1",1),

//create the new table which has the rates for every duration
    intRateTable = Table.FromColumns(
                        {List.Combine(remErrRow[dayList]),List.Combine(remErrRow[IntList])},
                        type table[Days=Int64.Type, Interest=Percentage.Type])
in
    intRateTable

这会产生一个表格,其中包含每天(从 39 到 ,及其相应的利率。

然后读取“Instruments”表并使用

JoinKind.LeftOuter

将其与 intRates 连接起来
let
    Source = Excel.CurrentWorkbook(){[Name="Instruments"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"InstrumentID", type text}, {"BusinessDays", Int64.Type}}),

//add the rate column
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"BusinessDays"}, intRates, {"Days"}, "intRates", JoinKind.LeftOuter),
    #"Expanded intRates" = Table.ExpandTableColumn(#"Merged Queries", "intRates", {"Interest"}, {"Interest"})
in
    #"Expanded intRates"

enter image description here

表格中间部分的一些结果与您发布的结果不同,但似乎与两个值之间的线性插值公式一致,所以我不确定差异是如何产生的


0
投票

非常感谢您的回答。这对我来说效果很好,但我不知道如何在包含多条利率曲线的表格中利用它。这意味着,源表将有第三列指定每行的利率“curve_name”。例如,某些行的 curve_name = “SOFR”,有些行的 curve_name = “EURIBOR”。

在这种情况下,我们如何向插值代码添加另一层,以便它仅根据每个 curve_name 的初始集 [BusinessDays, InterestRate] 构造插值列?

谢谢你

© www.soinside.com 2019 - 2024. All rights reserved.