Example #1
0
        /// <summary>
        /// Define the relationships.
        /// </summary>
        /// <param name="network">The network.</param>
        public void DefineRelationships(BayesianNetwork network)
        {
            // define event relations, if they are not there already
            ParsedEvent   childParsed = ChildEvent;
            BayesianEvent childEvent  = network.RequireEvent(childParsed.Label);

            foreach (ParsedEvent e in this.givenEvents)
            {
                BayesianEvent parentEvent = network.RequireEvent(e.Label);
                network.CreateDependency(parentEvent, childEvent);
            }
        }
Example #2
0
        public BayesianNetwork Create()
        {
            BayesianNetwork network = new BayesianNetwork();
            BayesianEvent   a       = network.CreateEvent("a");
            BayesianEvent   b       = network.CreateEvent("b");

            network.CreateDependency(a, b);
            network.FinalizeStructure();
            a.Table.AddLine(0.5, true);        // P(A) = 0.5
            b.Table.AddLine(0.2, true, true);  // p(b|a) = 0.2
            b.Table.AddLine(0.8, true, false); // p(b|~a) = 0.8
            network.Validate();
            return(network);
        }
Example #3
0
        static void Main(string[] args)
        {
            /*Przykład 4.2.3 Teoria Bayesa - Zakłady Produkcyjne Etap Manualny
             * Lesson 1 - Machine Learning in C# for Amazon ML Group created by me :-)
             * PS. Kill me I use polish :-)
             */

            BayesianNetwork network        = new BayesianNetwork();
            BayesianEvent   WadliwyElement = network.CreateEvent("wadliwy_element");
            BayesianEvent   A1             = network.CreateEvent("wadliwy_element_zaklad_A1");
            BayesianEvent   A2             = network.CreateEvent("wadliwy_element_zaklad_A2");
            BayesianEvent   A3             = network.CreateEvent("wadliwy_element_zaklad_A3");
            BayesianEvent   A4             = network.CreateEvent("wadliwy_element_zaklad_A4");

            network.CreateDependency(WadliwyElement, A1, A2, A3, A4);
            network.FinalizeStructure();

            WadliwyElement?.Table?.AddLine(0.1083, true);
            A1?.Table.AddLine(0.069, true, true);
            A1?.Table.AddLine(1 - 0.069, true, false);
            A2?.Table.AddLine(0.277, true, true);
            A2?.Table.AddLine(1 - 0.277, true, false);
            A3?.Table.AddLine(0.007, true, true);
            A3?.Table.AddLine(1 - 0.007, true, false);
            A4?.Table.AddLine(0.646, true, true);
            A4?.Table.AddLine(1 - 0.646, true, false);
            network.Validate();
            Console.WriteLine(network.ToString() + "\n");
            Console.WriteLine($"Liczba parametrów: {network.CalculateParameterCount()}");

            EnumerationQuery query = new EnumerationQuery(network);

            query.DefineEventType(WadliwyElement, EventType.Evidence);
            query.DefineEventType(A1, EventType.Outcome);
            query.DefineEventType(A2, EventType.Evidence);
            query.DefineEventType(A3, EventType.Evidence);
            query.DefineEventType(A4, EventType.Evidence);

            query.SetEventValue(WadliwyElement, false);
            query.SetEventValue(A1, false);
            query.SetEventValue(A2, false);
            query.SetEventValue(A3, false);
            query.SetEventValue(A4, false);
            query.Execute();

            Console.WriteLine(query.ToString());
        }
        /// <summary>
        /// Init to Naive Bayes.
        /// </summary>
        private void InitNaiveBayes()
        {
            // clear out anything from before
            _network.RemoveAllRelations();

            // locate the classification target event
            BayesianEvent classificationTarget = _network
                                                 .ClassificationTargetEvent;

            // now link everything to this event
            foreach (BayesianEvent e in _network.Events)
            {
                if (e != classificationTarget)
                {
                    _network.CreateDependency(classificationTarget, e);
                }
            }

            _network.FinalizeStructure();
        }
Example #5
0
        public void TestEnumeration1()
        {
            BayesianNetwork network = new BayesianNetwork();
            BayesianEvent   a       = network.CreateEvent("a");
            BayesianEvent   b       = network.CreateEvent("b");

            network.CreateDependency(a, b);
            network.FinalizeStructure();
            a.Table.AddLine(0.5, true);        // P(A) = 0.5
            b.Table.AddLine(0.2, true, true);  // p(b|a) = 0.2
            b.Table.AddLine(0.8, true, false); // p(b|~a) = 0.8
            network.Validate();

            EnumerationQuery query = new EnumerationQuery(network);

            query.DefineEventType(a, EventType.Evidence);
            query.DefineEventType(b, EventType.Outcome);
            query.SetEventValue(b, true);
            query.SetEventValue(a, true);
            query.Execute();
            TestPercent(query.Probability, 20);
        }
 /// <summary>
 /// Define the relationships.
 /// </summary>
 /// <param name="network">The network.</param>
 public void DefineRelationships(BayesianNetwork network)
 {
     // define event relations, if they are not there already
     ParsedEvent childParsed = ChildEvent;
     BayesianEvent childEvent = network.RequireEvent(childParsed.Label);
     foreach (ParsedEvent e in this.givenEvents)
     {
         BayesianEvent parentEvent = network.RequireEvent(e.Label);
         network.CreateDependency(parentEvent, childEvent);
     }
 }