Reputation: 374
My name is Ian. I'm currently working through the "Analyzing Stock Prices" tutorial on www.tryfsharp.org. I'm trying to reproduce their webcontent in Visual Studio 2012 with the appropriate libraries.
I am having trouble figuring out why my "descriptivestatistics" function in the MathNET library is not working. Snippets below.
Returned error:
error FS0041: No overloads match for method 'DescriptiveStatistics'. The available overloads are shown below (or in the Error List window). Possible overload: 'DescriptiveStatistics(data: Collections.Generic.IEnumerable) : unit'. Type constraint mismatch. The type CsvProvider<...> is not compatible with type Collections.Generic.IEnumerable The type 'CsvProvider<...>' is not compatible with the type 'Collections.Generic.IEnumerable'. Possible overload: 'DescriptiveStatistics(data: Collections.Generic.IEnumerable>) : unit'. Type constraint mismatch. The type CsvProvider<...> is not compatible with type Collections.Generic.IEnumerable> The type 'CsvProvider<...>' is not compatible with the type 'Collections.Generic.IEnumerable>'.
The particular code and another possible clue:
//get Stats
let stats = DescriptiveStatistics(msftClosesUsd)
//compute std. dev.
standardDeviation [ for r in msftData.Data -> float r.Close ] //had to add 'float' to r.Close to match type
Note: descriptiveStatistics is located at the very end of the code, and standardDeviation is about midway down. Any help is greatly appreciated!
// *****************************************************************
// ********************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)
Upvotes: 1
Views: 849
Reputation: 243096
I think the problem is that DescriptiveStatistics
does not understand units of measure and so you need to pass it the data without the unit annotations.
The easiest way to do this is to apply float
conversion function to all elements in your msftClosedUsd
using Seq.map
function:
let stats = DescriptiveStatistics(Seq.map float msftClosesUsd)
Upvotes: 1