F# 中的样本外预测
Out-of-sample forecasting in F#
我是 F# 的新手,想要 运行 样本外 OLS 预测。我有一个类似于下图的框架。我想使用前 3 个数据点估计参数,然后预测最后一个数据点。
let dfff=
[ "year" => series [ 1 => 1990.0; 2 => 1991.00; 3 => 1992.0; 4 => 1993.0]
"gold" => series [ 1 => 10.0; 2 => 10.00; 3 => 15.0; 4 => 20.0]
"silver" => series [ 1 => 20.0; 2 => 30.00; 3 => 45.0; 4 => 55.0] ]
|> frame
|> Frame.indexColsWith ["one"; "two"; "three"]
let dfff2 = dfff |> Frame.filterRows (fun k row -> row?one <= 1992.0 )
let mygold = [| for i in dfff2.GetColumn<float>("two").Values -> i |]
let mysilver = [| for i in dfff2.GetColumn<float>("three").Values -> i |]
let myparam =
namedParams [
"a", box mygold;
"b", box mysilver;
] |> R.data_frame
let myresult = R.lm(formula = "a~b", data = myparam)
R.summary(myresult)
let mycoefs = R.coef(R.summary(myresult))
let residuals = myresult.AsList().["residuals"].AsNumeric()
let dfff3 = dfff |> Frame.filterRows (fun k row -> row?one == 1993.0 )
有人可以帮我完成代码吗?我想检索 beta 和 alpha 系数并估计样本外 forecast.This 行将不起作用:
let beta=mycoefs.[1,1]
let predict=[| for i in dfff3 -> fun float i*beta |]
清理示例代码以提高可读性:
let dfff=
[ "year" => series [ 1 => 1990.0; 2 => 1991.00; 3 => 1992.0; 4 => 1993.0]
"gold" => series [ 1 => 10.0; 2 => 10.00; 3 => 15.0; 4 => 20.0]
"silver" => series [ 1 => 20.0; 2 => 30.00; 3 => 45.0; 4 => 55.0] ]
|> frame
let dfff2 = dfff |> Frame.filterRows (fun k row -> row?year <= 1992.0 )
let dfff3 = dfff |> Frame.filterRows (fun k row -> row?year = 1993.0 )
let myresult = R.lm(formula = "gold~silver", data = dfff2)
R.summary(myresult)
let mycoefs = R.coef(R.summary(myresult))
您可以通过几种方式预测最终观察结果:
R.predict_lm(myresult, dfff3)
/// val it : SymbolicExpression = 4
/// 16.57895
或者
let betas = mycoefs.AsNumericMatrix().[0..1,0]
betas.[0] + (dfff3.["silver"].Values |> Array.ofSeq).[0] * betas.[1]
/// val it : float = 16.57894737
你原来的问题提到了一个移动window。您可以更普遍地执行类似下面的操作,其中扩展了 window 用于样本外预测。我也使用年份作为索引,因为它是一个时间序列。
open RProvider
open RDotNet
open RProvider.``base``
open RProvider.stats
open System
open Deedle
let years = [|1990 .. 2010|]
let rand = System.Random()
let gold = [ for i in years do yield rand.NextDouble()]
let silver = [ for i in gold do yield 2.0 * i + rand.NextDouble()]
let x = Frame.ofColumns["gold" => Series(years, gold);
"silver" => Series(years, silver) ]
let listOfWindows =
[for year in years.[2..] do
yield x |> Frame.filterRows (fun key row -> key <= year)]
listOfWindows |> List.map (fun x ->
let yearToPredict = x.RowKeys |> Seq.max
let train =
x |> Frame.filterRows (fun year _ -> year < yearToPredict)
let test =
x |> Frame.filterRows (fun year _ -> year = yearToPredict)
let mdl = R.lm("silver~gold", train)
let predicted = R.predict_lm(mdl, test).AsNumeric().[0]
let actual = test.["silver"].[yearToPredict]
yearToPredict, predicted, actual
)
/// val it : (int * float * float) list =
/// [(1987, 2.229193258, 1.281468206); (1988, 1.111561003, 1.248677743);
/// (1989, 1.039488768, 0.7363931172); (1990, 1.680597973, 1.808993845);
/// (1991, 1.943243437, 2.092237694); (1992, 2.555973809, 2.00253973);...
///
我是 F# 的新手,想要 运行 样本外 OLS 预测。我有一个类似于下图的框架。我想使用前 3 个数据点估计参数,然后预测最后一个数据点。
let dfff=
[ "year" => series [ 1 => 1990.0; 2 => 1991.00; 3 => 1992.0; 4 => 1993.0]
"gold" => series [ 1 => 10.0; 2 => 10.00; 3 => 15.0; 4 => 20.0]
"silver" => series [ 1 => 20.0; 2 => 30.00; 3 => 45.0; 4 => 55.0] ]
|> frame
|> Frame.indexColsWith ["one"; "two"; "three"]
let dfff2 = dfff |> Frame.filterRows (fun k row -> row?one <= 1992.0 )
let mygold = [| for i in dfff2.GetColumn<float>("two").Values -> i |]
let mysilver = [| for i in dfff2.GetColumn<float>("three").Values -> i |]
let myparam =
namedParams [
"a", box mygold;
"b", box mysilver;
] |> R.data_frame
let myresult = R.lm(formula = "a~b", data = myparam)
R.summary(myresult)
let mycoefs = R.coef(R.summary(myresult))
let residuals = myresult.AsList().["residuals"].AsNumeric()
let dfff3 = dfff |> Frame.filterRows (fun k row -> row?one == 1993.0 )
有人可以帮我完成代码吗?我想检索 beta 和 alpha 系数并估计样本外 forecast.This 行将不起作用:
let beta=mycoefs.[1,1]
let predict=[| for i in dfff3 -> fun float i*beta |]
清理示例代码以提高可读性:
let dfff=
[ "year" => series [ 1 => 1990.0; 2 => 1991.00; 3 => 1992.0; 4 => 1993.0]
"gold" => series [ 1 => 10.0; 2 => 10.00; 3 => 15.0; 4 => 20.0]
"silver" => series [ 1 => 20.0; 2 => 30.00; 3 => 45.0; 4 => 55.0] ]
|> frame
let dfff2 = dfff |> Frame.filterRows (fun k row -> row?year <= 1992.0 )
let dfff3 = dfff |> Frame.filterRows (fun k row -> row?year = 1993.0 )
let myresult = R.lm(formula = "gold~silver", data = dfff2)
R.summary(myresult)
let mycoefs = R.coef(R.summary(myresult))
您可以通过几种方式预测最终观察结果:
R.predict_lm(myresult, dfff3)
/// val it : SymbolicExpression = 4
/// 16.57895
或者
let betas = mycoefs.AsNumericMatrix().[0..1,0]
betas.[0] + (dfff3.["silver"].Values |> Array.ofSeq).[0] * betas.[1]
/// val it : float = 16.57894737
你原来的问题提到了一个移动window。您可以更普遍地执行类似下面的操作,其中扩展了 window 用于样本外预测。我也使用年份作为索引,因为它是一个时间序列。
open RProvider
open RDotNet
open RProvider.``base``
open RProvider.stats
open System
open Deedle
let years = [|1990 .. 2010|]
let rand = System.Random()
let gold = [ for i in years do yield rand.NextDouble()]
let silver = [ for i in gold do yield 2.0 * i + rand.NextDouble()]
let x = Frame.ofColumns["gold" => Series(years, gold);
"silver" => Series(years, silver) ]
let listOfWindows =
[for year in years.[2..] do
yield x |> Frame.filterRows (fun key row -> key <= year)]
listOfWindows |> List.map (fun x ->
let yearToPredict = x.RowKeys |> Seq.max
let train =
x |> Frame.filterRows (fun year _ -> year < yearToPredict)
let test =
x |> Frame.filterRows (fun year _ -> year = yearToPredict)
let mdl = R.lm("silver~gold", train)
let predicted = R.predict_lm(mdl, test).AsNumeric().[0]
let actual = test.["silver"].[yearToPredict]
yearToPredict, predicted, actual
)
/// val it : (int * float * float) list =
/// [(1987, 2.229193258, 1.281468206); (1988, 1.111561003, 1.248677743);
/// (1989, 1.039488768, 0.7363931172); (1990, 1.680597973, 1.808993845);
/// (1991, 1.943243437, 2.092237694); (1992, 2.555973809, 2.00253973);...
///