<feed xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US">
    <title>The Mad Economist</title>
    <link rel="self" type="application/atom+xml" href="http://www.caribousoftware.com/BobsBlog/Atom.aspx" />
    <subtitle type="html">Rants on Stupidity</subtitle>
    <id>http://www.caribousoftware.com/BobsBlog/Default.aspx</id>
    <author>
        <name>Bob Lucke</name>
        <uri>http://www.caribousoftware.com/BobsBlog/Default.aspx</uri>
    </author>
    <generator uri="http://subtextproject.com" version="Subtext Version 2.0.0.43">Subtext</generator>
    <updated>2009-10-13T16:49:38Z</updated>
    <entry>
        <title>F# - Fluent NHibernate - POCO Class Object</title>
        <link rel="alternate" type="text/html" href="http://www.caribousoftware.com/BobsBlog/archive/2009/10/13/f-fluent-nhibernate-poco-class-object.aspx" />
        <id>http://www.caribousoftware.com/BobsBlog/archive/2009/10/13/f-fluent-nhibernate-poco-class-object.aspx</id>
        <published>2009-10-13T16:44:59Z</published>
        <updated>2009-10-13T16:49:38Z</updated>
        <content type="html">&lt;p&gt;&lt;font face="Arial"&gt;The next thing I want to try is to use an F# Class (instead of C#) with Fluent NHibernate.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;In my last post I worked with a C# Class &amp;amp; Fluent NHibernate.  The C# Class looked as follows:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;------------------------------------------------------------------&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;namespace StockPrices {&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    public class COMPANY {&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;        public virtual int Id { get; set; }&lt;br /&gt;
        public virtual string COMPANY_NAME { get; set; }&lt;br /&gt;
        public virtual string COMPANY_TICKER { get; set; }&lt;br /&gt;
        public virtual int ASSET_TYPE_ID { get; set; }&lt;br /&gt;
        public virtual int IS_ACTIVE { get; set; }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;}&lt;br /&gt;
------------------------------------------------------------------&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;So how do we make the same class in F#?&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;The first thing I tried was this:&lt;/font&gt;&lt;/p&gt;
&lt;font face="Arial"&gt;
&lt;p&gt;&lt;br /&gt;
&lt;font face="Courier New"&gt;------------------------------------------------------------------&lt;br /&gt;
#light&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;namespace StockPrices&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;open System&lt;br /&gt;
open System.Collections.Generic&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    // -----------------  1&lt;br /&gt;
    type COMPANY = class&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;        [&amp;lt;DefaultValue&amp;gt;] val mutable Id : int &lt;br /&gt;
        [&amp;lt;DefaultValue&amp;gt;] val mutable COMPANY_NAME : string&lt;br /&gt;
        [&amp;lt;DefaultValue&amp;gt;] val mutable COMPANY_TICKER : string&lt;br /&gt;
        [&amp;lt;DefaultValue&amp;gt;] val mutable ASSET_TYPE_ID : int &lt;br /&gt;
        [&amp;lt;DefaultValue&amp;gt;] val mutable IS_ACTIVE : int &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;        new() = {}&lt;br /&gt;
        &lt;br /&gt;
    end&lt;br /&gt;
------------------------------------------------------------------&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;This is the simplest way of setting up a class in F#.&lt;/p&gt;
&lt;p&gt;In this example, I use the default constructor -- these are the open parentheses in the New() statement.  Therefore, my properties are not initialized.  In F# this is a no no and is not allowed.  When you do not initialize a variable you need to use the [&amp;lt;DefaultValue&amp;gt;] attribute to initialize the value to Zero, it that is possible.  &lt;/p&gt;
&lt;p&gt;From Microsoft:  "The DefaultValue attribute is required on explicit fields in class types that have a primary constructor. This attribute specifies that the field is initialized to zero. The type of the field must support zero-initialization."&lt;/p&gt;
&lt;p&gt;So, the above statement gives me a class with five public variables -- the integers are initialized with Zeros; the strings are initialized with Nulls.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;
The following is my main F# program.  Note that my COMPANY class is in a module called StockPrices that I open at the start of the program.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;-----------------------------------------------------------------------------------------------&lt;br /&gt;
#light&lt;br /&gt;
open System&lt;br /&gt;
open System.Collections.Generic&lt;br /&gt;
open System.IO&lt;br /&gt;
open StockPrices&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;open FluentNHibernate.Automapping&lt;br /&gt;
open FluentNHibernate&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let properties = new Dictionary&amp;lt;string, string&amp;gt;()&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;properties.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider")&lt;br /&gt;
properties.Add("dialect", "NHibernate.Dialect.MsSql2000Dialect")&lt;br /&gt;
properties.Add("connection.driver_class", "NHibernate.Driver.SqlClientDriver")&lt;br /&gt;
properties.Add("show_sql", "true")&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;properties.Add("proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle")&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let connString = "server='BIG_ROCK\LOGGERSEDGE';Initial Catalog=SMDATA;User ID=sa;Password=XXXXX"&lt;br /&gt;
properties.Add("connection.connection_string", connString)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;//-&amp;gt; The New&lt;br /&gt;
let autoMappings = (FluentNHibernate.Automapping.AutoMap.AssemblyOf&amp;lt;StockPrices.COMPANY&amp;gt;()).Where(fun t -&amp;gt; (t.Namespace="StockPrices"))&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let aConfig = (new NHibernate.Cfg.Configuration()).AddProperties(properties).AddAutoMappings(autoMappings)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let sessionFactory = aConfig.BuildSessionFactory()&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let aSession = sessionFactory.OpenSession()&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;aSession.BeginTransaction()&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;//AT&amp;amp;T is Id 100&lt;br /&gt;
let coID = 100&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let someObj = aSession.Load(typeof&amp;lt;StockPrices.COMPANY&amp;gt;, coID) :?&amp;gt; StockPrices.COMPANY&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;printfn "Company Name: %s,  Ticker: %s" someObj.COMPANY_NAME someObj.COMPANY_TICKER&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;
&lt;font face="Courier New"&gt;aSession.Close()&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let userresp = Console.ReadLine() &lt;br /&gt;
-----------------------------------------------------------------------------------------------&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;This program craps out on the line:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let aConfig = (new NHibernate.Cfg.Configuration()).AddProperties(properties).AddAutoMappings(autoMappings)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;with the error:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;{"(XmlDocument)(2,4): XML validation error: The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has incomplete content. List of possible elements expected: 'meta, subselect, cache, synchronize, comment, tuplizer, id, composite-id' in namespace 'urn:nhibernate-mapping-2.2'."}&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;To be honest, I really have no clue what this all means.&lt;/p&gt;
&lt;p&gt;So, I decide to take another tack.  I try an F# class that actually uses the 'member' syntax:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;-----------------------------------------------------------------------------------------------&lt;br /&gt;
    type COMPANY() = class&lt;br /&gt;
    &lt;br /&gt;
        let mutable _Id : int = 0&lt;br /&gt;
        let mutable _COMPANY_NAME : string = ""&lt;br /&gt;
        let mutable _COMPANY_TICKER : string = ""&lt;br /&gt;
        let mutable _ASSET_TYPE_ID : int = 0&lt;br /&gt;
        let mutable _IS_ACTIVE : int = 0&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;        member x.Id with get() = _Id and set(v) = _Id &amp;lt;- v&lt;br /&gt;
        member x.COMPANY_NAME with get() = _COMPANY_NAME and set(v) = _COMPANY_NAME &amp;lt;- v&lt;br /&gt;
        member x.COMPANY_TICKER with get() = _COMPANY_TICKER and set(v) = _COMPANY_TICKER &amp;lt;- v&lt;br /&gt;
        member x.ASSET_TYPE_ID with get() = _ASSET_TYPE_ID and set(v) = _ASSET_TYPE_ID &amp;lt;- v&lt;br /&gt;
        member x.IS_ACTIVE with get() = _IS_ACTIVE and set(v) = _IS_ACTIVE &amp;lt;- v&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    end&lt;br /&gt;
