6 Replies - 1671 Views - Last Post: 17 October 2011 - 03:00 PM Rate Topic: -----

#1 fatcat22222  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-October 11

DJIA Histogram VB

Posted 17 October 2011 - 09:32 AM

Hello, I am currently working on a program to graph a histogram of the Dow Jones, the following is my code

Imports System.IO

Imports System.Math

Public Class Form1
    Dim g As Graphics
    Dim Data(), NormalizedData() As Double
    Dim Bound As Integer
    Dim NumToAverage As Integer
    Dim Mean, StdDev As Double
    Dim MaxTotal As Integer
    Dim NumBins As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim FS As FileStream
        Dim SR As StreamReader
        Dim Prices(30000) As Double
        Dim count As Integer = 0
        Dim itm As Object
        FS = New FileStream(Application.StartupPath & "/Prices2.txt", FileMode.Open)
        SR = New StreamReader(FS)
        itm = SR.ReadLine()
        While Not itm = Nothing
            count = count + 1
            Prices(count) = itm
            itm = SR.ReadLine()
        End While
        SR.Close()
        FS.Close()

        Dim Reverse(count) As Double
        Dim i As Integer

        For i = 1 To count
            Reverse(i) = Prices(count - i) + 1
        Next

        Dim Returns(count) As Double

        For i = 1 To count - 1
            Returns(i) = Log10(Reverse(i + 1) / Reverse(1))
        Next

        initialize()
        getdata()
        FindMeanAndStandardDeviation()
        NormalizeData()
        drawHistogram()
    End Sub

    Sub initialize()
        g = Graph.CreateGraphics

        ReDim Data(Bound)
        ReDim NormalizedData(Bound)
        NumBins = Graph.Width
    End Sub

    Dim count As Double
    Dim Returns(Bound) As Double

    Sub getdata()
        Bound = count - 1
        For i = 1 To Bound

            Data(i) = Returns(i) ' Describe the number Data(i) in words

        Next


    End Sub
       

    Sub FindMeanAndStandardDeviation()
        Dim i As Integer
        Dim sumX, sumX2 As Double
        sumX = 0
        sumX2 = 0

        For i = 1 To Bound
            sumX = sumX + Data(i)
            sumX2 = sumX2 + Data(i) ^ 2
        Next

        Mean = sumX / Bound
        txtMean.Text = Int(10000 * Mean) / 10000
        StdDev = Sqrt(sumX2 / Bound - Mean ^ 2) 'Why does this formula give the standard deviation?
        txtStdDev.Text = Int(10000 * StdDev) / 10000

    End Sub
    
    Sub NormalizeData()
        For i = 1 To Bound
            NormalizedData(i) = (Data(i) - Mean) / StdDev ' What is the mean and standard deviation of the numbers in the array NormalizedData?
        Next
    End Sub

    Sub drawHistogram()
        Const NumStdDevFromMean = 3
        Dim x, y As Double
        Dim Horizontal, Vertical As Integer
        Dim i, j As Integer
        Dim BinWidth As Double
        Dim Totals(NumBins) As Integer
        Dim redFill As SolidBrush = New SolidBrush(Color.Red)
        g.Clear(Color.White)
        BinWidth = 2 * NumStdDevFromMean / NumBins
        For i = 1 To Bound
            For j = 1 To NumBins
                x = NormalizedData(i)
                If (x >= ((j - 1) * BinWidth - NumStdDevFromMean)) And (x < (j * BinWidth -
             NumStdDevFromMean)) Then
                    Totals(j) = Totals(j) + 1
                    Exit For
                End If
            Next
        Next

        MaxTotal = 0

        For j = 1 To NumBins
            If Totals(j) > MaxTotal Then MaxTotal = Totals(j)
        Next

        For j = 1 To NumBins
            x = j
            y = Totals(j) / MaxTotal
            Horizontal = ConvertX(x)
            Vertical = ConvertY(y)
            g.FillRectangle(redFill, Horizontal, Vertical, 2, Graph.Height)
        Next

    End Sub

    Function getRandomNumber() As Double
        Return Rnd()
    End Function

    Function ConvertX(ByVal x As Double) As Integer

        Return ((x - 0) / (NumBins - 0)) * Graph.Width

    End Function


    Function ConvertY(ByVal y As Double) As Integer

        Return (1 - (y - 0) / (1 - 0)) * Graph.Height

    End Function





    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub
