1

我叫伊恩。我目前正在学习 www.tryfsharp.org 上的“分析股票价格”教程。我正在尝试使用适当的库在 Visual Studio 2012 中重现他们的网络内容。

我无法弄清楚为什么我在 MathNET 库中的“描述性统计”函数不起作用。下面的片段。

返回错误:

错误 FS0041:方法“DescriptiveStatistics”没有重载匹配。可用的重载如下所示(或在错误列表窗口中)。可能的重载:'DescriptiveStatistics(数据:Collections.Generic.IEnumerable):单位'。类型约束不匹配。CsvProvider<...> 类型与 Collections.Generic.IEnumerable 类型不兼容 'CsvProvider<...>' 类型与'Collections.Generic.IEnumerable' 类型不兼容。可能的重载:'DescriptiveStatistics(数据:Collections.Generic.IEnumerable>):单位'。类型约束不匹配。CsvProvider<...> 类型与 Collections.Generic.IEnumerable> 类型不兼容“CsvProvider<...>”类型与“Collections”类型不兼容。

特定代码和另一个可能的线索:

//获取统计信息

让 stats = DescriptiveStatistics(msftClosesUsd)

//计算标准 开发。

standardDeviation [ for r in msftData.Data -> float r.Close ] //必须将 'float' 添加到 r.Close 以匹配类型

注意:descriptiveStatistics 位于代码的最后,standardDeviation 大约在中间。任何帮助是极大的赞赏!

// *****************************************************************
// ********************Analyzing Stock Prices***********************
// *****************************************************************

// URL: http://www.tryfsharp.org/Learn/financial-computing#analyzing-stock-prices

#r @"...\FSharp.Data.1.1.5\lib\net40\FSharp.Data.dll"
#load @"...\FSharp.Charting.0.82\FSharp.Charting.fsx"

open FSharp.Data
open FSharp.Charting
open System

// Provides a strongly typed view of the file
type Stocks = CsvProvider<"C:\...\Documents\F#\MSFT.csv">

// Get the stock prices from yahoo on MSFT stock
[<Literal>]
let msftUrl = "http://ichart.finance.yahoo.com/table.csv?s=MSFT"
let msftData = Stocks.Load(msftUrl)

// **************** Calculating Standard Deviation *****************

let standardDeviation prices = 
    let count = Seq.length prices
    let avg = Seq.average prices
    let squares = [ for p in prices -> (p - avg) * (p - avg) ]
    sqrt ((Seq.sum squares) / (float count)) // Convert count to float to be able to divide into Seq.sum squares 
                                             //"F# does not insert any numberical conversions implicitly" - website

standardDeviation [ for r in msftData.Data -> float r.Close ] //had to add 'float' to r.Close to match type

// **************** Introducing Units of Measure *****************

type [<Measure>] USD
type [<Measure>] EUR

let msftClosesUsd = [ for r in msftData.Data -> float r.Close * 1.0<USD> ] //had to change r.close to float to get this to work
let msft = msftClosesUsd
// Average price in USD
let avg = msftClosesUsd |> Seq.average

// Convert EUR to USD
let euroToUsd eur = eur * 1.30<USD/EUR> // As of 2013-06-29

// Is the average price greater than 25 Euros?
let limit = 25.0<EUR>
if avg > (euroToUsd limit) then printfn("Greater!") // 1.3*25 = 32.5. Type 'avg' to see average. Mine was 29.4802.

let standardDeviationUnits (prices:seq<float<USD>>) = //"Could annotate the argument with seq<float<'u>> allowing for generic units." -URL

    let count = Seq.length prices
    let avg = Seq.average prices
    let squares = [ for p in prices -> (p - avg) * (p - avg) ]
    sqrt ((Seq.sum squares) / (float count))

// Unquote for calc below.
//standardDeviationUnits msftClosesUsd

// Get Math.NET Numerics Library Here: http://numerics.mathdotnet.com/
// Install and continue.
#r  @"...\MathNet.Numerics.2.5.0\lib\net40\MathNet.Numerics.dll"
open MathNet.Numerics.Statistics

//get Stats

let stats = DescriptiveStatistics(msftClosesUsd)
4

1 回答 1

1

我认为问题在于DescriptiveStatistics不了解度量单位,因此您需要在没有单位注释的情况下将数据传递给它。

最简单的方法是将float转换函数应用于msftClosedUsdusingSeq.map函数中的所有元素:

let stats = DescriptiveStatistics(Seq.map float msftClosesUsd)
于 2013-07-01T11:51:29.063 回答