分析F#中的股票价格

时间:2013-07-01 05:24:10

标签: f#

我的名字是伊恩。我目前正在www.tryfsharp.org上完成“分析股票价格”教程。我正在尝试使用适当的库在Visual Studio 2012中重现他们的webcontent。

我无法弄清楚为什么MathNET库中的“描述性统计”功能无法正常工作。下面的片段。

返回错误:

  

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

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

//获取统计数据

让stats = DescriptiveStatistics(msftClosesUsd)

// compute std。 dev的。

standardDeviation [对于msftData.Data中的r - > 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)

1 个答案:

答案 0 :(得分:1)

我认为问题是DescriptiveStatistics无法理解度量单位,因此您需要在没有单位注释的情况下传递数据。

最简单的方法是使用float功能将msftClosedUsd转换功能应用于Seq.map中的所有元素:

let stats = DescriptiveStatistics(Seq.map float msftClosesUsd)