End Class 


Im not sure if my getdata() sub routine is correct. I want this to display a histogram of returns for the DJIA as well as display the mean and standard deviation.
Also, on my Convert(Y) function it keeps saying im diviging by zero but I dont see how I am.

Any comments and/or help would be greatly appreciated.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: DJIA Histogram VB

#2 fatcat22222  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-October 11

Re: DJIA Histogram VB

Posted 17 October 2011 - 09:39 AM

Hello I am currently working to modify my Dow Jones histogram program.
I want to include Yearly Average Percentage Change and Skewness into my program.
This is the code I have thus far:
Imports System.IO

Imports System.Math

Public Class Form1
    Dim g As Graphics
    Dim Data(), NormalizedData() As Double
    Dim Bound As Integer
    Dim NumToAverage As Integer
    Dim Mean, StdDev As Double
    Dim MaxTotal As Integer
    Dim NumBins As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim FS As FileStream
        Dim SR As StreamReader
        Dim Prices(30000) As Double
        Dim count As Integer = 0
        Dim itm As Object
        FS = New FileStream(Application.StartupPath & "/Prices2.txt", FileMode.Open)
        SR = New StreamReader(FS)
        itm = SR.ReadLine()
        While Not itm = Nothing
            count = count + 1
            Prices(count) = itm
            itm = SR.ReadLine()
        End While
        SR.Close()
        FS.Close()

        Dim Reverse(count) As Double
        Dim i As Integer

        For i = 1 To count
            Reverse(i) = Prices(count - i) + 1
        Next

        Dim Returns(count) As Double

        For i = 1 To count - 1
            Returns(i) = Log10(Reverse(i + 1) / Reverse(1))
        Next

        initialize()
        YAverage()
        Skewness()
        getdata()
        FindMeanAndStandardDeviation()
        NormalizeData()
        drawHistogram()
    End Sub

    Sub initialize()
        g = Graph.CreateGraphics

        ReDim Data(Bound)
        ReDim NormalizedData(Bound)
        NumBins = Graph.Width
    End Sub

    Dim count As Double
    Dim Returns(Bound) As Double

    Sub getdata()
        Bound = count - 1
        For i = 1 To Bound

            Data(i) = Returns(i) ' Describe the number Data(i) in words

        Next


    End Sub
    Sub Skewness()

    End Sub
    Dim Sum As Double
    Dim Average As Double
    Dim Reverse(Bound) As Double
    Sub YAverage()
        For i = 1 To count
            Bound = count - 1
            Data(i) = Reverse(i)
        Next
        For i = 1 To Bound
            Sum = Sum + Returns(i)
            Average = Sum / Bound
            TextBox1.Text = Average
        Next

    End Sub

    Sub FindMeanAndStandardDeviation()
        Dim i As Integer
        Dim sumX, sumX2 As Double
        sumX = 0
        sumX2 = 0

        For i = 1 To Bound
            sumX = sumX + Data(i)
            sumX2 = sumX2 + Data(i) ^ 2
        Next

        Mean = sumX / Bound
        txtMean.Text = Int(10000 * Mean) / 10000
        StdDev = Sqrt(sumX2 / Bound - Mean ^ 2) 'Why does this formula give the standard deviation?
        txtStdDev.Text = Int(10000 * StdDev) / 10000

    End Sub
    
    Sub NormalizeData()
        For i = 1 To Bound
            NormalizedData(i) = (Data(i) - Mean) / StdDev ' What is the mean and standard deviation of the numbers in the array NormalizedData?
        Next
    End Sub

    Sub drawHistogram()
        Const NumStdDevFromMean = 3
        Dim x, y As Double
        Dim Horizontal, Vertical As Integer
        Dim i, j As Integer
        Dim BinWidth As Double
        Dim Totals(NumBins) As Integer
        Dim redFill As SolidBrush = New SolidBrush(Color.Red)
        g.Clear(Color.White)
        BinWidth = 2 * NumStdDevFromMean / NumBins
        For i = 1 To Bound
            For j = 1 To NumBins
                x = NormalizedData(i)
                If (x >= ((j - 1) * BinWidth - NumStdDevFromMean)) And (x < (j * BinWidth -
             NumStdDevFromMean)) Then
                    Totals(j) = Totals(j) + 1
                    Exit For
                End If
            Next
        Next

        MaxTotal = 0

        For j = 1 To NumBins
            If Totals(j) > MaxTotal Then MaxTotal = Totals(j)
        Next

        For j = 1 To NumBins
            x = j
            y = Totals(j) / MaxTotal
            Horizontal = ConvertX(x)
            Vertical = ConvertY(y)
            g.FillRectangle(redFill, Horizontal, Vertical, 2, Graph.Height)
        Next

    End Sub

    Function getRandomNumber() As Double
        Return Rnd()
    End Function

    Function ConvertX(ByVal x As Double) As Integer

        Return ((x - 0) / (NumBins - 0)) * Graph.Width

    End Function


    Function ConvertY(ByVal y As Double) As Integer

        Return (1 - (y - 0) / (1 - 0)) * Graph.Height

    End Function





    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub
End Class 


I believe I have the code working for the yearly average but I am stuck on the skewness. I am not quite sure how to put the mathematical formula into a programming code for skewness.

Any suggestions?

Thanks!
Was This Post Helpful? 0
  • +
  • -

#3 fatcat22222  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-October 11

Re: DJIA Histogram VB

Posted 17 October 2011 - 12:32 PM

Hello,

So I have figured out how to compute the skewness. This is the program I now have:

Imports System.IO

Imports System.Math

Public Class Form1
    Dim g As Graphics
    Dim Data(), NormalizedData() As Double
    Dim Bound As Integer
    Dim NumToAverage As Integer
    Dim Mean, StdDev As Double
    Dim MaxTotal As Integer
    Dim NumBins As Integer
    Dim i As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim FS As FileStream
        Dim SR As StreamReader
        Dim Prices(30000) As Double
        Dim count As Integer = 0
        Dim itm As Object
        FS = New FileStream(Application.StartupPath & "/Prices2.txt", FileMode.Open)
        SR = New StreamReader(FS)
        itm = SR.ReadLine()
        While Not itm = Nothing
            count = count + 1
            Prices(count) = itm
            itm = SR.ReadLine()
        End While
        SR.Close()
        FS.Close()

        Dim Reverse(count) As Double
        Dim i As Integer

        For i = 1 To count
            Reverse(i) = Prices(count - i) + 1
        Next

        Dim Returns(count) As Double

        For i = 1 To count - 1
            Returns(i) = Log10(Reverse(i + 1) / Reverse(1))
        Next

        initialize()
        YAverage()
        Skewness()
        getdata()
        FindMeanAndStandardDeviation()
        NormalizeData()
        drawHistogram()
    End Sub

    Sub initialize()
        g = Graph.CreateGraphics

        ReDim Data(Bound)
        ReDim NormalizedData(Bound)
        NumBins = Graph.Width
    End Sub

    Dim count As Double
    Dim Returns(Bound) As Double

    Sub getdata()
        Bound = count - 1
        For j = 1 To Bound

            Data(i) = Returns(i)

        Next


    End Sub
   
    Dim Sum As Double
    Dim Average As Double
    Dim Reverse(Bound) As Double


    Sub YAverage()
        For j = 1 To count
            Bound = count - 1
            Data(i) = Reverse(i)
        Next
        For j = 1 To Bound
            Sum = Sum + Returns(i)
            Average = Sum / Bound
            TextBox1.Text = Average
        Next

    End Sub

    Sub FindMeanAndStandardDeviation()
        Dim i As Integer
        Dim sumX, sumX2 As Double
        sumX = 0
        sumX2 = 0

        For i = 1 To Bound
            sumX = sumX + Data(i)
            sumX2 = sumX2 + Data(i) ^ 2
        Next

        Mean = sumX / Bound
        txtMean.Text = Int(10000 * Mean) / 10000
        StdDev = Sqrt(sumX2 / Bound - Mean ^ 2)
        txtStdDev.Text = Int(10000 * StdDev) / 10000

    End Sub
    Dim skew As Double = 0

    Sub Skewness()
        skew = (Returns(i) - Mean) * (Returns(i) - Mean) * (Returns(i) - Mean) / (Bound - 1)
        TextBox2.Text = skew
    End Sub
    Sub NormalizeData()
        For j = 1 To Bound
            NormalizedData(i) = (Data(i) - Mean) / StdDev
        Next
    End Sub

    Sub drawHistogram()
        Const NumStdDevFromMean = 4
        Dim x, y As Double
        Dim Horizontal, Vertical As Integer
        Dim i, j As Integer
        Dim BinWidth As Double
        Dim Totals(NumBins) As Integer
        Dim redFill As SolidBrush = New SolidBrush(Color.Red)
        g.Clear(Color.White)
        BinWidth = 2 * NumStdDevFromMean / NumBins
        For i = 1 To Bound
            For j = 1 To NumBins
                x = NormalizedData(i)
                If (x >= ((j - 1) * BinWidth - NumStdDevFromMean)) And (x < (j * BinWidth -
             NumStdDevFromMean)) Then
                    Totals(j) = Totals(j) + 1
                    Exit For
                End If
            Next
        Next

        MaxTotal = 0

        For j = 1 To NumBins
            If Totals(j) > MaxTotal Then MaxTotal = Totals(j)
        Next

        For j = 1 To NumBins
            x = j
            y = Totals(j) / MaxTotal
            Horizontal = ConvertX(x)
            Vertical = ConvertY(y)
            g.FillRectangle(redFill, Horizontal, Vertical, 2, Graph.Height)
        Next

    End Sub

    Function getRandomNumber() As Double
        Return Rnd()
    End Function

    Function ConvertX(ByVal x As Double) As Integer

        Return ((x - 0) / (NumBins - 0)) * Graph.Width

    End Function


    Function ConvertY(ByVal y As Double) As Integer

        Return (1 - (y - 0) / (1 - 0)) * Graph.Height

    End Function





    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub
End Class 


everything seems to be okay but I am still getting an error on the function convertY saying that I am dividing by zero. Any suggestions on how to fix this?
Was This Post Helpful? 0
  • +
  • -

#4 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 323
  • Joined: 12-January 11

Re: DJIA Histogram VB

Posted 17 October 2011 - 12:41 PM

There are a few suggestions you may want to consider.

First, (1-0) will ALWAYS equal 1 and (-0) doesn't do anything at all, so you can simplify your function to to
'y - 0 = y
'1 - 0 = 1
'(1 - y) / 1 = (1 - y)

Function ConvertY(ByVal y As Double) As Integer
            Return (1 - y) * Graph.Height
End Function


Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9393
  • View blog
  • Posts: 35,273
  • Joined: 12-June 08

Re: DJIA Histogram VB

Posted 17 October 2011 - 12:43 PM

What's the value you are putting in?

Your 'convertx' sure has a possibility to be a divide by zero... you know, when numbins = 0.

Function ConvertX(ByVal x As Double) As Integer
Return ((x - 0) / (NumBins - 0)) * Graph.Width
End Function



The math doesn't seem to work out here.. why have (1-0)? That will always be 1... and any number divided by 1 is just that number.. so you can eliminate that.

Function ConvertY(ByVal y As Double) As Integer
Return (1 - (y - 0) / (1 - 0)) * Graph.Height
End Function

Was This Post Helpful? 0
  • +
  • -

#6 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 323
  • Joined: 12-January 11

Re: DJIA Histogram VB

Posted 17 October 2011 - 12:52 PM

View Postmodi123_1, on 17 October 2011 - 12:43 PM, said:

What's the value you are putting in?

Your 'convertx' sure has a possibility to be a divide by zero... you know, when numbins = 0.

Function ConvertX(ByVal x As Double) As Integer
Return ((x - 0) / (NumBins - 0)) * Graph.Width
End Function



The math doesn't seem to work out here.. why have (1-0)? That will always be 1... and any number divided by 1 is just that number.. so you can eliminate that.

Function ConvertY(ByVal y As Double) As Integer
Return (1 - (y - 0) / (1 - 0)) * Graph.Height
End Function


I agree, you may want to just change your ConvertX at the same time to
If Not NumBins = 0 then
 Return (x / NumBins) * Graph.Width
Else
 'handle situations where NumBins = 0
End If


This post has been edited by Psyguy: 17 October 2011 - 12:53 PM

Was This Post Helpful? 0
  • +
  • -

#7 fatcat22222  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-October 11

Re: DJIA Histogram VB

Posted 17 October 2011 - 03:00 PM

Thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1