我叫伊恩。我目前正在学习www.tryfsharp.org上的“分析股票价格”教程。我试图在Visual 2012中使用适当的库复制它们的the内容。
--我很难弄清楚为什么我在MathNET库中的“描述符”函数不能工作。下面的片段.
返回错误:
错误FS0041:没有重载匹配的方法'DescriptiveStatistics‘。可用的重载如下所示(或在错误列表窗口中)。可能的重载:‘DescriptiveStatistics(数据: Collections.Generic.IEnumerable):unit’。类型约束不匹配。类型CsvProvider<...>与类型Collections.Generic.IEnumerable不兼容,类型'CsvProvider<...>‘与类型’compatible .Generic.IEnDigable‘不兼容。可能的重载:‘DescriptiveStatistics(数据: Collections.Generic.IEnumerable>):unit’。类型约束不匹配。类型CsvProvider<...>与类型Collections.Generic.IEnumerable>不兼容,类型'CsvProvider<...>‘与类型’compatible .Generic.IEnDigable>‘不兼容。
特定的代码和另一个可能的线索:
//获取统计数据
设stats = DescriptiveStatistics(msftClosesUsd)
//计算std。dev.
standardDeviation for r in msftData.Data -> float r.Close //不得不在r.Close中添加“float”以匹配类型
注意: 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)发布于 2013-07-01 11:51:29
我认为问题在于DescriptiveStatistics不理解度量单位,因此您需要在没有单元注释的情况下传递数据。
最简单的方法是使用float函数对msftClosedUsd中的所有元素应用Seq.map转换函数:
let stats = DescriptiveStatistics(Seq.map float msftClosesUsd)https://stackoverflow.com/questions/17397887
复制相似问题