try :

1
2
3
4
 

gaussianRand 10 |> Seq.average
By on 6/19/2010 1:30 PM ()

Thanks. What about the standard deviation, is there a function for that also?

By on 6/20/2010 1:10 AM ()

I don't think there's one in the library, but the first web-search-hit for "F# standard deviation" is

[link:blogs.msdn.com]

which defines a little function for it.

By on 6/20/2010 1:49 AM ()

You can do it in one iteration with:

1
2
3
4
let variance nums = nums |> Seq.fold (fun (cnt, sm, smsq) n -> (cnt+1., sm+n, smsq+n*n)) (0., 0., 0.)
                         |> (fun (cnt, sm, smsq) -> (cnt*smsq - sm*sm)/(cnt*cnt))
                         
let stdDev nums = sqrt (variance nums)
By on 6/20/2010 3:56 AM ()

Great. Maybe you also have a suggestion for plotting a histogram?

By on 6/20/2010 12:21 PM ()

You can use (shameless plug!) the library I posted here :

[link:cs.hubfs.net]

A basic density histogram can be coded as follows :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 

#r "System.Windows.Forms.DataVisualization.dll" 
#r "FSChart.dll"


open FSChart
open Builder

let hist x0 x1 n xs =
    
    let h  = Array.zeroCreate n
    let dx = (x1 - x0) / float n
    let nx = float <| Seq.length xs
    xs |> Seq.iter (fun x ->
        if x > x0 && x <= x1
        then let k = int <| ceil ((x - x0) / dx) - 1.0 in h.[k] <- h.[k] + 1.0)
    let y = Array.map (fun c -> c / (dx * nx)) h
    let x = Array.init n (fun i -> x0 + (float i + 0.5) * dx)
    
    let area  = Gallery.Area( Title = "Density Histogram" ) |> axisX ( linear 1.0 )
    let chart = single area [ bar 1.0 >> color blue >> border darkBlue 1 ]
    let plot  = Plot( MSChart(), chart )
    plot.Display( [x, y] )

hist -1.0 3.0 32 (gaussianRand 10000) // the arguments (xmin xmax nbins) are adapted to your example.
By on 6/20/2010 3:45 PM ()

I downloaded FSChart08 and managed to build it. But I didn't get neither the samples nor this histogram working. There is some problem with the referencing. I did put the FSChart08 library in C:\Program (x86)\, but it won't find the .dll files there. However, it finds:

--> Referenced 'c:\Program Files (x86)\Microsoft Chart Controls\Assemblies\System.Windows.Forms.DataVisualization.dll'

What is the solution to this?

By on 6/21/2010 2:01 AM ()

The samples script reference is a relative path to the release version of the library (i.e. it is meant to be run from its original location) : check that you built the correct version or change the reference to the debug directory.

Regarding the histogram code, you should put it in a script with a full path reference (as Mau pointed out) or copy the dll to the same folder.

By on 6/21/2010 2:56 AM ()

I tried FSI, but got the following error this time:

#r "C:\\Program (x86)\FSChart08\FSChart\bin\Debug\System.Windows.Forms.DataVisualization.dll"

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

stdin(1,1): error FS2301: 'C:\Program (x86)\FSChart08\FSChartin\Debug\System.Windows.Forms.DataVisualization.dll' is not a valid assembly name

By on 6/21/2010 3:17 AM ()

Or you can use verbatim strings prefixed with @ (this way you don't have to escape the '\').

By on 6/21/2010 3:49 AM ()

I'm afraid that neither works

#r "C:\\Program (x86)\\FSChart08\\FSChart\\bin\\Debug\\FSChart.dll"

#r @"C:\Program (x86)\FSChart08\FSChart\bin\Debug\FSChart.dll"

By on 6/21/2010 4:05 AM ()

I tested a little script with just the histogram on my machine, with the following references :

#r @"C:\Program Files\FSChart08\FSChart\bin\Debug\System.Windows.Forms.DataVisualization.dll"

#r @"C:\Program Files\FSChart08\FSChart\bin\Debug\FSChart.dll"

It works without problems. Be sure that the references are put in this precise order.

By on 6/21/2010 4:21 AM ()

I'm afraid it doesn't for me. Maybe I did something wrong in the build. Which file in FSChart should I do the build from?

By on 6/21/2010 5:23 AM ()

If you extracted the zip to a folder FSChart08 then the solution to build will be FSChart08\FSChart.sln

After building (e.g. the debug version), you should find the library (FSChart.dll) in FSChart08\FSChart\bin\Debug\, along with the Microsoft Chart Control assembly. If you are out of luck with absolute paths, try to put the two libraries in the same folder as your script and reference them as in my first code snippet.

By on 6/21/2010 6:21 AM ()

No luck. I keep on getting either

stdin(2,1): error FS0084: Assembly reference 'FSChart.dll' was not found or is invalid

or

stdin(2,1): error FS0082: Could not resolve this reference. Could not locate the assembly "FSChart.dll". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. (Code=MSB3245)

By on 6/21/2010 6:45 AM ()

(1) You seem to be using VS2008 : is .NET 3.5 SP1 installed, also have you installed the Chart Control with its dedicated setup program MSChart.exe ?

(2) Did you try the local solution, that is, create a folder Histogram, copy FSChart.dll and System.Windows.Forms.DataVisualization.dll, then create a script histogram.fsx in this folder beginning as follows ?

1
2
3
4
5
6
#r @"System.Windows.Forms.DataVisualization.dll"
#r @"FSChart.dll"

open FSChart
open Builder
By on 6/21/2010 8:00 AM ()

Seems to work now, I used a .txt file instead of a .fsx file. Should that matter?

By on 6/21/2010 8:33 AM ()

What version of F# are you using?

(you can check that by right-clicking the FSI windows and selecting 'reset session'), or scrolling up to the top.

By on 6/21/2010 4:14 AM ()

You might have to escape all the "\" with "\\" ...

By on 6/21/2010 3:26 AM ()

1) If you want to compile the project, you need to right click the project file in the solution explorer, choose add reference, browse, select the library dll.

2) If you want to run interactively in FSI, you need to type

#r "CompletePathOfYourLibrary"

e.g.

#r "C:\\MyLib.dll"

By on 6/21/2010 2:18 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper