Xem mẫu

Problem Solving 153 RUN-TIME ERRORS Run-time errors are those that often cause our programs to terminate. Examples of logic errors that can turn into run-time errors are divide-by-zero exceptions and array index out-of-range exceptions. Other run-time errors may arise when we attempt to connect to a database, open a file, or send an XML message, where errors beyond our control disrupt the flow of our program. What can be especially annoying about run-time errors is that they may not show up the first time, or even the first ten times, we execute a program—but only on the eleventh time. That is to say, a specific run-time error may occur only when a certain sequence of events takes place. To deal with some potentially unavoidable run-time errors, we can create exception handlers (blocks of code) to resolve or handle errors in our programs and allow it to continue. In order to demonstrate these different types of errors, we will need an example program. FORECASTING COVARIANCE Covariances between assets play an important part of many automated trading and risk management systems. As shown in Chapter 8, correlations and covariances are calculated using historical price data. But covariances can also be updated and forecast using GARCH methodologies since covariance rates often exhibit mean reversion. One GARCH approach forecasts covari-ances thusly: stþ1,i, j ¼ (1 ÿ a ÿ b) C þ art,irt, j þ bs^t,i, j and stþn,i, j ¼ C þ (a þ b)jÿ1 (s^tþ1,i, j ÿ C) where C is the long-run covariance. Now let’s create a short program to forecast the covariance between two stocks over the next 20 days. Step 1 In VB.NET start a new Windows application named CovarForecast. Team-LRN 154 Introduction to VB.NET Step 2 On Form1, add a single text box with the multiline property changed to True. Step 3 In the Project menu bar item, select Add Class. You can leave the file name as the default Class1.vb. Step 4 In the Class1 code window, change the class name to CovarForecast and add the following code: Public Class CovarForecast Private dblForecasts As Double() Private dblAlpha As Double Private dblBeta As Double Private dblPrevForecast As Double Private dblCovariance As Double Public Sub New() dblForecasts = New Double(20) { } dblPrevForecast = 0.00022627 dblCovariance = 0.000205927 dblAlpha = 0.1943 dblBeta = 0.5274 CalcForecasts() End Sub 0 Long Run Covariance 0 Optimized coefficient 0 Optimized coefficient Private Sub CalcForecasts() Dim j As Integer Dim newIBMreturn# = 0.0232 Dim newMSFTreturn# = 0.0352 dblForecasts(1) = (1 - dblAlpha - dblBeta) * dblCovariance + _ dblAlpha * newIBMreturn * newMSFTreturn + _ dblBeta * dblPrevForecast For j = 2 To 20 dblForecasts(j) = dblCovariance + dblAlpha + dblBeta ^ _ (j - 1) * (dblForecasts(1) - dblCovariance) Next j End Sub Public Function GetForecasts() As Double() Return dblForecasts End Function End Class End Sub As with most classes, our CovarForecast class has several Private member variables and a constructor function. Also the CovarForecast class has a Private subroutine CalcForecasts() and a Public method GetForecasts(). In the constructor method, wesetthe valuesofthe appropriate variables including the long run covariance, the optimized values of alpha and beta, and the previous 1-day-ahead forecast. Within the CalcForecasts() subroutine, we receive new data about our two stocks, IBM and MSFT. Namely, a big up day in the market has Team-LRN Problem Solving 155 raised both boats significantly, and consequently the historical correlation will increase. However, over the long term, we expect the correlation to revert to the mean, as we will see in our forecasts. Step 5 Back in the Form1 code window, add the following code in the Form1_Load event: Private Sub Form1_Load(ByVal sender As ...) Handles MyBase.Load Dim x As Integer Dim myForecasts As Double() Dim myCovars As CovarForecast myCovars = New CovarForecast() myForecasts = myCovars.GetForecasts() For x = 1 To myForecasts.GetUpperBound(0) TextBox1.Text &= x & " day ahead forecast: " & vbTab & _ Format(myForecasts(x), "0.0000000") & vbCrLf Next x End Sub In the Form1_Load event, we have created a CovarForecast object named myCovars. Once we instantiate an object based upon the CovarForecast class, the constructor method performs all the calculations and places the forecasted values into an array. We call the GetForecasts method to retrieve this array and loop through the elements to print the values in the text box. Step 6 Run the program (see Figure 9.1). If you copied the code correctly, your program will run. However, the results you got were not the same as shown in Figure 9.1. We have devilishly hidden a logic error in the code. But first let’s examine the syntax. The program code above contains no syntax errors. So we will create one and see what happens. Step 7 In the first line of the Form1_Load event, purposely misspell myCovars as myCobars. Dim myCobars As New CovarForecast() Notice that in your code the reference to the correctly spelled object myCovars is now underlined in blue. If we attempt to compile the program, a build error will occur which will be described in the Task List window. Double-clicking on this error message in the Task List window will take you right to the line of Team-LRN 156 Introduction to VB.NET F I G U R E 9.1 code containing the error, as shown in Figure 9.2. Syntax errors such as this are common and easily fixed. Logic errors are much more difficult to root out. If we had not provided a picture showing the correct results, how would we know there is a problem in the program? With no method for verifying our calculations, we are lost. As discussed in the methodology presented in Chapter 2, all complex calculations should first be modeled in Excel before conversion to programming code. Figure 9.3 demonstrates the prototyping of this model in spreadsheet format. If we know that the spreadsheet calculations were done properly, it is clear that our coded formulas are incorrect. Focusing on the lines containing the Team-LRN Problem Solving 157 F I G U R E 9.2 F I G U R E 9.3 Team-LRN ... - tailieumienphi.vn
nguon tai.lieu . vn