Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Big O Algorithm Analyzer for .NET

, , 16 Feb 2010
A heurisitc graphing tool to help discover 'Big O Notation' function thru infinite asymptotic's and instrumentation.
Article_Demo.zip
Article_Demo
ColorPicker.dll
Generics.dll
SampleInfiniteAsy.dll
WPFBigOAnalyzer.exe
WPFBigOAnalyzer.vshost.exe
WPFGraph.dll
Article_Src.zip
Article_Src
SampleInfiniteAsy
bin
Debug
obj
Debug
Properties
SampleInstrimted
bin
Debug
obj
Debug
Properties
WPFBigOAnalyzer
BigOFunction
bin
Debug
WPFBigOAnalyzer.vshost.exe
WPFBigOAnalyzer.vshost.exe.manifest
Release
ColorPicker.dll
Generics.dll
WPFBigOAnalyzer.exe
WPFBigOAnalyzer.vshost.exe
WPFGraph.dll
Heurstics
obj
Debug
Refactor
WPFBigOAnalyzer.exe
TempPE
Properties.Resources.Designer.cs.dll
Properties
Settings.settings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Reflection;
using System.Threading;

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;

using WPFBigOAnalyzer;
using WPFBigOAnalyzer.BigOFunction;

using Meta.Numerics;
using Meta.Numerics.Statistics;
using Meta.Numerics.Matrices;
using Meta.Numerics.Functions;
namespace WPFBigOAnalyzer.Heurstics
{
    class TraditionalStatistical
    {
        #region Fields
        private static double _maxExtent = 0;
        private static DecoratedMarker _bestGuess;
        private static Dictionary<String, DecoratedMarker[]> _TargetData;
        private static Dictionary<String, DecoratedMarker> _RefrenceData = new Dictionary<string, DecoratedMarker>();
        private static Dictionary<String, Object> _ResultData = new Dictionary<string,Object>();
        private static StringBuilder sb = new StringBuilder(1000);
        #endregion

        public static StringBuilder Report
        {
            get { return TraditionalStatistical.sb; }
            private set { TraditionalStatistical.sb = value; }
        }

        public static double MaxExtent
        {
            get { return TraditionalStatistical._maxExtent; }
            private set { TraditionalStatistical._maxExtent = value; }
        }

        public static Dictionary<String, DecoratedMarker> RefrenceData
        {
            get { return TraditionalStatistical._RefrenceData; }
        }

        public static String BestGuess
        {
            get;
            private set;
        }

        public static DecoratedMarker BestGuessMarker
        {
            get { return TraditionalStatistical._bestGuess; }
            set { TraditionalStatistical._bestGuess = value; }
        }
        public TraditionalStatistical()
        {
            Report = null;
            _TargetData = new Dictionary<string, DecoratedMarker[]>();
            _ResultData = new Dictionary<string, Object>();
            GetTargetData();
        }