-----------------------------------------------------------------------------------------------&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;I rerun the my program.&lt;/p&gt;
&lt;p&gt;This time, I make it one more line before it crashes on the line:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let sessionFactory = aConfig.BuildSessionFactory()&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;This time I get this big ugly:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;NHibernate.InvalidProxyTypeException was unhandled&lt;br /&gt;
Message="The following types may not be used as proxies:&lt;br /&gt;
StockPrices.COMPANY: method get_Id should be 'public/protected virtual' or 'protected internal virtual'&lt;br /&gt;
StockPrices.COMPANY: method set_Id should be 'public/protected virtual' or 'protected internal virtual'&lt;br /&gt;
StockPrices.COMPANY: method get_COMPANY_NAME should be 'public/protected virtual' or 'protected internal virtual'&lt;br /&gt;
StockPrices.COMPANY: method set_COMPANY_NAME should be 'public/protected virtual' or 'protected internal virtual'&lt;br /&gt;
StockPrices.COMPANY: method get_COMPANY_TICKER should be 'public/protected virtual' or 'protected internal virtual'&lt;br /&gt;
StockPrices.COMPANY: method set_COMPANY_TICKER should be 'public/protected virtual' or 'protected internal virtual'&lt;br /&gt;
StockPrices.COMPANY: method get_ASSET_TYPE_ID should be 'public/protected virtual' or 'protected internal virtual'&lt;br /&gt;
StockPrices.COMPANY: method set_ASSET_TYPE_ID should be 'public/protected virtual' or 'protected internal virtual'&lt;br /&gt;
StockPrices.COMPANY: method get_IS_ACTIVE should be 'public/protected virtual' or 'protected internal virtual'&lt;br /&gt;
StockPrices.COMPANY: method set_IS_ACTIVE should be 'public/protected virtual' or 'protected internal virtual'&lt;br /&gt;
StockPrices.COMPANY: field _Id should not be public nor internal&lt;br /&gt;
StockPrices.COMPANY: field _COMPANY_NAME should not be public nor internal&lt;br /&gt;
StockPrices.COMPANY: field _COMPANY_TICKER should not be public nor internal&lt;br /&gt;
StockPrices.COMPANY: field _ASSET_TYPE_ID should not be public nor internal&lt;br /&gt;
StockPrices.COMPANY: field _IS_ACTIVE should not be public nor internal"&lt;br /&gt;
&lt;/font&gt;&lt;br /&gt;
This message provides a few clues.  The messages seem to indicate that my class needs to use virtual properties (ala C#).  I also assume this to mean that my F# syntax does &lt;strong&gt;not &lt;/strong&gt;yield virtual properties.&lt;/p&gt;
&lt;p&gt;So, I need to poke around a find how to create virtual properties in F#&lt;/p&gt;
&lt;p&gt;This is what I came up with:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;-----------------------------------------------------------------------------------------------&lt;br /&gt;
    type COMPANY() = class&lt;br /&gt;
    &lt;br /&gt;
        let mutable _Id : int = 0&lt;br /&gt;
        let mutable _COMPANY_NAME : string = ""&lt;br /&gt;
        let mutable _COMPANY_TICKER : string = ""&lt;br /&gt;
        let mutable _ASSET_TYPE_ID : int = 0&lt;br /&gt;
        let mutable _IS_ACTIVE : int = 0&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;        abstract Id : int with get, set&lt;br /&gt;
        default x.Id with get() = _Id and set(v) = _Id &amp;lt;- v&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;        abstract COMPANY_NAME : string with get, set&lt;br /&gt;
        default x.COMPANY_NAME with get() = _COMPANY_NAME and set(v) = _COMPANY_NAME &amp;lt;- v&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;        abstract COMPANY_TICKER : string with get, set&lt;br /&gt;
        default x.COMPANY_TICKER with get() = _COMPANY_TICKER and set(v) = _COMPANY_TICKER &amp;lt;- v&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;        abstract ASSET_TYPE_ID : int with get, set&lt;br /&gt;
        default x.ASSET_TYPE_ID with get() = _ASSET_TYPE_ID and set(v) = _ASSET_TYPE_ID &amp;lt;- v&lt;br /&gt;
    &lt;br /&gt;
        abstract IS_ACTIVE : int with get, set&lt;br /&gt;
        default x.IS_ACTIVE with get() = _IS_ACTIVE and set(v) = _IS_ACTIVE &amp;lt;- v&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    end&lt;br /&gt;
-----------------------------------------------------------------------------------------------&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;It is very similar to my class above, however, it uses the keyword 'abstract' to create an abstract property.  The 'default' keyword (replacing the member keyword) then heads of the implementation of the property in the current class.&lt;/p&gt;
&lt;p&gt;This is from MSDN:&lt;/p&gt;
&lt;p&gt;"Properties can be abstract. As with methods, abstract just means that there is a virtual dispatch associated with the property. Abstract properties can be truly abstract, that is, without a definition in the same class. The class that contains such a property is therefore an abstract class. Alternatively, abstract can just mean that a property is virtual, and in that case, a definition must be present in the same class. Note that abstract properties must not be private, and if one accessor is abstract, the other must also be abstract."http://msdn.microsoft.com/en-us/library/dd483467(VS.100).aspx&lt;/p&gt;
&lt;p&gt;If one were to refer to a POCO class in F#, this would probably be along the lines you would expect.&lt;/p&gt;
&lt;p&gt;OK, let's give this puppy a twirl!&lt;/p&gt;
&lt;p&gt;Kaboom!&lt;/p&gt;
&lt;p&gt;I crash again on the same line as before.&lt;/p&gt;
&lt;p&gt;This is my error:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;NHibernate.InvalidProxyTypeException was unhandled&lt;br /&gt;
  Message="The following types may not be used as proxies:&lt;br /&gt;
StockPrices.COMPANY: field _Id should not be public nor internal&lt;br /&gt;
StockPrices.COMPANY: field _COMPANY_NAME should not be public nor internal&lt;br /&gt;
StockPrices.COMPANY: field _COMPANY_TICKER should not be public nor internal&lt;br /&gt;
StockPrices.COMPANY: field _ASSET_TYPE_ID should not be public nor internal&lt;br /&gt;
StockPrices.COMPANY: field _IS_ACTIVE should not be public nor internal"&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Shorter, but still a problem.  At least I have gotten rid of the 'virtual property' errors and I am now left with a bunch of errors that seem to say that my private variable placeholders are illegal.  I find this odd, as I do not get the same message with this type of syntax in VB:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;------------------------------------------------------------------------- &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;Option Explicit On&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;Namespace StockPrices&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    Public Class COMPANY&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;        Private _ID As Integer&lt;br /&gt;
        Private _CompanyName As String&lt;br /&gt;
        Private _CompanyTicker As String&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;        Public Overridable Property Id() As Integer&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;            Get&lt;br /&gt;
                Id = _ID&lt;br /&gt;
            End Get&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;            Set(ByVal value As Integer)&lt;br /&gt;
                _ID = value&lt;br /&gt;
            End Set&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;        End Property&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;        Public Overridable Property COMPANY_NAME() As String&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;            Get&lt;br /&gt;
                COMPANY_NAME = _CompanyName&lt;br /&gt;
            End Get&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;            Set(ByVal value As String)&lt;br /&gt;
                _CompanyName = value&lt;br /&gt;
            End Set&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;        End Property&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;        Public Overridable Property COMPANY_TICKER() As String&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;            Get&lt;br /&gt;
                COMPANY_TICKER = _CompanyTicker&lt;br /&gt;
            End Get&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;            Set(ByVal value As String)&lt;br /&gt;
                _CompanyTicker = value&lt;br /&gt;
            End Set&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;        End Property&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    End Class&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;End Namespace&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;------------------------------------------------------------------------- &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;The above outline for a class works fine with Fluent NHibernate.&lt;/p&gt;
&lt;p&gt;What to do?&lt;/p&gt;
&lt;p&gt;Well, I know my class would be OK if I could just get rid of the proxy errors.  I mean these variables should be irrelevant, no?&lt;/p&gt;
&lt;p&gt;So, I found a way to suppress them:&lt;/p&gt;
&lt;p&gt;&lt;font style="BACKGROUND-COLOR: #ffff00" face="Courier New"&gt;properties.Add("use_proxy_validator", "false")&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;I just need to add the above line to my properties collection.  This kills the proxy validator.&lt;/p&gt;
&lt;p&gt;I know this could have unknown repercussions, so if anyone knows a legitimate way around this problem, I would sure like to know.&lt;/p&gt;
&lt;p&gt;Low and behold, this actually works.&lt;/p&gt;
&lt;p&gt;I get exactly what I expect!&lt;/p&gt;
&lt;p&gt;My output:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;------------------------------------------------------------------------- &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;NHibernate: SELECT company0_.Id as Id0_0_, company0_.IS_ACTIVE as IS2_0_0_, company0_.ASSET_TYPE_ID as ASSET3_0_0_, company0_.COMPANY_TICKER as COMPANY4_0_0_, company0_.COMPANY_NAME as COMPANY5_0_0_ FROM [COMPANY] company0_ WHERE company0_.Id=@p0;@p0 = 100&lt;br /&gt;
Company Name: AT&amp;amp;T,  Ticker: T&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&lt;font face="Courier New"&gt;------------------------------------------------------------------------- &lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;font face="Arial"&gt;
&lt;p&gt; We are now good!&lt;/p&gt;
&lt;/font&gt;&lt;/font&gt;&lt;img src="http://www.caribousoftware.com/BobsBlog/aggbug/51.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://www.caribousoftware.com/BobsBlog/comments/51.aspx</wfw:comment>
        <slash:comments>6</slash:comments>
        <wfw:commentRss>http://www.caribousoftware.com/BobsBlog/comments/commentRss/51.aspx</wfw:commentRss>
        <trackback:ping>http://www.caribousoftware.com/BobsBlog/services/trackbacks/51.aspx</trackback:ping>
    </entry>
    <entry>
        <title>F# - Fluent NHibernate RTM</title>
        <link rel="alternate" type="text/html" href="http://www.caribousoftware.com/BobsBlog/archive/2009/09/22/f-fluent-nhibernate-rtm.aspx" />
        <id>http://www.caribousoftware.com/BobsBlog/archive/2009/09/22/f-fluent-nhibernate-rtm.aspx</id>
        <published>2009-09-22T13:34:09Z</published>
        <updated>2009-09-22T13:43:52Z</updated>
        <content type="html">&lt;p&gt;Well, I finally got the courage to download the RTM version of Fluent NHibernate.&lt;/p&gt;
&lt;p&gt;I wrote about using Fluent NHibernate in an earlier post (June 28, 2009), so a lot of this is repetitive and some is new.&lt;/p&gt;
&lt;p&gt;I have highlighted in yellow the important stuff.&lt;/p&gt;
&lt;p&gt;First, I create a simple class&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;------------------------------------------------------------------&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;namespace StockPrices {&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    public class COMPANY {&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;        public virtual int Id { get; set; }&lt;br /&gt;
        public virtual string COMPANY_NAME { get; set; }&lt;br /&gt;
        public virtual string COMPANY_TICKER { get; set; }&lt;br /&gt;
        public virtual int ASSET_TYPE_ID { get; set; }&lt;br /&gt;
        public virtual int IS_ACTIVE { get; set; }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;}&lt;br /&gt;
------------------------------------------------------------------&lt;br /&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;The COMPANY class has only five properties: Id, COMPANY_NAME and COMPANY_TICKER, ASSET_TYPE_ID and IS_ACTIVE&lt;/p&gt;
&lt;p&gt;I next create a SQL Server table that is iso-morphic to my class.&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;CREATE TABLE [COMPANY] (&lt;br /&gt;
 [Id] [int] IDENTITY (1, 1) NOT NULL ,&lt;br /&gt;
 [COMPANY_NAME] [varchar] (100) NOT NULL ,&lt;br /&gt;
 [COMPANY_TICKER] [varchar] (50) NOT NULL ,&lt;br /&gt;
 [ASSET_TYPE_ID] [int] NOT NULL ,&lt;br /&gt;
 [IS_ACTIVE] [int] NOT NULL ,&lt;br /&gt;
 CONSTRAINT [PK_COMPANY] PRIMARY KEY  CLUSTERED &lt;br /&gt;
 (&lt;br /&gt;
  [Id]&lt;br /&gt;
 )  ON [PRIMARY] &lt;br /&gt;
) ON [PRIMARY]&lt;br /&gt;
GO&lt;br /&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;OK, now I can start to work in F#&lt;/p&gt;
&lt;p&gt;I now a new project.&lt;/p&gt;
&lt;p&gt;I first add a reference to my StockPrices class and also an open statement:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;#light&lt;br /&gt;
open System&lt;br /&gt;
open System.Collections.Generic&lt;br /&gt;
open System.IO&lt;br /&gt;
open StockPrices&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;I now need to add references to FluentNHibernate and NHibernate (I do believe that you need both)&lt;/p&gt;
&lt;p&gt;I then add two open statements into my code for FluentNHibernate&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;------------------------------------------------------------------&lt;br /&gt;
open FluentNHibernate&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font style="BACKGROUND-COLOR: #ffff00" face="Courier New"&gt;//Old&lt;br /&gt;
//open FluentNHibernate.AutoMap&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font style="BACKGROUND-COLOR: #ffff00" face="Courier New"&gt;//New RTM&lt;br /&gt;
open FluentNHibernate.Automapping&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
&lt;/font&gt;Note the change in the namespace "AutoMap" has been changed to "AutoMapping".&lt;/p&gt;
&lt;p&gt;The next thing I do is declare a dictionary object to hold a set of configuration properties.  I then add a set of attributes.&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let properties = new Dictionary&amp;lt;string, string&amp;gt;()&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;properties.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider")&lt;br /&gt;
properties.Add("dialect", "NHibernate.Dialect.MsSql2000Dialect")&lt;br /&gt;
properties.Add("connection.driver_class", "NHibernate.Driver.SqlClientDriver")&lt;br /&gt;
properties.Add("show_sql", "true")&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let connString = "server='BIG_ROCK\LOGGERSEDGE';Initial Catalog=SMDATA;User ID=sa;Password=XXXXXX"&lt;br /&gt;
properties.Add("connection.connection_string", connString)&lt;br /&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Not much to say here, other than I am using SQL Server 2000, so I have to tell nHibernate to use the MsSql2000Dialect.  I set my connection string and add it to my set of properties.&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;In the RTM version, I also had to add the following property:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font style="BACKGROUND-COLOR: #ffff00"&gt;properties.Add("proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle")&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;I did not need this before, but you need it now.  Otherwise you get this big ugly:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;The ProxyFactoryFactory was not configured.&lt;br /&gt;
Initialize 'proxyfactory.factory_class' property of the session-factory configuration section with one of the available NHibernate.ByteCode providers.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;property name='proxyfactory.factory_class'&amp;gt;NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu&amp;lt;/property&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;property name='proxyfactory.factory_class'&amp;gt;NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle&amp;lt;/property&amp;gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;To be honest, I have not explored the guts of NHibernate to figure this all out.  I just know I could not get my older version to run without the proxyfactory.factory_class property.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;Next, come the autoMappings.  This has also changed.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font style="BACKGROUND-COLOR: #ffff00"&gt;//-&amp;gt; The Old&lt;br /&gt;
//let autoMappings = (AutoPersistenceModel.MapEntitiesFromAssemblyOf&amp;lt;StockPrices.COMPANY&amp;gt;()).Where(fun t -&amp;gt; (t.Namespace = "StockPrices"))&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font style="BACKGROUND-COLOR: #ffff00"&gt;//-&amp;gt; The New&lt;br /&gt;
let autoMappings = (FluentNHibernate.Automapping.AutoMap.AssemblyOf&amp;lt;StockPrices.COMPANY&amp;gt;()).Where(fun t -&amp;gt; (t.Namespace="StockPrices"))&lt;br /&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;Not too much difference here -- basically the method "MapEntitiesFromAssemblyOf" has been replaced with "AutoMap.AssemblyOf".  It just took a while to figure it all out.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;The point of this statement is that I want FluentNHibernate to map my Company class object and my database table automatically, obviating the need for a mapping interface xml file.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;This part:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;let autoMappings = FluentNHibernate.Automapping.AutoMap.AssemblyOf&amp;lt;StockPrices.COMPANY&amp;gt;()&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;says map my object StockPrices.COMPANY to the database.  I then need to add a where clause that tells FluentNHibernate  what namespace to use.  You should check out the FluentNHiernate site to get a more technical understanding of what is going on in the background.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;I also discovered I need to wrap this:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;FluentNHibernate.Automapping.AutoMap.AssemblyOf&amp;lt;StockPrices.COMPANY&amp;gt;()&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;in parentheses like this:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;(FluentNHibernate.Automapping.AutoMap.AssemblyOf&amp;lt;StockPrices.COMPANY&amp;gt;())&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;in order to get intellisense to work.  Also, it generated this convoluted error:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;"Successive arguments should be separated by spaces or tupled, and arguments involving function or method applications should be parenthesized"&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;It took me a while to figure out what this meant.  I guess I must be stupid.  Anyway, the where clause:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;.Where(fun t -&amp;gt; t.Namespace = "StockPrices")&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;is an anonymous or lambda function.  The function returns true if the namespace of an entity equals the name "StockPrices" where my COMPANY class resides.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial" size="2"&gt;F# provides a way to define a nameless function using the keyword &lt;font face="Courier New"&gt;fun&lt;/font&gt;. This type of function receives just one input value and returns just one output value.  Generally, if a function is to be passed as an argument to another function (as in the case here), then often you don’t need to give it a name of its own. T&lt;/font&gt;&lt;font face="Arial"&gt;hese functions are referred to as anonymous functions and sometimes called lambda functions or even just lambdas.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;The guts of the function above:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;t.Namespace = "StockPrices"&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt; could have just as easily have been written:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;if t.Namespace = "StockPrices" then true else false&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;but that would not have been as cool.&lt;/font&gt;&lt;/p&gt;
&lt;font face="Arial"&gt;&lt;font face="Arial"&gt;&lt;font face="Arial"&gt;
&lt;p&gt;My next line:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let aConfig = (new NHibernate.Cfg.Configuration()).AddProperties(properties).AddAutoMappings(autoMappings)&lt;/font&gt;&lt;/p&gt;
&lt;font face="Courier New"&gt;
&lt;p&gt;&lt;font face="Arial"&gt;does quite a bit of work: It&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;(1)  Instantiates an NHibernate Configuration;&lt;br /&gt;
(2)  Add my dictionary lit of properties to the Configuration; and most importantly&lt;br /&gt;
(3)  Adds my autoMappings&lt;br /&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;Again, according to Gregory, &lt;code&gt;AddAutoMappings&lt;/code&gt; substitutes for &lt;code&gt;AddAssembly (used in regular NHibernate)&lt;/code&gt;. This allows us to stop NHibernate from looking for &lt;code&gt;hbm.xml&lt;/code&gt; files, and use our auto mapped entities instead.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;The next block of code opens a NHibernate session and looks up AT&amp;amp;T in my database.  It then prints the company name and ticker to the console, giving us the gratification of knowing that something works.&lt;/font&gt; &lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let sessionFactory = aConfig.BuildSessionFactory()&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let aSession = sessionFactory.OpenSession()&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;aSession.BeginTransaction()&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;//AT&amp;amp;T is Id 100&lt;br /&gt;
let coID = 100&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let someObj = aSession.Load(typeof&amp;lt;StockPrices.COMPANY&amp;gt;, coID) :?&amp;gt; StockPrices.COMPANY&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;printfn "Company Name: %s,  Ticker: %s" someObj.COMPANY_NAME someObj.COMPANY_TICKER&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;aSession.Close()&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let userresp = Console.ReadLine() &lt;br /&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&lt;font face="Arial"&gt;The one weird piece of code is this:&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&lt;font face="Courier New"&gt;let someObj = aSession.Load(typeof&amp;lt;StockPrices.COMPANY&amp;gt;, coID) :?&amp;gt; StockPrices.COMPANY&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;should return a type of  StockPrices.COMPANY, but it does not. -- it returns a generic Object.  I have not figured out why.  It certainly does in VB.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;So, I have to cast a generic Object into StockPrices.COMPANY so I could actually use it.  In F#,  there is an operator ":?&amp;gt;"&lt;/font&gt;&lt;font face="Arial"&gt;  known as on downcast operator the uses the syntax: &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;x :?&amp;gt; T&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;According to our buddies at MSFT:&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;"The :?&amp;gt; operator performs a dynamic cast, which means that the success of the cast is determined at run time. A cast that uses the :?&amp;gt; operator is not checked at compile time; but at run time, an attempt is made to cast to the specified type. If the object is compatible with the target type, the cast succeeds. If the object is not compatible with the target type, the runtime raises an InvalidCastException."&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;Anyhow, this operator allows me to cast the result into a type StockPrices.COMPANY.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;I then print the output to the console.  Note:  I also get my sql query generated by NHibernate.  This is the result of setting the show_sql property to true: properties.Add("show_sql", "true").&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;Next Task:  Use an F# Class instead of a C# Class&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;Here is the full F# code:&lt;/font&gt;&lt;/p&gt;
&lt;font face="Arial"&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;-----------------------------------------------------------------&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;#light&lt;br /&gt;
open System&lt;br /&gt;
open System.Collections.Generic&lt;br /&gt;
open System.IO&lt;br /&gt;
open StockPrices&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;open FluentNHibernate.Automapping&lt;br /&gt;
open FluentNHibernate&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let properties = new Dictionary&amp;lt;string, string&amp;gt;()&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;properties.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider")&lt;br /&gt;
properties.Add("dialect", "NHibernate.Dialect.MsSql2000Dialect")&lt;br /&gt;
properties.Add("connection.driver_class", "NHibernate.Driver.SqlClientDriver")&lt;br /&gt;
properties.Add("show_sql", "true")&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;properties.Add("proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle")&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let connString = "server='BIG_ROCK\LOGGERSEDGE';Initial Catalog=SMDATA;User ID=sa;Password=XXXXXX"&lt;br /&gt;
properties.Add("connection.connection_string", connString)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;//-&amp;gt; The Old&lt;br /&gt;
//let autoMappings = (AutoPersistenceModel.MapEntitiesFromAssemblyOf&amp;lt;StockPrices.COMPANY&amp;gt;()).Where(fun t -&amp;gt; (t.Namespace = "StockPrices"))&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;//-&amp;gt; The New&lt;br /&gt;
let autoMappings = (FluentNHibernate.Automapping.AutoMap.AssemblyOf&amp;lt;StockPrices.COMPANY&amp;gt;()).Where(fun t -&amp;gt; (t.Namespace="StockPrices"))&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let aConfig = (new NHibernate.Cfg.Configuration()).AddProperties(properties).AddAutoMappings(autoMappings)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let sessionFactory = aConfig.BuildSessionFactory()&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let aSession = sessionFactory.OpenSession()&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;aSession.BeginTransaction()&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;//AT&amp;amp;T is Id 100&lt;br /&gt;
let coID = 100&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let someObj = aSession.Load(typeof&amp;lt;StockPrices.COMPANY&amp;gt;, coID) :?&amp;gt; StockPrices.COMPANY&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;printfn "Company Name: %s,  Ticker: %s" someObj.COMPANY_NAME someObj.COMPANY_TICKER&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;aSession.Close()&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let userresp = Console.ReadLine() &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;
&lt;font face="Courier New"&gt;-----------------------------------------------------------------&lt;br /&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;/font&gt;
&lt;p&gt;&lt;font face="Arial"&gt; &lt;/font&gt;&lt;/p&gt;
&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;img src="http://www.caribousoftware.com/BobsBlog/aggbug/50.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://www.caribousoftware.com/BobsBlog/comments/50.aspx</wfw:comment>
        <slash:comments>7</slash:comments>
        <wfw:commentRss>http://www.caribousoftware.com/BobsBlog/comments/commentRss/50.aspx</wfw:commentRss>
        <trackback:ping>http://www.caribousoftware.com/BobsBlog/services/trackbacks/50.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Excuse Me?</title>
        <link rel="alternate" type="text/html" href="http://www.caribousoftware.com/BobsBlog/archive/2009/09/16/excuse-me.aspx" />
        <id>http://www.caribousoftware.com/BobsBlog/archive/2009/09/16/excuse-me.aspx</id>
        <published>2009-09-16T20:00:10Z</published>
        <updated>2009-09-16T21:34:09Z</updated>
        <content type="html">&lt;p&gt;Well, now I have definite evidence that there is no intelligent life on earth.&lt;/p&gt;
&lt;p&gt;Last week an appeals court  threw out Lucent's $358 million award against Microsoft for infringing on a patent allowing MS Outlook users to enter a date by clicking on a calendar.  Go here &lt;font face="Arial"&gt;&lt;a href="http://www.channelinsider.com/c/a/Errata/Microsoft-358-Million-Damage-Award-Overturned-620309"&gt;http://www.channelinsider.com/c/a/Errata/Microsoft-358-Million-Damage-Award-Overturned-620309&lt;/a&gt;?.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Does anyone else think this is galactically stupid?&lt;/p&gt;
&lt;p&gt;I have have just three questions.&lt;/p&gt;
&lt;p&gt;(1)  What idiots on a jury think clicking on a calendar is worth $358 Million?&lt;/p&gt;
&lt;p&gt;(2)  Does Lucent think that because they're such a failure they have to resort to frivolous lawsuits to make money?&lt;/p&gt;
&lt;p&gt;(3)  What on earth is the patent office thinking by granting a patent for clicking?  Next will come addition or multiplication.&lt;/p&gt;
&lt;p&gt;It seems like the only one with any intelligence was the judge!&lt;/p&gt;
&lt;p&gt;Come on you guys.  Fire all your lawyers, hire engineers and get working on the cool stuff.&lt;/p&gt;&lt;img src="http://www.caribousoftware.com/BobsBlog/aggbug/49.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://www.caribousoftware.com/BobsBlog/comments/49.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://www.caribousoftware.com/BobsBlog/comments/commentRss/49.aspx</wfw:commentRss>
        <trackback:ping>http://www.caribousoftware.com/BobsBlog/services/trackbacks/49.aspx</trackback:ping>
    </entry>
    <entry>
        <title>F# - Classes (In progress)</title>
        <link rel="alternate" type="text/html" href="http://www.caribousoftware.com/BobsBlog/archive/2009/09/05/f-classes-in-progress.aspx" />
        <id>http://www.caribousoftware.com/BobsBlog/archive/2009/09/05/f-classes-in-progress.aspx</id>
        <published>2009-09-05T10:07:02Z</published>
        <updated>2009-09-09T10:13:47Z</updated>
        <content type="html">&lt;p&gt;So, today I decided I would try to figure out how classes work in F#.&lt;/p&gt;
&lt;p&gt;I have decided to make a Bank object.  The first thing I tried is this:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;#light&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;type Bank() = class&lt;br /&gt;
    nCustomers : int&lt;br /&gt;
    nEmployees : int&lt;br /&gt;
end&lt;br /&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;This does not work!  I get the error:&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;Program.fs(6,5): error FS0010: Unexpected identifier in member definition.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;Who knows what that means.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;So, I start poking around and find that I need to use the '&lt;strong&gt;val&lt;/strong&gt;' key word to create a public property.  So the next thing I tried is this:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;type Bank() = class&lt;br /&gt;
    val nCustomers : int&lt;br /&gt;
    val nEmployees : int&lt;br /&gt;
end&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;This also does not work!  I get the error:&lt;/font&gt; &lt;font face="Arial" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;Program.fs(6,9): error FS0191: Uninitialized 'val' fields in implicit construction types must be mutable and marked with the '[&amp;lt;DefaultValue&amp;gt;]' attribute. Consider using a 'let' binding instead of a 'val' field&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;After poking around on Mister Softy, I find I need to do it like this:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;type Bank() = class&lt;br /&gt;
    [&amp;lt;DefaultValue&amp;gt;] val mutable nCustomers : int&lt;br /&gt;
    [&amp;lt;DefaultValue&amp;gt;] val mutable private nEmployees : int&lt;br /&gt;
end&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;So this is why:&lt;/p&gt;
&lt;p&gt;(1)  I need the val keyword in conjunction with the mutable keyword in order to make the property public.  That is the default.  Note that my property nEmployees uses the keyword 'private' to make the property private.  The &lt;strong&gt;let&lt;/strong&gt; construct will not work if you want the property to be public.  Let bindings are always private.&lt;/p&gt;
&lt;p&gt;(2)  I really do need both the &lt;strong&gt;val&lt;/strong&gt; and &lt;strong&gt;mutable&lt;/strong&gt; keywords.&lt;/p&gt;
&lt;p&gt;(3)  In this example, I use the default constructor -- these are the open parentheses in the Bank() statement.  Therefore, my properties are initialized.  In F# this is a no no and is not allowed.  When you do not initialize a variable you need to use the Default attribute to initialize the value to Zero, it that is possible.  From Microsoft:&lt;/p&gt;
&lt;p&gt;"&lt;font face="Arial"&gt;The DefaultValue attribute is required on explicit fields in class types that have a primary constructor. This attribute specifies that the field is initialized to zero. The type of the field must support zero-initialization."&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;So, the above statement gives me a class with one public variable (nCustomers) initialized at zero and one private variable, also initialized at zero.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;The following code snippet shows how I instantiate a Bank and assign it a nCustomers:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let BofA = new Bank()&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;//The following does not work&lt;br /&gt;
//let BofA.nCustomers = 4300&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;BofA.nCustomers &amp;lt;- 4300&lt;br /&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;Now what if I do want to use a constructor instead of the default constructor.  I can create a constructor thusly:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;type Bank = class&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    [&amp;lt;DefaultValue&amp;gt;] val mutable nCustomers : int&lt;br /&gt;
    [&amp;lt;DefaultValue&amp;gt;] val mutable private nEmployees : int&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    new(nCust) = {nCustomers = nCust}&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;end&lt;br /&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;In the above snippet, I now create a constructor and use it to assign one of my properties nCustomers.  &lt;/font&gt;&lt;font face="Arial"&gt;The above, however, does &lt;strong&gt;not&lt;/strong&gt; work.  I get this error:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;Program.fs(10,18): error FS0191: Extraneous fields have been given values&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;I guess F# can't deal with both a constructor and a default initialization at the same time.  So, I need to write it as such:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;type Bank = class&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    val mutable nCustomers : int&lt;br /&gt;
    [&amp;lt;DefaultValue&amp;gt;] val mutable private nEmployees : int&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    new(nCust) = {nCustomers = nCust}&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;end&lt;br /&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;I need to take out the attribute &lt;font face="Courier New"&gt;[&amp;lt;DefaultValue&amp;gt;]&lt;/font&gt; if the property is assigned by the constructor.  I guess that makes sense.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;So, now I can instantiate my Bank and then cut the numbers of customers as as such:&lt;/font&gt;&lt;/p&gt;
&lt;font face="Arial"&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let BofA = new Bank(5400)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;BofA.nCustomers &amp;lt;- 4300&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;If I want to initialize both my properties, I would make by constructor as follows: &lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;type Bank = class&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    val mutable nCustomers : int&lt;br /&gt;
    val mutable private nEmployees : int&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    new(nCust, nEmp) = {nCustomers = nCust; nEmployees = nEmp}&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;end&lt;br /&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;You separate your initialization statements with a semi-colon.&lt;/p&gt;
&lt;p&gt;You can also set up a class using an implicit constructor as such:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;type Bank(nCust : int, nEmp : int) = class&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    let mutable _Cust : int = nCust&lt;br /&gt;
    let mutable _Emp : int = nEmp&lt;br /&gt;
    &lt;br /&gt;
    member x.nCustomers with get() = _Cust and set(v) = _Cust &amp;lt;- v&lt;br /&gt;
    member x.nEmployees with get() = _Emp and set(v) = _Emp &amp;lt;- v&lt;br /&gt;
         &lt;br /&gt;
end&lt;br /&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;This format allows you to put the constructor in the class header.  You then set up get and set statements as needed for your class properties.  Note that you don't need both a get and set statement.  If the property is read only, you would use the syntax:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;member x.nCustomers with get() = _Cust&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Or, if you limited the property to write only the syntax would be:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;member x.nCustomers with set(v) = _Cust &amp;lt;- v&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;So what is the "x." doodad for?  Well, I can tell you one thing, if you don't use it you will get an invalid syntax error.&lt;/p&gt;
&lt;p&gt;The x. really is the class "self" identifier, like Me or this.  Microsoft points out that is great because "&lt;font face="Arial"&gt;Unlike in other .NET languages, you can name the self identifier however you want; you are not restricted to names such as self, Me, or this."&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;&lt;strong&gt;Inheritance&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;Inheritance works in F#.  Using an explicit constructor we can create a base class (e.g. Bank) and then have another class, say CitiBank inherit the properties and methods of the Bank class:&lt;/font&gt; &lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;type Bank = class&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    val mutable nCustomers : int&lt;br /&gt;
    val mutable nEmployees : int&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    new(nCust, nEmp) = {nCustomers = nCust; nEmployees = nEmp}&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;end&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;type CitiBank = class&lt;br /&gt;
    &lt;font style="BACKGROUND-COLOR: #ffff00"&gt;inherit Bank&lt;/font&gt; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    val nDeposits : float&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    new(nCust : int, nEmp : int, xDep : float) =&lt;br /&gt;
        {&lt;font style="BACKGROUND-COLOR: #ffff00"&gt;inherit Bank(nCust, nEmp)&lt;/font&gt;;&lt;br /&gt;
         nDeposits = xDep}&lt;br /&gt;
    &lt;br /&gt;
end&lt;br /&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;You just need to use the key word inherit (lower case)  in the class declaration.  We can then add additional properties (e.g. deposits) to our new class.  Note the syntax of the new constructor for the CitiBank class  -- you basically call the constructor for the base class, passing along the arguments from the constructor of the higher level class.&lt;/p&gt;
&lt;p&gt;If you want to use an implicit constructor, this syntax should work:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;type Bank(nCust : int, nEmp : int) = class&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    let mutable _Cust : int = nCust&lt;br /&gt;
    let mutable _Emp : int = nEmp&lt;br /&gt;
    &lt;br /&gt;
    member x.nCustomers with get() = _Cust and set(v) = _Cust &amp;lt;- v&lt;br /&gt;
    member x.nEmployees with get() = _Emp and set(v) = _Emp &amp;lt;- v&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;         &lt;br /&gt;
end&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;type FedBank(nCust : int, nEmp : int, xDep : float) = class&lt;br /&gt;
    &lt;font style="BACKGROUND-COLOR: #ffff00"&gt;inherit Bank(nCust, nEmp)&lt;/font&gt; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    let mutable _Dep : float = xDep&lt;br /&gt;
    member x.nDeposits with get() = _Dep and set(v) = _Dep &amp;lt;- v&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;end&lt;br /&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Again, you call the constructor for the base class in the constructor for the higher level class.&lt;/p&gt;
&lt;font face="Courier New"&gt;
&lt;p&gt;&lt;font face="Arial"&gt;&lt;strong&gt;Adding a Method&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;So, now let's add a method called 'AddDeposit'.  We use the member keyword and then define our function.  The function takes a float and adds it to the existing deposit balance.  The function doesn't return anything.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;type FedBank(nCust : int, nEmp : int, xDep : float) = class&lt;br /&gt;
    inherit Bank(nCust, nEmp) &lt;/p&gt;
&lt;p&gt;    let mutable _Dep : float = xDep&lt;br /&gt;
    member x.nDeposits with get() = _Dep and set(v) = _Dep &amp;lt;- v&lt;/p&gt;
&lt;p&gt;&lt;font style="BACKGROUND-COLOR: #ffff00"&gt;    member x.AddDeposit(aDeposit : float) =&lt;br /&gt;
        _Dep &amp;lt;- _Dep + aDeposit&lt;br /&gt;
        ()  &lt;br /&gt;
&lt;/font&gt;        &lt;br /&gt;
end&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;I need to use the 'mutable' syntax because the deposit balance will be constantly changing.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;We can now see if it works:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;let BofA = new FedBank(5400, 56, 32212.21)&lt;/p&gt;
&lt;p&gt;let dep0 = BofA.nDeposits&lt;/p&gt;
&lt;p&gt;BofA.AddDeposit(5000.0)&lt;/p&gt;
&lt;p&gt;let dep1 = BofA.nDeposits&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;This does work.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;Note that if I had wanted to return the current deposit balance, I would have written my function:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;&lt;font face="Courier New"&gt;    member x.AddDeposit(aDeposit : float) =&lt;br /&gt;
        _Dep &amp;lt;- _Dep + aDeposit&lt;br /&gt;
        _Dep  &lt;br /&gt;
&lt;/font&gt;&lt;br /&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;/font&gt;&lt;/font&gt;&lt;img src="http://www.caribousoftware.com/BobsBlog/aggbug/48.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://www.caribousoftware.com/BobsBlog/comments/48.aspx</wfw:comment>
        <slash:comments>2</slash:comments>
        <wfw:commentRss>http://www.caribousoftware.com/BobsBlog/comments/commentRss/48.aspx</wfw:commentRss>
        <trackback:ping>http://www.caribousoftware.com/BobsBlog/services/trackbacks/48.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Railroad Traffic Data - WE 8/29/2009</title>
        <link rel="alternate" type="text/html" href="http://www.caribousoftware.com/BobsBlog/archive/2009/09/03/railroad-traffic-data-we-8292009.aspx" />
        <id>http://www.caribousoftware.com/BobsBlog/archive/2009/09/03/railroad-traffic-data-we-8292009.aspx</id>
        <published>2009-09-03T11:07:14Z</published>
        <updated>2009-09-03T11:07:14Z</updated>
        <content type="html">&lt;p&gt;Well, I just got a look at the rail traffic data for the week ending 8/29/2009.&lt;/p&gt;
&lt;p&gt;We actually see a fairly sizable increase -- all non-intermodal traffic increased by 2.1% over the previous week:&lt;/p&gt;
&lt;p&gt;&lt;img height="379" width="623" alt="" src="/BobsBlog/images/www_caribousoftware_com/BobsBlog/CarloadsAll82909.png" /&gt;&lt;/p&gt;
&lt;p&gt;Two percent may not sound like a lot, but in the world of railroads it is a fairly big deal.  Total carloads are now 285,580, well off their low of around 250,000 just a few weeks ago.&lt;/p&gt;
&lt;p&gt;Let's look at coal:&lt;/p&gt;
&lt;p&gt;&lt;img height="382" width="624" alt="" src="/BobsBlog/images/www_caribousoftware_com/BobsBlog/Carloads_Coal82909.png" /&gt;&lt;/p&gt;
&lt;p&gt;Coal loads were up just about the same 2.1 percent.  This stands to reason as coal makes up nearly half of all carloads.&lt;/p&gt;
&lt;p&gt;Chemicals also showed a fairly good sized jump after being fairly flat:&lt;/p&gt;
&lt;p&gt;&lt;img height="383" width="627" alt="" src="/BobsBlog/images/www_caribousoftware_com/BobsBlog/Carloads_Chem82909.png" /&gt;&lt;/p&gt;
&lt;p&gt;The week-over-week increase for chemicals was 6.25 percent.  Carloads reached their highest level in nearly a year.&lt;/p&gt;
&lt;p&gt;Intermodal traffic also showed a fairly good jump, after being virtually flat.  Intermodal traffic increased by 4.84 percent -- again a sizable increase for just one week.&lt;/p&gt;
&lt;p&gt;&lt;img height="381" width="629" alt="" src="/BobsBlog/images/www_caribousoftware_com/BobsBlog/Carloads_IM82909.png" /&gt;&lt;/p&gt;
&lt;p&gt;These are all pretty good numbers.  So, what can we say?  This week's numbers provide further assurance that things are starting to pick up.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;&lt;img src="http://www.caribousoftware.com/BobsBlog/aggbug/47.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://www.caribousoftware.com/BobsBlog/comments/47.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://www.caribousoftware.com/BobsBlog/comments/commentRss/47.aspx</wfw:commentRss>
        <trackback:ping>http://www.caribousoftware.com/BobsBlog/services/trackbacks/47.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Where is the Economy - Railroad Traffic 8/22/09</title>
        <link rel="alternate" type="text/html" href="http://www.caribousoftware.com/BobsBlog/archive/2009/08/28/where-is-the-economy-railroad-traffic-82209.aspx" />
        <id>http://www.caribousoftware.com/BobsBlog/archive/2009/08/28/where-is-the-economy-railroad-traffic-82209.aspx</id>
        <published>2009-08-28T07:22:26Z</published>
        <updated>2009-08-28T07:22:26Z</updated>
        <content type="html">&lt;p&gt;Here is the chart for railroad traffic for the week ended 8/22/09&lt;/p&gt;
&lt;p&gt;&lt;img height="381" width="623" alt="" src="/BobsBlog/images/www_caribousoftware_com/BobsBlog/CarloadsAll82209.png" /&gt;&lt;/p&gt;
&lt;p&gt;The data are for all non-intermodal rail carloads.  I have been following this statistic as an indicator of the state of the economy.  It appears that the trend started about 5 or 6 weeks ago of increasing rail traffic is holding to form.  Total non-intermodal traffic stands at about 280,000 carloads versus 250,000 carloads (an increase of about 12 percent).  While I was concerned a few weeks ago that the uptick was a blip, I am more confident now that we are seeing is the real deal.  I should note that traffic this time last year was on order of 325,000 carloads, so we are far from being out of the woods.&lt;/p&gt;
&lt;p&gt;Intermodal carloads are shown below:&lt;/p&gt;
&lt;p&gt;&lt;img height="385" width="628" alt="" src="/BobsBlog/images/www_caribousoftware_com/BobsBlog/Carloads_IM82209.png" /&gt;&lt;/p&gt;
&lt;p&gt;The pattern here is not as positive.  Though there has been an uptick in the past few weeks, the most recent observations are flat.&lt;/p&gt;
&lt;p&gt;The biggest user of the railroads is the coal industry.  The recent trend in coal shipments is shown below:&lt;/p&gt;
&lt;p&gt;&lt;img height="380" width="626" alt="" src="/BobsBlog/images/www_caribousoftware_com/BobsBlog/Carloads_Coal82209.png" /&gt;&lt;/p&gt;
&lt;p&gt;The trend is positive, but still slow.  Like a big oil tanker, the US economy is large, unwieldy, and hard to turn.  But, methinks, once the momentum has turned the progress will be tough to reverse.&lt;/p&gt;
&lt;p&gt;Cash for clunkers?&lt;/p&gt;
&lt;p&gt;The following chart show carloads for motor vehicles and equipment.&lt;/p&gt;
&lt;p&gt;&lt;img height="384" width="628" alt="" src="/BobsBlog/images/www_caribousoftware_com/BobsBlog/CarloadsMotV82209.png" /&gt;&lt;/p&gt;
&lt;p&gt;The recent trend does seem positive, but I don't know if that is a normal seasonal pattern or some offshoot of the cash for clunkers program.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;&lt;img src="http://www.caribousoftware.com/BobsBlog/aggbug/46.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://www.caribousoftware.com/BobsBlog/comments/46.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://www.caribousoftware.com/BobsBlog/comments/commentRss/46.aspx</wfw:commentRss>
        <trackback:ping>http://www.caribousoftware.com/BobsBlog/services/trackbacks/46.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Sugar Shortage #2</title>
        <link rel="alternate" type="text/html" href="http://www.caribousoftware.com/BobsBlog/archive/2009/08/23/sugar-shortage-2.aspx" />
        <id>http://www.caribousoftware.com/BobsBlog/archive/2009/08/23/sugar-shortage-2.aspx</id>
        <published>2009-08-23T10:28:48Z</published>
        <updated>2009-08-24T17:39:50Z</updated>
        <content type="html">&lt;p&gt;In my last post, I complained about the high cost of sugar quotas to US consumers, measuring about &lt;strong&gt;$1.8 billion&lt;/strong&gt; a year.  But, what does this have to do with a sugar shortage?  In the last few months, the world sugar market has changed dramatically.  Although historically the world price of sugar has been far below the US price, the gap has closed considerably.  Below, I show the same price chart from my prior post:&lt;/p&gt;
&lt;p&gt;&lt;img height="480" alt="" width="642" src="/BobsBlog/images/www_caribousoftware_com/BobsBlog/Sugar_Prices.png" /&gt;&lt;/p&gt;
&lt;p&gt;Currently, the world price is 21.84 cents/pound and the US price is 26.45 cents/pound.  The world price is actually above the target market price set by farm legislation and implemented by the USDA.  Ironically, the cost of the subsidy paid to sugar producers is dramatically lower in the current year.  The high world price indicates that the supply/demand relationship in the world market has shifted such that its is now a sellers market -- there is not enough sugar on the world market to supply consuming countries.&lt;/p&gt;
&lt;p&gt;According to data from the USDA, world production of sugar declined from 166.5 million tons (metric) in the 2007/8 fiscal year to 148.7 million tons (metric) in fiscal 2008/9, a decline of roughly 10 percent.  This decline has been matched by an increase in world consumption by about 1.5 percent.  What was a market that had bountiful supplies in 2007/8 now has a very tight supply/demand situation.&lt;/p&gt;
&lt;p&gt;So, where is sugar produced:&lt;/p&gt;
&lt;p&gt;&lt;img height="755" alt="" width="496" src="/BobsBlog/images/www_caribousoftware_com/BobsBlog/Sug1.png" /&gt;&lt;/p&gt;
&lt;p&gt;The above chart shows sugar production for the 2008/9 fiscal year.  Brazil is by far the largest produce of sugar (the above is for sugar alone, exclusive of cane used for ethanol).  India is the second largest producer, followed closely by China.  It also turns out that India is also the biggest consumer of sugar (in total) ahead of both China and the United States.  &lt;/p&gt;
&lt;p&gt;Now the above charts shows absolute sugar production in 2008/9.  The following shows the year-to-year change between 2007/8 and 2008/9.&lt;/p&gt;
&lt;p&gt; &lt;img height="754" alt="" width="623" src="/BobsBlog/images/www_caribousoftware_com/BobsBlog/SugarProdChange.png" /&gt;&lt;/p&gt;
&lt;p&gt;It turns out that the bulk in the world decline in sugar production comes from a really bad crop in India.  Of the global reduction in sugar production of 17.7 million tons (metric), India accounts for 11.85 million tons (tons).  In fact India has switched from being a net exporter (5.8 million tons in 2007/8) to being a net importer of sugar.  The ramifications of this reduction are apparent in the world sugar market -- prices have risen dramatically.&lt;/p&gt;
&lt;p&gt;How does this affect the United States?&lt;/p&gt;
&lt;p&gt;In 2007/08, Total US Sugar Supply (exclusive of existing stocks) was 10.772 million tons.  Of this amount 76% was supplied domestically, 13% from Tariff Rate Quota (TRQ) imports,  6% from Mexico and 5% from other sources.  Tariff Rate Quota (TRQ) imports are those limited by Farm Legislation and implemented by the USDA.  These imports amounted to 1.354 million tons -- basically the amount of the limit.&lt;/p&gt;
&lt;p&gt;&lt;img height="403" alt="" width="411" src="/BobsBlog/images/www_caribousoftware_com/BobsBlog/SugProd2007_8(1).png" /&gt; &lt;/p&gt;
&lt;p&gt;In 2008/09, US sugar producers had a bad year and their production declined by 7% -- about 0.581 million tons.  So, right from the get go, the supply/demand situation would be tight anyway.  Total supply was about the same in 2008/09 -- 10.762 million tons.  So what made up the shortfall?&lt;/p&gt;
&lt;p&gt;&lt;img height="405" alt="" width="414" src="/BobsBlog/images/www_caribousoftware_com/BobsBlog/SugProd2008_9.png" /&gt;&lt;/p&gt;
&lt;p&gt;In 2008/09 TRQ imports were basically flat.  Mexico basically drastically increased there imports into the US -- growing from 0.694 million tons to 1.450 million tons -- more than double.  Mexican sugar is not subject to an import quota and can imported under NAFTA duty free.  Methinks that if the red state farmers had had their way in limiting Mexican sugar imports, the pricing situation for sugar would be much worse.  The result of the Mexican imports is that the price of sugar in the US has remained fairly flat throughout the year, even though world sugar prices have escalated substantially.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Crunch Time&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Right now, the markets are intensely focused on the 2009/10 supply in the US.  Domestic production is expected to rise somewhat, basically back to 2007/08 levels.  But, overall supply is expected to fall by 0.730 million tons, or about 6.8 percent.  How can that be?&lt;/p&gt;
&lt;p&gt;&lt;img height="408" alt="" width="414" src="/BobsBlog/images/www_caribousoftware_com/BobsBlog/SugProd2009_10.png" /&gt;&lt;/p&gt;
&lt;p&gt;The first problem is that TRQ imports are expected to fall by about 0.250 million tons.  How can that be.  The US limits sugar imports doesn't it?  Well the US does limit imports.  But it does so on a country-by-country basis.  So, if a country like &lt;font face="Arial"&gt;Zimbabwe does not meet its allowable quota of 12,636 metric tons, the quota 'shortfall' just goes into space and is unused.  Thus, for example, Brazil has net its quota by the end of June (quotas are set on a fiscal year -- ending September 30th -- basis), it cannot import anymore into the US.  The country-by-country limits always mean that there will be an aggregate shortfall.  That is, counties that have extra sugar to export can't make up up for those with a short.fall.  The result -- depending on the year TRQ imports are always less than what is allowed in aggregate.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;So, while the April, 2009 projection of sugar imports for 2009/10 was set at 1.496 million tons, it has been systematically been reduced to 1.182 million tons (a decrease of about 20%) reflecting not a tighter quota limit, but a greater shortfall.  Why?  Two reasons come to mind.  (1) Bad weather &amp;amp; bad harvests or (2) a willingness on producers to sell their goods elsewhere.  I suspect it it the latter.  Because the world price is basically the same as the US price (as faced by an exporter), why would anyone prefer to sell to the US when they can get the same price from someone else?  I mean put yourself in the shoes of some guy in (pick a country) who has been tormented by quotas &amp;amp; paperwork for years.  Why would he (or she) voluntarily choose to trade with the US?&lt;/p&gt;
&lt;p&gt;Whatever the the rationale, TRQ imports are expected to decrease by  0.250 million tons.&lt;/p&gt;
&lt;p&gt;What about Mexico?  Mexico is interesting because the country is now producing less sugar than it consumes.  So, how did it significantly raise its imports into the US in 2008/09.  Answer:  It all came out of existing stocks.  Mexico has basically sold its entire preexisting (as of the start of 2008/09) stock of sugar to the US.  I'm sure they did this because they got a good price.  But, the fact of the matter is the cookie jar is empty.  Imports from Mexico into the US are expected to fall by &lt;strong&gt;1.285 million tons&lt;/strong&gt;.  Folks, that is a lot of sugar.  Mexico will consume what it produces, but there is nothing extra to export.&lt;/p&gt;
&lt;p&gt;The USDA also expects that part of the 2009/10 sugar shortfall will be filled by a draw down in US stocks from 1.252 million tons to 0.709 million tons for an extra 0.543 million tons.  &lt;/p&gt;
&lt;p&gt;These factors all combine to make for a very tight market in the forthcoming fiscal year.  This, then, is the focus of the food companies in the letter to the Secretary of Agriculture requesting an increase in the TRQ country limits (see my prior post).&lt;/p&gt;
&lt;p&gt;The current US supply/demand balance now reflects the world's supply/demand balance.  Currently, the TRQ import quotas are basically non binding and the US must compete for sugar on the world market.  If this is in fact the case, it is not immediately obvious that by raising the TRQ quotas (as requested by the food companies), there will be any additional sugar provided into the US market.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;A digression on the TRQ program&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The TRQ program limits the sugar imports by allocating a quota to 40 countries.  The actual quota assigned to a country is based on the total allowable quota and a percentage share assigned to the country.  The percentage shares are based on trade data from 1975-81 when the sugar trade was relatively unrestricted.&lt;/p&gt;
&lt;p&gt;The chart below shows the 2008/09 TRQ limits for each country compared to their actual imports through July, 2009.&lt;/p&gt;
&lt;p&gt;&lt;img height="962" alt="" width="494" src="/BobsBlog/images/www_caribousoftware_com/BobsBlog/TRQ0.png" /&gt;&lt;/p&gt;
&lt;p&gt;The chart above shows that most of the big producers/importers are nearly maxed out on the quota, with the exception of Argentina which still has a lot of quota room left.  There are also a number of smaller producers that have not imported any sugar into the US this year.  Overall, after 10 months of the fiscal year, total TRQ imports are running at 71% of quota.  By comparison, by the same point in time last fiscal year, imports were running 74% of quota.&lt;/p&gt;
&lt;p&gt;Given the above distribution of imports, it appears that for many countries raising the import quotas would have no affect on the sugar supplied to the US market -- many countries won't hit the current quotas.  On the other hand, for the big producers like Brazil, The Philippines, Australia, and The Dominican Republic, raising their quotas may indeed yield greater imports because they are likely to max out under their current quotas.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;&lt;img src="http://www.caribousoftware.com/BobsBlog/aggbug/45.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://www.caribousoftware.com/BobsBlog/comments/45.aspx</wfw:comment>
        <slash:comments>1</slash:comments>
        <wfw:commentRss>http://www.caribousoftware.com/BobsBlog/comments/commentRss/45.aspx</wfw:commentRss>
        <trackback:ping>http://www.caribousoftware.com/BobsBlog/services/trackbacks/45.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Sugar Quotas #1</title>
        <link rel="alternate" type="text/html" href="http://www.caribousoftware.com/BobsBlog/archive/2009/08/21/tariffs-on-sugar.aspx" />
        <id>http://www.caribousoftware.com/BobsBlog/archive/2009/08/21/tariffs-on-sugar.aspx</id>
        <published>2009-08-21T18:10:08Z</published>
        <updated>2009-08-23T10:27:22Z</updated>
        <content type="html">&lt;p&gt;I came across a story in the WSJ last week (WSJ August 13th) in which some major food companies were warning of a sugar shortage in the US.  Excuse me.  A sugar shortage?  I'm not making this up.  Here's the text of the letter:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New" /&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;August 5, 2009&lt;br /&gt;
The Honorable Thomas J. Vilsack&lt;br /&gt;
Secretary&lt;br /&gt;
U.S. Department of Agriculture&lt;br /&gt;
Jamie L. Whitten Federal Building&lt;br /&gt;
1400 Independence Avenue, S.W.&lt;br /&gt;
Washington, DC 20250&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;Dear Mr. Secretary:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;The organizations and companies below urge you to increase the sugar import quota&lt;br /&gt;
immediately. Your experts forecast unprecedented shortages without prompt action.&lt;br /&gt;
According to USDA's World Agricultural Supply and Demand Estimates, the United&lt;br /&gt;
States will end the next fiscal year with less than 13 days' worth of sugar on hand, unless&lt;br /&gt;
imports are increased. If this forecast is accurate, our nation will virtually run out of sugar.&lt;/p&gt;
&lt;p&gt;The shortage does not have to happen. The only reason markets are forecast to be&lt;br /&gt;
so tight is the restrictive U.S. policy on sugar imports. Imports are subject to restrictive&lt;br /&gt;
quotas.&lt;/p&gt;
&lt;p&gt;But you have the authority to increase the sugar import quota, and we urge you to do&lt;br /&gt;
so immediately, both for the current fiscal year - where high prices already indicate a&lt;br /&gt;
painfully tight market - and for the upcoming year.&lt;/p&gt;
&lt;p&gt;Without a quota increase, consumers will pay higher prices, food manufacturing jobs&lt;br /&gt;
will be at risk and trading patterns will be distorted. Please act now in the interest of all&lt;br /&gt;
Americans.&lt;/p&gt;
&lt;p&gt;Sincerely,&lt;/p&gt;
&lt;p&gt;American Bakers Association&lt;br /&gt;
American Beverage Association&lt;br /&gt;
Blommer Chocolate&lt;br /&gt;
Competitive Enterprise Institute&lt;br /&gt;
ConAgra Foods, Inc.&lt;br /&gt;
Consumer Federation of America&lt;br /&gt;
Council for Citizens Against Government Waste&lt;br /&gt;
Emergency Committee for American Trade&lt;br /&gt;
General Mills, Inc.&lt;br /&gt;
Gonella Frozen Products&lt;br /&gt;
Grocery Manufacturers Association&lt;br /&gt;
The Hershey Company&lt;br /&gt;
Independent Bakers Association&lt;br /&gt;
International Dairy Foods Association&lt;br /&gt;
Kraft Foods&lt;br /&gt;
Krispy Kreme Doughnut Corp.&lt;br /&gt;
Mars, Incorporated&lt;br /&gt;
National Confectioners Association&lt;br /&gt;
Nestle USA&lt;br /&gt;
Pepperidge Farm&lt;br /&gt;
Sweetener Users Association&lt;br /&gt;
Unilever United States, Inc.&lt;br /&gt;
U.S. Chamber of Commerce&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;font face="Arial"&gt;I say to myself this is bizarre.  So, I've spent the last fews days trying to figure out what is going on.  &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;In my sleuthing around, I find out that the U.S. uses price supports, domestic marketing allotments, and tariff-rate quotas (TRQs) to influence the amount of sugar available to the U.S. market. The program is used to support U.S. sugar prices above comparable levels in the world market.  In order to keep the program off the Federal Budget,  the program relies on import quotas to limit imports, so that a certain target price can be maintained.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;The rates for raw cane sugar are: &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;18 cents per pound in FY 2009, &lt;br /&gt;
18.25 cents per pound in FY 2010, &lt;br /&gt;
18.50 cents per pound in FY 2011, and &lt;br /&gt;
18.75 cents per pound in FY 2012-13. &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;The rates for refined beet sugar are: &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;22.9 cents per pound in FY 2009 and &lt;br /&gt;
128.5 percent of the loan rate for raw cane sugar in FY 2010-13. &lt;br /&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;So, I ask myself, what is the world price?  In turns out that over the last 15 or so years, the average difference between the world price and the domestic price is about ten cents a pound:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;img height="480" alt="" width="642" src="/BobsBlog/images/www_caribousoftware_com/BobsBlog/Sugar_Prices.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;I mean this is about double the world price.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;If we look at US Sugar Consumption over the same time period:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;img height="483" alt="" width="646" src="/BobsBlog/images/www_caribousoftware_com/BobsBlog/SugarConsumption.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;It turns out that the US consumes about 900,000 tons a month -- that is a lot of donuts and cokes.  You figure in a year that is about 10 million tons.  If you figure the US population is 300 million, that means people eat about  67 pounds of sugar a year.  Yikes.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;So, how much is the subsidy we pay to sugar farmers.  I looked at the price difference per month and multiplied it by the monthly US production of sugar:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;img height="432" alt="" width="643" src="/BobsBlog/images/www_caribousoftware_com/BobsBlog/SugarSubsidy.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;So, over the last 15 or so years, we as consumers pay sugar producers a subsidy of about &lt;strong&gt;$150 Million&lt;/strong&gt; per month or about &lt;strong&gt;$1.8 Billion&lt;/strong&gt; a year.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;So who gets all this dough.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;Let's look at who grows sugar cane and sugar beets:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Farm Count, by State:&lt;/strong&gt;&lt;/p&gt;
&lt;table cellspacing="1" cellpadding="1" width="400" summary="" border="1"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;State&lt;/td&gt;
            &lt;td&gt;
            &lt;p&gt;Sugar Beets&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;Sugar Cane&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Caifornia&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;155&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Colorado&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;226&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Delaware&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;3&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Florida&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;108&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Georgia&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;35&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Hawaii&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;9&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Idaho&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;507&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Louisiana&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;461&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Michigan&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;737&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Minnesota&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;1247&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Montana&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;220&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Nebraska&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;162&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;North Dakota&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;553&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Oregon&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;73&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Texas&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;114&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Washington&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;3&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Wyoming&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;139&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Total&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;4022&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;692&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;Sugar Production ('000 Tons), by State:&lt;/strong&gt;&lt;/p&gt;
&lt;table cellspacing="1" cellpadding="1" width="400" summary="" border="1"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;State&lt;/td&gt;
            &lt;td&gt;
            &lt;p&gt;Sugar Beets&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;Sugar Cane&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Caifornia&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;231&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Colorado&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;115&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Delaware&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;3&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Florida&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;1,800&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Georgia&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;*&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Idaho&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;845&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Hawaii&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;200&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Louisiana&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;1,400&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Michigan&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;530&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Minnesota&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;1,715&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Montana&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;176&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Nebraska&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;160&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;North Dakota&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;854&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Oregon&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;52&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Texas&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;152&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Washington&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;12&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Wyoming&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;101&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Total&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;4,791&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;3,552&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;font face="Arial"&gt;Now let's look at the implied sugar subsidy by multiplying the tons by $0.10/Lb.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sugar Subsidy ($million), by State:&lt;/strong&gt;&lt;/p&gt;
&lt;table cellspacing="1" cellpadding="1" width="400" summary="" border="1"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;State&lt;/td&gt;
            &lt;td&gt;
            &lt;p&gt;Sugar Beets&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;Sugar Cane&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Caifornia&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;46.2&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Colorado&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;22.9&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Delaware&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;*&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Florida&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;360.0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Georgia&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;*&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Idaho&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;168.9&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Hawaii&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;40.0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Louisiana&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;280.0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Michigan&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;106.0&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Minnesota&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;343.0&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Montana&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;35.2&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Nebraska&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;31.9&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;North Dakota&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;170.9&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Oregon&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;10.4&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Texas&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;30.4&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Washington&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;2.4&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Wyoming&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;20.3&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Total&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;$958.1&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;$710.4&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;font face="Arial"&gt;If we put this on a 'Per Farm' basis, we can estimate how much subsidy goes to each farm, on average, by state:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sugar Subsidy ($thound) per Farm, by State:&lt;/strong&gt;&lt;/p&gt;
&lt;table cellspacing="1" cellpadding="1" width="400" summary="" border="1"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;State&lt;/td&gt;
            &lt;td&gt;
            &lt;p&gt;Sugar Beets&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;Sugar Cane&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Caifornia&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;297.9&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Colorado&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;101.5&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Delaware&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;*&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Florida&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;3,333.3&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Georgia&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;*&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Idaho&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;333.2&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Hawaii&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;4,444.4&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Louisiana&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;607.4&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Michigan&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;143.8&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Minnesota&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;275.0&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Montana&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;160.1&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Nebraska&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;197.1&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;North Dakota&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;309.0&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Oregon&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;142.3&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Texas&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;266.7&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Washington&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;802.1&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Wyoming&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;145.8&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;0&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Average&lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;238.2&lt;/p&gt;
            &lt;/td&gt;
            &lt;td&gt;
            &lt;p align="right"&gt;1026.6&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;font face="Arial"&gt;Good grief.  When all is said and done we are paying sugar beet farmers an average of $238,200/year to grow their crop.  We are paying sugar cane farmers average of $1,026,600/year to grow their crop.  That's &lt;strong&gt;one million&lt;/strong&gt; bucks a year folks. That's real money.  We're talking a serious case of Welfare for Farmers.  I need to get in on this racket.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;How did I get off on this tangent anyway?  I was taliking about a sugar shortage not farm welfare.  This post is already too long, that will just have to in the next one.&lt;/font&gt;&lt;/p&gt;
&lt;hr /&gt;&lt;img src="http://www.caribousoftware.com/BobsBlog/aggbug/44.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://www.caribousoftware.com/BobsBlog/comments/44.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://www.caribousoftware.com/BobsBlog/comments/commentRss/44.aspx</wfw:commentRss>
        <trackback:ping>http://www.caribousoftware.com/BobsBlog/services/trackbacks/44.aspx</trackback:ping>
    </entry>
    <entry>
        <title>F# Serial Correlation (2)</title>
        <link rel="alternate" type="text/html" href="http://www.caribousoftware.com/BobsBlog/archive/2009/08/14/f-serial-correlation-2-in-progress.aspx" />
        <id>http://www.caribousoftware.com/BobsBlog/archive/2009/08/14/f-serial-correlation-2-in-progress.aspx</id>
        <published>2009-08-14T17:34:56Z</published>
        <updated>2009-08-25T15:15:44Z</updated>
        <content type="html">&lt;p&gt;In my last post I set up an example dataset and then showed how use the Durbin-Watson statistic to test for serial correlation.  Once we have recognized that serial correlation exists, we need to make the appropriate adjustments because OLS is no longer the best estimation technique.&lt;/p&gt;
&lt;p&gt;We need to rely on GLS to perform our estimation.  The key to this is to identify what the matrix Omega (Z) looks like.  In the case where we have first order autocorrelation AR(1), the Omega (Z) matrix looks like:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;[1    p    p&lt;sup&gt;2&lt;/sup&gt;    p&lt;sup&gt;3&lt;/sup&gt;  ...  p&lt;sup&gt;n-1&lt;/sup&gt;]&lt;br /&gt;
|p    1    p     p&lt;sup&gt;2&lt;/sup&gt;  ...  &lt;sup&gt;pn-2&lt;/sup&gt;|&lt;br /&gt;
|p&lt;sup&gt;2&lt;/sup&gt;   p    1     p   ...  p&lt;sup&gt;n-3&lt;/sup&gt;|&lt;br /&gt;
|..   ..   ..    ..  ...  &lt;sup&gt;...&lt;/sup&gt; |&lt;br /&gt;
[p&lt;sup&gt;n-1&lt;/sup&gt; p&lt;sup&gt;n-2&lt;/sup&gt; p&lt;sup&gt;n-3&lt;/sup&gt;  p&lt;sup&gt;n-4&lt;/sup&gt;  ...  1  ]&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;where the p is first order auto-correlation coefficient -- meaning the correlation between sequential error terms is p (rho).  I would use Greek letters but I cant.)&lt;/p&gt;
&lt;p&gt;So, we can easily apply GLS, we just need to figure out what p (rho) is.&lt;/p&gt;
&lt;p&gt;One way of estimating p (rho) is to use the Durban-Watson statistic.  It turns out that the DW Statistic is approximately equal to:&lt;/p&gt;
&lt;p&gt;2 * (1 - p).  In our regression, we computed a DW statistic of 1.09, which would imply a p (rho) of 0.455.&lt;/p&gt;
&lt;p&gt;But, there is another, more interesting method, called the Hildreth-Lu Procedure.&lt;/p&gt;
&lt;p&gt;This approach uses a grid search to estimate the most 'likely' value for p (rho).  In practice this might involve running 100 regressions, each using a different value of p (rho), such as 0.01, 0.02, 0.03, ...0.99.   We would then select the regression that yielded the lowest sum of squared errors.  Once we have narrowed down the range, we could run another 100 regressions to further refine our estimate.  What a perfect task for F# where we can send off multiple regressions at once and make full use of our dual core or multi-core computers.&lt;/p&gt;
&lt;p&gt;We first transform our original data to look like this:&lt;/p&gt;
&lt;p&gt;Y* = X* + v&lt;/p&gt;
&lt;p&gt;Where&lt;/p&gt;
&lt;p&gt;Y* = Y&lt;sub&gt;t&lt;/sub&gt;  - pY&lt;sub&gt;t-1&lt;/sub&gt;&lt;/p&gt;
&lt;p&gt;Each entry in X*: X&lt;sub&gt;kt&lt;/sub&gt; = X&lt;sub&gt;kt&lt;/sub&gt; - pX&lt;sub&gt;kt-1&lt;/sub&gt;&lt;/p&gt;
&lt;p&gt;v = e&lt;sub&gt;t&lt;/sub&gt;  - pe&lt;sub&gt;t-1&lt;/sub&gt;&lt;/p&gt;
&lt;p&gt;We then run a regular OLS regression and calculate the sum of squared errors.  What we need is a function that will take a value of p (rho), do the data transformation and then run the OLS regression.  The function would then return of the sum of squared errors.&lt;/p&gt;
&lt;p&gt;Here's the code:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;type rssType = {rho : float; rss : float}&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let HL_iteration (X0 : float [,] ) (y : float [,] ) (p : float) =&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    let n1 = n-1&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    let X2 = Array2D.zeroCreate&amp;lt;float&amp;gt; n1 k&lt;br /&gt;
    let y2 = Array2D.zeroCreate&amp;lt;float&amp;gt; n1 1&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    for i = 0 to (n1-1) do&lt;br /&gt;
        y2.[i, 0] &amp;lt;- y.[i+1, 0] - p * y.[i, 0] &lt;br /&gt;
        X2.[i, 0] &amp;lt;- 1.0 - p&lt;br /&gt;
        X2.[i, 1] &amp;lt;- X0.[i+1, 1] - p * X0.[i, 1]&lt;br /&gt;
        X2.[i, 2] &amp;lt;- X0.[i+1, 2] - p * X0.[i, 2]&lt;br /&gt;
        X2.[i, 3] &amp;lt;- X0.[i+1, 3] - p * X0.[i, 3]&lt;br /&gt;
        X2.[i, 4] &amp;lt;- X0.[i+1, 4] - p * X0.[i, 4]&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    let ret, RSS = regress X2 y2&lt;br /&gt;
    let retVal = {rho=p; rss=RSS}&lt;br /&gt;
    retVal&lt;br /&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;I first create a type rssType that can hold both rho and the sum of squared errors.  The function takes our X and y matrices as input, as well a value for p (rho).  We then get the count of observations and reduce it by one because we lose the first observation in the process (the first observation has no lagged value.  We then create two arrays,  X2 and y2 that will hold our transformed data.  We loop over the original data and do the transformation element-by-element.  Once we have our transformed data, we pass it to our regular OLS regression method.&lt;/p&gt;
&lt;p&gt;We return the values of p (rho) and the sum residual sum of squares (sum or squared errors).  The idea here is that we can call this function multiple times for different p's (rho's) and get back the sum of squares.  We can then pick the rho with the smallest sum of squares.&lt;/p&gt;
&lt;p&gt;This is how we do that:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let HL_iterate (X : float [,] ) (y : float [,] ) (i0 : float) (i1 : float) =&lt;br /&gt;
    &lt;br /&gt;
    let incr = (i1 - i0) / 10.0&lt;br /&gt;
    let rssList = new List&amp;lt;rssType&amp;gt;()&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    let asyncList = new List&amp;lt;Async&amp;lt;rssType&amp;gt;&amp;gt;()&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    for i = 0 to 9 do&lt;br /&gt;
        let (iret : Async&amp;lt;rssType&amp;gt;) = async {return HL_iteration X y ((i0 + ((float) i * incr)))}&lt;br /&gt;
        asyncList.Add iret&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    let Result =&lt;br /&gt;
        async { let! asyncWorkflow = Async.Parallel asyncList&lt;br /&gt;
                return asyncWorkflow }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    let rssList = Async.RunSynchronously Result&lt;br /&gt;
        &lt;br /&gt;
    for aRSS : rssType in rssList do&lt;br /&gt;
        printfn "RSS, rho: %10.5f %10.6E " aRSS.rss aRSS.rho&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    rssList&lt;br /&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;This function takes our original X and y matrices,  and two end points &lt;font face="Courier New"&gt;i0&lt;/font&gt; and &lt;font face="Courier New"&gt;i1&lt;/font&gt;.  The plan here is to take the two end points for rho, say 0.01 and 0.99, and split them into 10 equally spaced values for rho.  We then run a regression for each of the ten values and obtain the results.  By narrowing then end points we can fine tune our grid search to obtain a more precise estimate of rho.  The variable &lt;font face="Courier New"&gt;incr&lt;/font&gt; is just the range &lt;font face="Courier New"&gt;i1 - i0&lt;/font&gt; divided by 10.  I then create a list &lt;font face="Courier New"&gt;rssList&lt;/font&gt; of type &lt;font face="Courier New"&gt;rssType&lt;/font&gt; to hold my results.&lt;/p&gt;
&lt;p&gt;Now, I want to send off all my ten regressions at once, and take advantage of F#'s handling of multi-threaded operations.  So, I set up a list of function calls I want to run asynchronously.  These will be held in my variable &lt;font face="Courier New"&gt;asyncList&lt;/font&gt;.  I next create a loop with 10 iterations than creates the function call for each of my spaced out values of rho.   The value of rho: &lt;font face="Courier New"&gt;(i0 + ((float) i * incr)&lt;/font&gt; is just the starting point plus i times the value of the i of the increment variable.  I add each function call to my &lt;font face="Courier New"&gt;asyncList&lt;/font&gt;.&lt;/p&gt;
&lt;p&gt;I then set up the workflow to process the function calls in parallel and only return when they are all complete (this is the magic of the let! statement).&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    let Result =&lt;br /&gt;
        async { let! asyncWorkflow = Async.Parallel asyncList&lt;br /&gt;
                return asyncWorkflow }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;I return the values in the variable &lt;font face="Courier New"&gt;asyncWorkflow&lt;/font&gt; which is a list of rssType values.  Note:  this statement does not actually run the functions (regressions).  You actually need the following statement:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    let rssList = Async.RunSynchronously Result&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;to run the regressions and fill up the rssList of results.&lt;/p&gt;
&lt;p&gt;I then create a main function to call my HL_iterate function:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let rssList = HL_iterate X y 0.01 0.99&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;for aRSS in rssList do&lt;br /&gt;
    printfn "RSS, rho: %10.6E %10.5f  " aRSS.rss aRSS.rho &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let rssArr = Array.sortWith (fun (x : rssType) (y : rssType) -&amp;gt; if y.rss = x.rss then 0 elif y.rss &amp;lt; x.rss then 1 else -1 ) rssList&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;printfn "Lowest RSS, rho: %10.6E %10.5f  "  (rssArr.[0].rss) (rssArr.[0].rho)&lt;br /&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;---------------------------------------------------------------------------&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;The variable rssList has the results from the ten regressions.  I then print them out.  My next step is to find the regression with the lowest sum of squares.  I do this by invoking the Array method sortWith.  This method allows me to sort an array using an anonymous function that I provide.&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;(fun (x : rssType) (y : rssType) -&amp;gt; if y.rss = x.rss then 0 elif y.rss &amp;lt; x.rss then 1 else -1 )&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;My function takes the value of rss (one part of the rssType structure) and compares it to the next value.   My comparison uses the '&amp;lt;' operator so that I can get a resulting array that is sorted in ascending order.  Thus, it is easy to obtain the regression with the lowest sum of squares -- it ends up at the head of the list.&lt;/p&gt;
&lt;p&gt;So, let's run it and see what we get:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;RSS, rho: 5.018932E+009    0.01000  &lt;br /&gt;
RSS, rho: 4.636517E+009    0.10800  &lt;br /&gt;
RSS, rho: 4.348996E+009    0.20600  &lt;br /&gt;
RSS, rho: 4.156232E+009    0.30400  &lt;br /&gt;
RSS, rho: 4.058028E+009    0.40200  &lt;br /&gt;
RSS, rho: 4.054117E+009    0.50000  &lt;br /&gt;
RSS, rho: 4.144200E+009    0.59800  &lt;br /&gt;
RSS, rho: 4.328156E+009    0.69600  &lt;br /&gt;
RSS, rho: 4.606552E+009    0.79400  &lt;br /&gt;
RSS, rho: 4.981314E+009    0.89200  &lt;br /&gt;
Lowest RSS, rho: 4.054117E+009    0.50000  &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;I just love to see the both processor cores spike!  We see that the lowest rho is in the middle at 0.50.  We can fine tune this by setting our end points to 0.5 +/- 0.1 and calling the HL_iterate function again:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;HL_iterate X y (rssList.[0].rho - 0.1) (rssList.[0].rho+0.1)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let rssList2 = HL_iterate X y (rssArr.[0].rho - 0.1) (rssArr.[0].rho + 0.1)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;for aRSS in rssList2 do&lt;br /&gt;
    printfn "RSS, rho: %10.6E %10.5f  " aRSS.rss aRSS.rho &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let rssArr2 = Array.sortWith (fun (x : rssType) (y : rssType) -&amp;gt; if y.rss = x.rss then 0 elif y.rss &amp;lt; x.rss then 1 else -1 ) rssList2&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;printfn "Lowest RSS, rho: %10.6E %10.5f  "  (rssArr2.[0].rss) (rssArr2.[0].rho)&lt;br /&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;We now get our second round of results&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;RSS, rho: 4.059089E+009    0.40000  &lt;br /&gt;
RSS, rho: 4.050250E+009    0.42000  &lt;br /&gt;
RSS, rho: 4.045336E+009    0.44000  &lt;br /&gt;
RSS, rho: 4.044344E+009    0.46000  &lt;br /&gt;
RSS, rho: 4.047272E+009    0.48000  &lt;br /&gt;
RSS, rho: 4.054117E+009    0.50000  &lt;br /&gt;
RSS, rho: 4.064876E+009    0.52000  &lt;br /&gt;
RSS, rho: 4.079547E+009    0.54000  &lt;br /&gt;
RSS, rho: 4.098129E+009    0.56000  &lt;br /&gt;
RSS, rho: 4.120619E+009    0.58000  &lt;br /&gt;
Lowest RSS, rho: 4.044344E+009    0.46000  &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;We now add a third iteration, where we adjust the endpoints to be 0.46 +/- 0.01:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let rssList3 = HL_iterate X y (rssArr2.[0].rho - 0.01) (rssArr2.[0].rho + 0.01)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;for aRSS in rssList3 do&lt;br /&gt;
    printfn "RSS, rho: %10.6E %10.5f  " aRSS.rss aRSS.rho &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let rssArr3 = Array.sortWith (fun (x : rssType) (y : rssType) -&amp;gt; if y.rss = x.rss then 0 elif y.rss &amp;lt; x.rss then 1 else -1 ) rssList3&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;printfn "Lowest RSS, rho: %10.6E %10.5f  "  (rssArr3.[0].rss) (rssArr3.[0].rho)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;We now get our third round of results:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;RSS, rho: 4.044350E+009    0.45000  &lt;br /&gt;
RSS, rho: 4.044270E+009    0.45200  &lt;br /&gt;
RSS, rho: 4.044230E+009    0.45400  &lt;br /&gt;
RSS, rho: 4.044229E+009    0.45600  &lt;br /&gt;
RSS, rho: 4.044267E+009    0.45800  &lt;br /&gt;
RSS, rho: 4.044344E+009    0.46000  &lt;br /&gt;
RSS, rho: 4.044460E+009    0.46200  &lt;br /&gt;
RSS, rho: 4.044616E+009    0.46400  &lt;br /&gt;
RSS, rho: 4.044811E+009    0.46600  &lt;br /&gt;
RSS, rho: 4.045045E+009    0.46800  &lt;br /&gt;
Lowest RSS, rho: 4.044229E+009    0.45600  &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;So, I think I'll stop here.  My best guess as rho is 0.456.&lt;/p&gt;
&lt;p&gt;Now I'll just build my Z matrix (shown at the top) and apply my GLS estimation method.&lt;/p&gt;
&lt;p&gt;This two-step procedure is commonly referred to as the Prais-Winsten procedure.&lt;/p&gt;
&lt;p&gt;It turns out, given the matrix of the form:&lt;/p&gt;
&lt;font face="Courier New"&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;[1    p    p&lt;sup&gt;2&lt;/sup&gt;    p&lt;sup&gt;3&lt;/sup&gt;  ...  p&lt;sup&gt;n-1&lt;/sup&gt;]&lt;br /&gt;
|p    1    p     p&lt;sup&gt;2&lt;/sup&gt;  ...  &lt;sup&gt;pn-2&lt;/sup&gt;|&lt;br /&gt;
|p&lt;sup&gt;2&lt;/sup&gt;   p    1     p   ...  p&lt;sup&gt;n-3&lt;/sup&gt;|&lt;br /&gt;
|..   ..   ..    ..  ...  &lt;sup&gt;...&lt;/sup&gt; |&lt;br /&gt;
[p&lt;sup&gt;n-1&lt;/sup&gt; p&lt;sup&gt;n-2&lt;/sup&gt; p&lt;sup&gt;n-3&lt;/sup&gt;  p&lt;sup&gt;n-4&lt;/sup&gt;  ...  1  ]&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;that the inverse is easily computed.  The inverse of Z is:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;Z&lt;sup&gt;-1&lt;/sup&gt; = (1 / (1 - p&lt;sup&gt;2&lt;/sup&gt;) * [ 1    -p     0     0     0     0]&lt;br /&gt;
                      |-p  1+p&lt;sup&gt;2&lt;/sup&gt;    -p     0     0     0|&lt;br /&gt;
                      | 0    -p  1+p&lt;sup&gt;2&lt;/sup&gt;    -p     0     0|&lt;br /&gt;
                      | ..   ..    ..    ..    ..    ..|&lt;br /&gt;
                      | 0     0     0    -P  1+p&lt;sup&gt;2&lt;/sup&gt;    -p|&lt;br /&gt;
                      [ 0     0     0     0    -p     1]&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;So for our program all we have to do is create Z&lt;sup&gt;-1&lt;/sup&gt; based on our estimate of p (rho) and then run our GLS regression&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;We now add the following to our program:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;let rho = rssArr3.[0].rho&lt;/p&gt;
&lt;p&gt;//----------------------------------------------------------------------&lt;br /&gt;
// Now Build ZI Matrix&lt;br /&gt;
//----------------------------------------------------------------------&lt;/p&gt;
&lt;p&gt;let dd = (1.0 / (1.0 - Math.Pow(rho,2.0)))&lt;/p&gt;
&lt;p&gt;let ZI = Array2D.init&amp;lt;float&amp;gt; n n (fun i j -&amp;gt; if i=j then dd * (1.0 + Math.Pow(rho,2.0)) elif (i=j+1 or j=i+1) then -1.0*rho*dd else 0.0)&lt;/p&gt;
&lt;p&gt;ZI.[0,0] &amp;lt;- dd * 1.0&lt;br /&gt;
ZI.[n-1,n-1] &amp;lt;- dd * 1.0&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;let ret = regressGLS X y ZI&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;We first grab &lt;font face="Courier New"&gt;rho&lt;/font&gt; estimate from our last Hildreth-Liu iteration.  Recall it should be 0.456.  We then build a ZI matrix, which is Omega Inverse.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;The variable &lt;font face="Courier New"&gt;dd&lt;/font&gt; is just the multiplier out in front of the matrix (= 1 / (1 - p&lt;sup&gt;2&lt;/sup&gt;)).&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;&lt;font face="Courier New"&gt;ZI&lt;/font&gt; is an n x n matrix where the diagonal elements are 1 + p&lt;sup&gt;2&lt;/sup&gt; and the adjacent entries are merely &lt;font face="Courier New"&gt;-p&lt;/font&gt;.  The diagonal corners are set to 1.0.  All these elements are multiplied by &lt;font face="Courier New"&gt;dd&lt;/font&gt;.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;I then call my regressGLS method, passing to it our data matrices and Omega inverse (&lt;font face="Courier New"&gt;ZI&lt;/font&gt;).  The full code of RegressGLS is shown below:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;--------------------------------------------------------------------------------&lt;/p&gt;
&lt;p&gt;let regressGLS (x0 : float [,] ) (y : float [,] ) (ZI : float [,] ) =&lt;/p&gt;
&lt;p&gt;    let xp = transpose2 x0&lt;/p&gt;
&lt;p&gt;    //--------------------------------&lt;br /&gt;
    // Now create X'ZI X&lt;br /&gt;
    //--------------------------------&lt;/p&gt;
&lt;p&gt;    let xpx = matMult2 (matMult2 xp ZI) x0&lt;/p&gt;
&lt;p&gt;    let X = xpx&lt;br /&gt;
    let XI, ret = matInverse X&lt;/p&gt;
&lt;p&gt;    let k = X.GetUpperBound 0&lt;br /&gt;
    let ni = 1 + x0.GetUpperBound 0&lt;br /&gt;
    &lt;br /&gt;
    for i = 0 to k do&lt;br /&gt;
         //printfn "XI Matrix: %10.5f %10.5f " XI.[i,0] XI.[i,1]&lt;br /&gt;
         printfn "XI Matrix: %15.10f %15.10f %15.10f %15.10f %15.10f " XI.[i,0] XI.[i,1] XI.[i,2] XI.[i,3] XI.[i,4]&lt;/p&gt;
&lt;p&gt;    //-----------------------------------------&lt;br /&gt;
    //Now Test&lt;br /&gt;
    //Multiply XI and X to calculate Identity Matrix&lt;br /&gt;
    //-----------------------------------------&lt;br /&gt;
    let XTest = Array2D.zeroCreate&amp;lt;float&amp;gt; (k+1) (k+1)&lt;br /&gt;
    let XTest = matMult XI X XTest&lt;/p&gt;
&lt;p&gt;    for i = 0 to k do&lt;br /&gt;
         //printfn "XTest Matrix: %10.5f %10.5f " XTest.[i,0] XTest.[i,1]&lt;br /&gt;
         printfn "XTest Matrix: %10.5f %10.5f %10.5f %10.5f %10.5f " XTest.[i,0] XTest.[i,1] XTest.[i,2] XTest.[i,3] XTest.[i,4]&lt;br /&gt;
    &lt;br /&gt;
    //let a0 = Array2D.zeroCreate&amp;lt;float&amp;gt; (k+1) ni&lt;br /&gt;
    //let a1 = Array2D.zeroCreate&amp;lt;float&amp;gt; ni 1&lt;br /&gt;
    let b = matMult2 (matMult2 (matMult2 XI xp) ZI) y&lt;/p&gt;
&lt;p&gt;    for i = 0 to k do&lt;br /&gt;
         printfn "Beta Hat: %i %10.5f" i b.[i,0]&lt;br /&gt;
    &lt;br /&gt;
    //---------------------------------------&lt;br /&gt;
    //  Calc Std Errors&lt;br /&gt;
    //---------------------------------------&lt;/p&gt;
&lt;p&gt;    let yp = transpose2 y&lt;br /&gt;
    &lt;br /&gt;
    let S1 = matMult2 (matMult2 yp ZI) y &lt;br /&gt;
    let S2 = matMult2 (matMult2 (matMult2 (transpose2 b) xp) ZI) y     &lt;br /&gt;
    &lt;br /&gt;
    printfn "S1: %10.2f" S1.[0,0]&lt;br /&gt;
    printfn "S2: %10.2f" S2.[0,0]&lt;/p&gt;
&lt;p&gt;    let sse = S1.[0,0] - S2.[0,0]&lt;br /&gt;
    printfn "SSE: %10.2f" sse&lt;br /&gt;
    &lt;br /&gt;
    let sigma2 = sse / float (ni - (k+1))&lt;br /&gt;
    printfn "SE: %10.2f" sigma2&lt;br /&gt;
    &lt;br /&gt;
    //--------------------------------------------------&lt;br /&gt;
    //Calc Variance/Covariance Matrix of B Hat&lt;br /&gt;
    //--------------------------------------------------&lt;br /&gt;
    //---------------------------------------&lt;br /&gt;
    // Calc T-Stats&lt;br /&gt;
    //---------------------------------------&lt;br /&gt;
    &lt;br /&gt;
    let bVar = Array2D.zeroCreate&amp;lt;float&amp;gt; (k+1) 1&lt;br /&gt;
    let bSE = Array2D.zeroCreate&amp;lt;float&amp;gt; (k+1) 1&lt;br /&gt;
    let TStat = Array2D.zeroCreate&amp;lt;float&amp;gt; (k+1) 1&lt;/p&gt;
&lt;p&gt;    for i = 0 to k do&lt;br /&gt;
        bVar.[i,0] &amp;lt;- sigma2 * XI.[i,i]&lt;br /&gt;
        bSE.[i,0] &amp;lt;- Math.Sqrt bVar.[i,0]&lt;br /&gt;
        TStat.[i,0] &amp;lt;- b.[i,0] / bSE.[i,0]&lt;br /&gt;
        printfn "B, Var, SE, TStat: %10.5f %10.5f %10.5f %10.5f" b.[i,0] bVar.[i,0] bSE.[i,0] TStat.[i,0]&lt;/p&gt;
&lt;p&gt;--------------------------------------------------------------------------------&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;When we run the GLS regression here is what we get:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;---------------------------------------------------------------------&lt;br /&gt;
Beta Hat: 0 31086.62449&lt;br /&gt;
Beta Hat: 1  -65.68368&lt;br /&gt;
Beta Hat: 2  739.92914&lt;br /&gt;
Beta Hat: 3  -85.11546&lt;br /&gt;
Beta Hat: 4   11.52357&lt;/p&gt;
&lt;p&gt;S1: 932945003919.98&lt;br /&gt;
S2: 927832435762.29&lt;br /&gt;
SSE: 5112568157.69&lt;br /&gt;
SE: 11862107.09&lt;/p&gt;
&lt;p&gt;B, Var, SE, TStat: 31086.62449 7459397.27394 2731.18972   11.38208&lt;br /&gt;
B, Var, SE, TStat:   -65.68368     166.51318   12.90400   -5.09018&lt;br /&gt;
B, Var, SE, TStat:   739.92914     268.59157   16.38876   45.14856&lt;br /&gt;
B, Var, SE, TStat:   -85.11546     414.70682   20.36435   -4.17963&lt;br /&gt;
B, Var, SE, TStat:    11.52357      20.11106    4.48454    2.56962&lt;br /&gt;
---------------------------------------------------------------------&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;My original straight OLS estimates are shown here:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;---------------------------------------------------------------------&lt;br /&gt;
B, Var, SE, TStat: 29843.97538 3520606.31417 1876.32788   15.90552&lt;br /&gt;
B, Var, SE, TStat:   -61.85082      74.88767    8.65377   -7.14727&lt;br /&gt;
B, Var, SE, TStat:   732.56913     126.54212   11.24909   65.12250&lt;br /&gt;
B, Var, SE, TStat:   -68.85197     198.90621   14.10341   -4.88194&lt;br /&gt;
B, Var, SE, TStat:    10.10098      10.35856    3.21847    3.13844&lt;br /&gt;
---------------------------------------------------------------------&lt;br /&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;It turns out that the parameter estimates (Beta Hats) are pretty much the same between the two regressions.  I suppose this is to be expected since both yield unbiased estimates of the parameters.  The standard errors are higher in the GLS regression, but that is OK.  The standard errors calculated under plain vanilla OLS are biased so they aren't any good anyway.  The standard errors under GLS are unbiased and are the appropriate ones to use for hypothesis testing.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;So, there you have F# &amp;amp; GLS!&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;&lt;br /&gt;
&lt;/font&gt;&lt;font face="Arial"&gt;&lt;strong&gt;PS.  If anyone finds a problem, please let me know!&lt;/strong&gt;&lt;/font&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;/font&gt;&lt;img src="http://www.caribousoftware.com/BobsBlog/aggbug/43.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://www.caribousoftware.com/BobsBlog/comments/43.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://www.caribousoftware.com/BobsBlog/comments/commentRss/43.aspx</wfw:commentRss>
        <trackback:ping>http://www.caribousoftware.com/BobsBlog/services/trackbacks/43.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Where is the Economy - Railroad Traffic</title>
        <link rel="alternate" type="text/html" href="http://www.caribousoftware.com/BobsBlog/archive/2009/08/13/where-is-the-economy-railroad-traffic.aspx" />
        <id>http://www.caribousoftware.com/BobsBlog/archive/2009/08/13/where-is-the-economy-railroad-traffic.aspx</id>
        <published>2009-08-13T18:30:07Z</published>
        <updated>2009-08-13T19:52:37Z</updated>
        <content type="html">&lt;p align="left"&gt;So, In my quest to see where the economy stands, I came across some really enlightening data.  Namely railroad traffic data published by the Association of American of Railroads.  They report data on carloads, by type of product, on a weekly basis.  I figure that looking at the weekly data can give a good idea of where the economy is in terms of the recession.  So, here's the first chart:&lt;/p&gt;
&lt;p&gt;&lt;img height="378" alt="" width="621" src="http://www.caribousoftware.com/BobsBlog/images/www_caribousoftware_com/BobsBlog/Carloads_NonIM.png" /&gt;&lt;/p&gt;
&lt;p&gt;This chart traces the weekly carload data from September 1, 2009 to the week ending August 8th 2009.  The carload data are for all products, but exclude intermodal (trailers &amp;amp; containers) carloads.  For reference, the lowest bar is for the Christmas/New Year holidays.  The trend line is a moving 4-week average.  You can see that traffic really fell off during the 4th quarter of 2008 and has stayed low.  The last 3 or 4 weeks show some uptick in traffic, but it would be hard to conclude that things are getting rosy fast.  The recent Fed statement that the economy seems to have leveled is borne out in these data.&lt;/p&gt;
&lt;p&gt;I also plotted Coal shipments (by far the largest product category):&lt;/p&gt;
&lt;p&gt;&lt;img height="379" alt="" width="620" src="/BobsBlog/images/www_caribousoftware_com/BobsBlog/Carloads_Coal.png" /&gt;&lt;/p&gt;
&lt;p&gt;Coal shipments are an interesting product to look at because they are so tied to electrical generation.  We see basically the same pattern as above, with again a slight uptick in the last 3-4 weeks.  I should note weather plays a role in electricity generation as a warm summer will yield extra demand due to air conditioning.  I haven't looked at the data, but it seems like this summer has been cooler than last.&lt;/p&gt;
&lt;p&gt;Next I looked at chemicals - the next largest product category:&lt;/p&gt;
&lt;p&gt;&lt;img height="378" alt="" width="626" src="/BobsBlog/images/www_caribousoftware_com/BobsBlog/Carloads_Chem.png" /&gt;&lt;/p&gt;
&lt;p&gt;Generally the chemicals that are shipped by rail are commodity type chemicals.  We see a somewhat muted pattern for this product category -- lower than last year, but pretty steady.  (I should check on that first data point, it looks out of whack)&lt;/p&gt;
&lt;p&gt;Next we look at crushed stone, sand &amp;amp; gravel.  Perhaps an indicator of highway and other infrastructure spending.  Are the stimulus dollars having an impact?&lt;/p&gt;
&lt;p&gt;&lt;img height="386" alt="" width="628" src="/BobsBlog/images/www_caribousoftware_com/BobsBlog/Carloads_Stone.png" /&gt;&lt;/p&gt;
&lt;p&gt;Things still look flat.&lt;/p&gt;
&lt;p&gt;Lastly, I took a look at intermodal traffic -- truck trailers and containers.  This category is large and would seem to serve as a good indicator of how imports are faring.&lt;/p&gt;
&lt;p&gt;&lt;img height="384" alt="" width="625" src="/BobsBlog/images/www_caribousoftware_com/BobsBlog/Carloads_IM.png" /&gt;&lt;/p&gt;
&lt;p&gt;Here we see the biggest drop off in the 4th quarter of 2008.  Things have been flat since March, with perhaps a slight uptick recently.  Nothing there to really write home about.&lt;/p&gt;
&lt;p&gt;There you have it.&lt;/p&gt;
&lt;p&gt;I think this data source is very valuable because it is timely -- it comes out weekly on Thursdays or the following week.  Because there are only a few railroads (and all of these are members of the AAR) that make up most of the freight traffic, the survey almost covers perhaps 90% of the total rail volume.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;&lt;img src="http://www.caribousoftware.com/BobsBlog/aggbug/42.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://www.caribousoftware.com/BobsBlog/comments/42.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://www.caribousoftware.com/BobsBlog/comments/commentRss/42.aspx</wfw:commentRss>
        <trackback:ping>http://www.caribousoftware.com/BobsBlog/services/trackbacks/42.aspx</trackback:ping>
    </entry>
</feed>