Click here to Skip to main content
15,896,455 members
Articles / Programming Languages / C# 4.0

Detecting Manipulations in Data with Benford's Law – A C# Class

Rate me:
Please Sign up or sign in to vote.
4.89/5 (42 votes)
23 Jun 2011CPOL11 min read 55.4K   941   67  
Statistical tests to determine goodness of fit between a data set and the Benford distribution. Benford's Law is useful in detecting fraud or artificially created numbers in otherwise "natural" distributions.
namespace BenfordSampleApp
{
    partial class MainForm
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.RawDataGrid = new System.Windows.Forms.DataGridView();
            this.AnalysisGrid = new System.Windows.Forms.DataGridView();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.Analyze = new System.Windows.Forms.Button();
            this.GetSampleData = new System.Windows.Forms.Button();
            this.GetRandom = new System.Windows.Forms.Button();
            this.ClearGrid = new System.Windows.Forms.Button();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.X2 = new System.Windows.Forms.Label();
            this.MAD = new System.Windows.Forms.Label();
            this.MSD = new System.Windows.Forms.Label();
            this.MAPE = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.RawDataGrid)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.AnalysisGrid)).BeginInit();
            this.SuspendLayout();
            // 
            // RawDataGrid
            // 
            this.RawDataGrid.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left)));
            this.RawDataGrid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.RawDataGrid.Location = new System.Drawing.Point(15, 25);
            this.RawDataGrid.Name = "RawDataGrid";
            this.RawDataGrid.Size = new System.Drawing.Size(389, 326);
            this.RawDataGrid.TabIndex = 0;
            // 
            // AnalysisGrid
            // 
            this.AnalysisGrid.AllowUserToAddRows = false;
            this.AnalysisGrid.AllowUserToDeleteRows = false;
            this.AnalysisGrid.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.AnalysisGrid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.AnalysisGrid.Location = new System.Drawing.Point(410, 25);
            this.AnalysisGrid.Name = "AnalysisGrid";
            this.AnalysisGrid.ReadOnly = true;
            this.AnalysisGrid.Size = new System.Drawing.Size(356, 252);
            this.AnalysisGrid.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(96, 13);
            this.label1.TabIndex = 2;
            this.label1.Text = "Dataset to Analyze";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(407, 9);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(96, 13);
            this.label2.TabIndex = 3;
            this.label2.Text = "Benford Conformity";
            // 
            // Analyze
            // 
            this.Analyze.Location = new System.Drawing.Point(691, 283);
            this.Analyze.Name = "Analyze";
            this.Analyze.Size = new System.Drawing.Size(75, 23);
            this.Analyze.TabIndex = 4;
            this.Analyze.Text = "Analyze";
            this.Analyze.UseVisualStyleBackColor = true;
            this.Analyze.Click += new System.EventHandler(this.Analyze_Click);
            // 
            // GetSampleData
            // 
            this.GetSampleData.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.GetSampleData.Location = new System.Drawing.Point(15, 357);
            this.GetSampleData.Name = "GetSampleData";
            this.GetSampleData.Size = new System.Drawing.Size(103, 23);
            this.GetSampleData.TabIndex = 5;
            this.GetSampleData.Text = "Get Sample Data";
            this.GetSampleData.UseVisualStyleBackColor = true;
            this.GetSampleData.Click += new System.EventHandler(this.GetSampleData_Click);
            // 
            // GetRandom
            // 
            this.GetRandom.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.GetRandom.Location = new System.Drawing.Point(124, 357);
            this.GetRandom.Name = "GetRandom";
            this.GetRandom.Size = new System.Drawing.Size(117, 23);
            this.GetRandom.TabIndex = 6;
            this.GetRandom.Text = "Get Random Values";
            this.GetRandom.UseVisualStyleBackColor = true;
            this.GetRandom.Click += new System.EventHandler(this.GetRandom_Click);
            // 
            // ClearGrid
            // 
            this.ClearGrid.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.ClearGrid.Location = new System.Drawing.Point(334, 357);
            this.ClearGrid.Name = "ClearGrid";
            this.ClearGrid.Size = new System.Drawing.Size(75, 23);
            this.ClearGrid.TabIndex = 8;
            this.ClearGrid.Text = "Clear Grid";
            this.ClearGrid.UseVisualStyleBackColor = true;
            this.ClearGrid.Click += new System.EventHandler(this.ClearGrid_Click);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(414, 286);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(118, 13);
            this.label3.TabIndex = 9;
            this.label3.Text = "Mean Signed Deviation";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(414, 310);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(126, 13);
            this.label4.TabIndex = 10;
            this.label4.Text = "Mean Absolute Deviation";
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(414, 360);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(123, 13);
            this.label5.TabIndex = 11;
            this.label5.Text = "Chi-Square Test Statistic";
            // 
            // X2
            // 
            this.X2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.X2.Location = new System.Drawing.Point(559, 357);
            this.X2.Name = "X2";
            this.X2.Size = new System.Drawing.Size(207, 18);
            this.X2.TabIndex = 14;
            this.X2.Text = "0";
            // 
            // MAD
            // 
            this.MAD.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.MAD.Location = new System.Drawing.Point(559, 307);
            this.MAD.Name = "MAD";
            this.MAD.Size = new System.Drawing.Size(126, 18);
            this.MAD.TabIndex = 13;
            this.MAD.Text = "0";
            // 
            // MSD
            // 
            this.MSD.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.MSD.Location = new System.Drawing.Point(559, 283);
            this.MSD.Name = "MSD";
            this.MSD.Size = new System.Drawing.Size(126, 18);
            this.MSD.TabIndex = 12;
            this.MSD.Text = "0";
            // 
            // MAPE
            // 
            this.MAPE.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.MAPE.Location = new System.Drawing.Point(559, 333);
            this.MAPE.Name = "MAPE";
            this.MAPE.Size = new System.Drawing.Size(127, 18);
            this.MAPE.TabIndex = 16;
            this.MAPE.Text = "0";
            // 
            // label7
            // 
            this.label7.AutoSize = true;
            this.label7.Location = new System.Drawing.Point(414, 336);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(143, 13);
            this.label7.TabIndex = 15;
            this.label7.Text = "Mean Absolute Percent Error";
            // 
            // MainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(778, 392);
            this.Controls.Add(this.MAPE);
            this.Controls.Add(this.label7);
            this.Controls.Add(this.X2);
            this.Controls.Add(this.MAD);
            this.Controls.Add(this.MSD);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.ClearGrid);
            this.Controls.Add(this.GetRandom);
            this.Controls.Add(this.GetSampleData);
            this.Controls.Add(this.Analyze);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.AnalysisGrid);
            this.Controls.Add(this.RawDataGrid);
            this.Name = "MainForm";
            this.Text = "Benford Analysis Sample App";
            ((System.ComponentModel.ISupportInitialize)(this.RawDataGrid)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.AnalysisGrid)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.DataGridView RawDataGrid;
        private System.Windows.Forms.DataGridView AnalysisGrid;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button Analyze;
        private System.Windows.Forms.Button GetSampleData;
        private System.Windows.Forms.Button GetRandom;
        private System.Windows.Forms.Button ClearGrid;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Label X2;
        private System.Windows.Forms.Label MAD;
        private System.Windows.Forms.Label MSD;
        private System.Windows.Forms.Label MAPE;
        private System.Windows.Forms.Label label7;
    }
}

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)


Written By
Software Developer
United States United States
I learned my first programming language--Apple Basic--in 1989. Over the years, I've done projects in C/C++, Pascal, FoxPro, 4D, AS/400, dBase, perl/CGI, Access, MSSQL, VB5-6/COM+, Classic ASP, uncounted legions of Windows, Mac, and *nix scripting languages and now C# ASP.NET/MVC/Razor/jQuery.

I started getting paid to do this stuff in 1993 as the admin for a 100 node network and began writing one-off apps for the company in my spare time. By 2002, I was developing and managing enterprise software projects full time.

I sat through so many Microsoft classes that they should offer me an honorary MCSE, MCSD, and a bunch of other letters, plus name something on campus after me. I took an undergraduate degree in Management & Business Information Systems, earned an MBA, and I hold a PMP credential, though trying to bring projects to successful completion (as opposed to tracking processes into perpetuity) using the PMBOK is like trying to get to a nice restaurant in a big city by reading a book about its architecture. But, I digress...

I've worked in the building materials industry supporting wholesale trading since 1993. I maintain an unhealthy level of interest in exchange-based and cash forward trading, derivatives, simulation, forecasting, project management, and other quantitative analysis topics (e.g. queue theory, optimal inventory policy, etc.) Most recently, I finished a Systems Science certificate in Computer Modeling & Simulation.

Comments and Discussions