        private static void GenerateReport()
        {
            BestGuess = "Not Found!";
            double bestPerson = -1;

            sb = new StringBuilder(1000);

            foreach (String key in _ResultData.Keys)
            {
                Dictionary<String, object> results = (Dictionary<String, object>)_ResultData[key];
                sb.Append("~~~~~~~~~~~~~~~~").Append(key).Append("~~~~~~~~~~~~~~~~").Append("\r\n");
                /*
                 * This test measures the strength of the linear correlation between two variables. 
                 * The test statistic r is simply the covariance of the two variables, scaled by their 
                 * respective standard deviations so as to obtain a number between 
                 * -1 (perfect linear anti-correlation) and +1 (perfect linear correlation).
                 * The Pearson test cannot reliably detect or rule out non-linear correlations.
                 * The Pearson correlation test requires O(N) operations.
                 */
                #region Pearson r Test
                try 
                {
                    object o = results["Persons r Test: "];
                
                    sb.Append("Persons r Test: ").Append(results["Persons r Test: "]).Append("\r\n");
                    sb.Append("P(r' < r): ").Append(results["P(r' < r): "]).Append("\r\n");
                    sb.Append("P(r' > r): ").Append(results["P(r' > r): "]).Append("\r\n");
                    sb.Append("r Mean Distribution: ").Append(((Distribution)results["r Distribution: "]).Mean).Append("\r\n");
                    sb.Append("r Median: ").Append(((Distribution)results["r Distribution: "]).Median).Append("\r\n");
                    sb.Append("r Standard Deviation: ").Append(((Distribution)results["r Distribution: "]).StandardDeviation).Append("\r\n");
                    sb.Append("r Nonvanishing Interval: ").Append(((Distribution)results["r Distribution: "]).Support).Append("\r\n");

                    if (bestPerson < (double)results["Persons r Test: "] || bestPerson == -1)
                    {
                        bestPerson = (double)results["Persons r Test: "];
                        BestGuess = key;
                        String[] k = key.Split(new char[] { Convert.ToChar(":") });
                        BestGuessMarker = _RefrenceData[k[1]];
                    }
                }
                catch{}
                #endregion
                sb.Append("\r\n").Append("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~").Append("\r\n\r\n");
            }
            sb.Insert(0, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\nBest Guess: " + BestGuess + "\r\n" + bestPerson + "\r\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n\r\n");
            Report = sb;
            WPFBigOAnalyzer.Window1.Document.Blocks.Clear();
            WPFBigOAnalyzer.Window1.Document.Blocks.Add(new Paragraph(new Run(TraditionalStatistical.Report.ToString())));
        }

        #region Experimental Data Helper

        static void GetTargetData()
        {
            _TargetData = (Dictionary<String, DecoratedMarker[]>)((WPFBigOAnalyzer.Window1)WPFBigOAnalyzer.Window1.WPFBigOAnalyser).TargetData;
        }

        static void GetRefrenceData(int infinityPoint)
        {
            _RefrenceData = new Dictionary<string, DecoratedMarker>();
            Assembly asm = Assembly.GetExecutingAssembly();
            Type functions = asm.GetType("WPFBigOAnalyzer.BigOFunction.Functions");

            foreach (MethodInfo m in asm.GetType("WPFBigOAnalyzer.BigOFunction.Functions").GetMethods())
            {
                if (m.Name.IndexOf("Line") != -1)
                {
                    DecoratedMarker dm = (DecoratedMarker)m.Invoke(functions, new object[] { infinityPoint });
                    _RefrenceData.Add(dm.MarkerText,dm);
                }
            }
        }

        #endregion

        #region Biviriate Tests - (Pearson r Test, Spearman ρ Test, Kendall τ Test)

        protected internal static void RunTests()
        {
            int totcount = 0;
            double[,] eval = null;

            Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;

            Thread.BeginCriticalRegion();
            //get each test line on the graph..
            foreach (String targetKey in _TargetData.Keys)
            {
                MultivariateSample sample = new MultivariateSample(2);
                DecoratedMarker[] markers = _TargetData[targetKey];

                if (totcount == 0)
                    totcount = markers.Length;

                GetRefrenceData(markers.Length);
                eval = new double[_RefrenceData.Count + 2, markers.Length];

                for (int p = 0; p < markers.Length; p++)
                {
                    if (p == 0)
                        eval[0, p] = markers[p].Point[0];
                    else
                        eval[0, p] = markers[p].Point[0] + eval[0, p - 1];
                }

                int t = 1;
                int t1;
                foreach (String refKey in _RefrenceData.Keys)
                {
                    t++;
                    t1 = 0;
                    foreach (double d in _RefrenceData[refKey].Point)
                    {
                        eval[t, t1++] = d;
                    }
                }
                Thread.EndCriticalRegion();

                //Create the sample data...  and run the test...
                for (int x = 1; x < _RefrenceData.Count + 1; x++)
                {
                    sample.Clear();

                    for (int y = 0; y < totcount; y++)
                    {
                        sample.Add(new double[] { eval[0, y], eval[x + 1, y] });
                    }
                    Dictionary<String, object> results = new Dictionary<String, object>();

                    try
                    {
                        Thread.BeginCriticalRegion();
                        TestResult r = sample.PearsonRTest(0, 1);
                        if (r.Statistic.ToString() != "NaN")
                        {
                            results.Add("Persons r Test: ", r.Statistic);
                            results.Add("P(r' < r): ", r.LeftProbability);
                            results.Add("P(r' > r): ", r.RightProbability);
                            results.Add("r Distribution: ", r.Distribution);
                        }
                        else
                        {
                            results.Add("Persons r Test: ", r.Statistic);
                        }
                    }
                    catch (Exception e)
                    {

                        Console.WriteLine(e.Message);
                    }
                    finally { Thread.EndCriticalRegion(); }

                    _ResultData.Add(targetKey + ":" + (String)_RefrenceData.Keys.ElementAt<String>(x - 1), results);
                }
            }
            GenerateReport();
        }
    }

        #endregion
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Authors

dawright

United States United States
I am a .NET developer who works daily on enterprise-scale applications using C#, SQL, XML, ASP.NET, and myriad other technologies. My academic background is in physics and economics.
 
I am the original architect of Sandcastle managed reference documentation engine and of the Meta.Numerics library for scientific computation.

Proto-Bytes
CEO AW Proto-Code, Inc.
United States United States
I’m a self learned wiz kid turned Architect. Stared with an Apple IIe, using AppleSoft Basic, ProDos and Begal Basic at age 10.
 
Picked up LPC in college before the Dot Com Explosion. Wrote some Object C in the USAF for one of my instructors. Got a break from a booming computer manufacture testing server software. Left the Boom and went to work for a Dot Com built from the ashes of Sun Micro in CS. Mentoring in Java solidified me as a professional developer. Danced in the light of the sun for 13 years, before turning to the dark side. An evil MVP mentored me in the ways of C# .NET. I have not looked back since.
 
Interests include:
 
~ Windows Presentation Foundation and Silverlight
~ Parallel Programming
~ Instruction Set Simulation and Visualization
~ CPU to GPU code conversion
~ Performance Optimizations
~ Mathematics and Number Theory
~ Domain Specific Languages
~ Virtual Machine Design and Optimization
~ Graphics Development
~ Compiler Theory and Assembler Conversion Methodology
~ CUDA, OpenCL, Direct Compute, Quantum Mechanics
 
IEEE Associate Member 2000
Group type: Organisation (No members)


Follow on   Twitter

| Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 16 Feb 2010
Article Copyright 2009 by dawright, Proto-Bytes
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid