64 views

Uploaded by mk3chan

algebra

- Var - Monte Carlo Simulation
- PCA Biology
- Revision for Java Programming I Basic
- Engineering Models 2 Homework 1
- Sigma 6 Plenty
- Sieg Ram08
- Computing Applications in Mechanical Systems (MATLAB Simulink)
- Vibration 2
- From Algorithms to Z-Scores
- Blindly Combined Energy Detection for Spectrum Sensing in Cognitive Radio
- Case Study- Risk & Return
- PROJECT SYNOPSIS Team anDroid-1.docx
- Cpp Programs
- 31 MultivariateAnalysis.pdf
- Lab 5 - Elementary Statistics With R_matlab_numerical Measures
- rpp transformasi
- proj09
- A Study on Suicide problem using Combined Overlap Block Neutrosophic Cognitive Maps
- Syllabus Iit
- Output

You are on page 1of 9

bas

Attribute VB_Name = "Algebra" '---------------------------------------------------------------'Leonid Sopotnitskiy, 2nd January 2013 'Certificate in Quantitative Finance 'Final Project: kth to default CDS pricing by Copula '---------------------------------------------------------------'Please see comments containing descriptions of numerical 'techniques implemented in the task. 'All variables are declared explicitly. Option Explicit

'-----------------------------------------------' MEAN CALCULATION CODE '-----------------------------------------------Function Mean(rng As Variant) As Variant Dim Arr As Variant 'array containing the input values Dim I As Integer, n As Integer, temp_mean As Double ' i - row index ' n - total number of rows in array ' temp_maen - dummy sum of values within the array Arr = rng I = 1 n = UBound(Arr, 1) 'I now enter a loop to sum up all the values in the array While I <= n temp_mean = temp_mean + Arr(I, 1) I = I + 1 Wend Mean = temp_mean / n 'the function returns the value of the mean End Function '-----------------------------------------------' VARIANCE CALCULATION CODE '-----------------------------------------------Function Variance(rng As Variant) As Variant Dim Arr As Variant Dim mu As Double, temp_var As Double Dim I As Integer, n As Integer Arr = rng 'array containing the input values mu = Mean(Arr) 'calculated mean n = UBound(Arr, 1) 'total number of rows in array 'this loop calculates the variance of the input values For I = 1 To n

-1-

C:\Users\chanmu\Desktop\vba\kbasket\Algebra.bas

temp_var = temp_var + (Arr(I, 1) - mu) ^ 2 / n Next I Variance = temp_var 'the last coefficient is for scaling purposes End Function '-----------------------------------------------' COVARIANCE CALCULATION CODE ' this algorithm is required to calculate the ' elementwise covariances that will be placed off ' the diagonal in the covariance matrix '-----------------------------------------------Function Covariance(x As Variant, y As Variant) As Variant Dim ArrX As Variant, ArrY As Variant Dim muX As Double, muY As Double, temp_covar As Double Dim I As Integer, n As Integer muX = Mean(x) ' calculate the mean of array 1 muY = Mean(y) ' calculate the mean of array 2 ArrX = x ArrY = y n = UBound(ArrX, 1) ' since the arrays should be of the same size, I will use a single variable to ' store the number of values in them 'the following loop calculates the covariance of the input data For I = 1 To n temp_covar = temp_covar + ((ArrX(I, 1) - muX) * (ArrY(I, 1) - muY)) Next I Covariance = (temp_covar / n) 'the last coefficient is used for scaling factors End Function '--------------------------------------------------' COVARIANCE MATRIX CALCULATION CODE ' this function generates a covariance matrix ' and implements the Variance & Covariance functions ' coded in this module '--------------------------------------------------Function CovarianceMatrix(rng As Variant) As Variant Dim Dim I = j = ArrX As Variant, ArrY As Variant, CMtx As Variant I As Integer, j As Integer, n As Integer rng.rows.Count rng.columns.Count

ReDim CVMtx(1 To j, 1 To j) ' the output is a jxj symmetric matrix ' with variances stored on the diagonal and ' covariances placed off the diagonal. 'ArrX & ArrY store different vectors of data that is required to calculate 'covariance ReDim ArrX(1 To I, 1) ReDim ArrY(1 To I, 1)

-2-

C:\Users\chanmu\Desktop\vba\kbasket\Algebra.bas

'the following algorithm calculates and places the variances and covariances 'of the input data For j = 1 To rng.columns.Count For n = 1 To rng.rows.Count ArrX(n, 1) = rng(n, j) Next n For I = 1 To rng.columns.Count For n = 1 To rng.rows.Count ArrY(n, 1) = rng(n, I) Next n If I = j Then CVMtx(I, j) = Variance(ArrX) 'the diagonal of the matrix contains variances Else CVMtx(I, j) = Covariance(ArrX, ArrY) 'off-diagonal values are covariances End If Next I Next j CovarianceMatrix = CVMtx 'output End Function ' The next 2 functions were done for testing purposes: ' in order to build a covariance matrix, we could also ' create a correlation matrix P and a variance matrix ' S, that would contain variances on the diagonal and ' zeros everywhere else. We could then apply the following formula: ' ' Covariance Matrix = S^0.5 * P * S^0.5 (P. Wilmott on Quantitative Finance Vol. 1, Ch. 11, p.184) ' ' or in VBA it would be: ' ' CovarianceMatrix = MtxMultiply(MtxMultiply(VarianceMatrix(rng) _ ^ 0.5, CorrelationMatrix(rng)), VarianceMatrix(rng) ^ 0.5) ' ' the result would be the same. '-----------------------------------------------' VARIANCE MATRIX CALCULATION CODE ' this function returns a variance matrix, i.e. ' it contains zeros off its diagonal. '-----------------------------------------------Function VarianceMatrix(rng As Variant) As Variant Dim matrix As Variant, VMtx As Variant Dim I As Integer, j As Integer, n As Integer I = rng.rows.Count j = rng.columns.Count ReDim VMtx(1 To j, 1 To j) 'the output will be a symmetric jxj matrix ReDim matrix(1 To I, 1) 'this matrix will store individual columns

-3-

C:\Users\chanmu\Desktop\vba\kbasket\Algebra.bas

'The following loops build the variance matrix. For j = 1 To rng.columns.Count For I = 1 To rng.columns.Count If I = j Then For n = 1 To rng.rows.Count matrix(n, 1) = rng(n, j) 'columns are copied into separate arrays Next n VMtx(I, j) = Variance(matrix) 'the variance of values contained in the input array Else VMtx(I, j) = 0 'off-diagonal elements are 0, as we are not interested in covariance here End If Next I Next j VarianceMatrix = VMtx 'output End Function '------------------------------------------------------' A simple linear correlation function, ' calculating correlation between 2 variables / arrays '------------------------------------------------------Function Correlation(a As Variant, B As Variant) As Variant Dim ArrA As Variant, ArrB As Variant Dim I As Integer If TypeName(a) = "Range" Then I = a.rows.Count Else I = UBound(a, 1) End If ReDim ArrA(1 To I, 1) 'ArrX & ArrY store various arrays which ReDim ArrB(1 To I, 1) 'will be analyzed for correlation. ArrA = a ArrB = B Correlation = Covariance(ArrA, ArrB) / ((Variance(ArrA) * Variance(ArrB)) ^ 0.5) End Function '-----------------------------------------------' CORRELATION CALCULATION CODE ' this function produces a matrix with calculated ' correlations of vectors of the input array. '-----------------------------------------------Function CorrelationMatrix(rng As Variant) As Variant Dim ArrX As Variant, ArrY As Variant, CMtx As Variant Dim I As Integer, j As Integer, n As Integer I = rng.rows.Count j = rng.columns.Count ReDim CMtx(1 To j, 1 To j) ReDim ArrX(1 To I, 1) 'ArrX & ArrY store various arrays which ReDim ArrY(1 To I, 1) 'will be analyzed for correlation.

-4-

C:\Users\chanmu\Desktop\vba\kbasket\Algebra.bas

' The following loop performs an element-wise calculation of correlations ' by switching vectors. For j = 1 To rng.columns.Count For n = 1 To rng.rows.Count ArrX(n, 1) = rng(n, j) Next n For I = 1 To rng.columns.Count For n = 1 To rng.rows.Count ArrY(n, 1) = rng(n, I) Next n If I = j Then CMtx(I, j) = 1 'the diagonal of the matrix should contain 1's Else ' correlation is computed in this part of the code CMtx(I, j) = Covariance(ArrX, ArrY) / ((Variance(ArrX) * Variance(ArrY)) ^ 0.5) End If Next I Next j CorrelationMatrix = CMtx 'output End Function '-----------------------------------------------' TRANSPOSE MATRIX CODE ' this function produces a transposed matrix. '-----------------------------------------------Function Transpose(a As Variant) As Variant Dim I As Integer, j As Integer, AT As Variant I = a.rows.Count j = a.columns.Count ReDim AT(1 To j, 1 To I) 'setting the dimensions for the target matrix

While I > 0 j = a.columns.Count While j > 0 AT(j, I) = a(I, j) ' remapping of matrix elements j = j - 1 Wend I = I - 1 Wend Transpose = AT 'output End Function '-----------------------------------------------' MATRIX MULTIPLICATION CODE ' this function multiplies 2 matrices '-----------------------------------------------Function MtxMultiply(a As Variant, B As Variant) As Variant

-5-

C:\Users\chanmu\Desktop\vba\kbasket\Algebra.bas

Dim ia As Integer, ja As Integer, ib As Integer, jb As Integer Dim I As Integer, j As Integer, z As Integer Dim temp_Mtx As Variant, v1 As Variant, v2 As Variant ia ja ib jb = = = = a.rows.Count ' #rows in A a.columns.Count ' #columns in A B.rows.Count ' #rows in B B.columns.Count ' #columns in B

'creating dimensions for the output product matrix ReDim temp_Mtx(1 To ia, 1 To jb) 'the following code does element-wise multiplication of matrix A and B For I = 1 To ia For j = 1 To jb For z = 1 To ja temp_Mtx(I, j) = temp_Mtx(I, j) + a(I, z) * B(z, j) Next z Next j Next I MtxMultiply = temp_Mtx 'output End Function '-----------------------------------------------------------------------' Cholesky Decomposition coded in the following function ' Returns the lower matrix A (M = A x A_transposed) '-----------------------------------------------------------------------Function cholesky(matrix As range) Dim I As Integer, j As Integer, k As Integer, n As Integer Dim a() As Double Dim element As Double Dim L_Lower() As Double n = matrix.columns.Count ReDim a(1 To n, 1 To n) ReDim L_Lower(1 To n, 1 To n) 'copying values into array For I = 1 To n For j = 1 To n a(I, j) = matrix(I, j).Value L_Lower(I, j) = 0 Next j Next I For I = 1 To n For j = 1 To n element = a(I, j) For k = 1 To I - 1 element = element - L_Lower(I, k) * L_Lower(j, k) Next k If I = j Then L_Lower(I, I) = Sqr(element)

-6-

C:\Users\chanmu\Desktop\vba\kbasket\Algebra.bas

ElseIf I < j Then L_Lower(j, I) = element / L_Lower(I, I) End If Next j Next I cholesky = L_Lower End Function 'Spearman's Rho ranked correlation matrix Function RhoMatrix(rng As Variant) As Variant

Dim ArrX As Variant, ArrY As Variant, CMtx As Variant Dim I As Integer, j As Integer, n As Integer I = rng.rows.Count j = rng.columns.Count ReDim CMtx(1 To j, 1 To j) ReDim ArrX(1 To I, 1) 'ArrX & ArrY store various arrays which ReDim ArrY(1 To I, 1) 'will be analyzed for correlation. ' The following loop performs an element-wise calculation of correlations ' by switching vectors. For j = 1 To rng.columns.Count For n = 1 To rng.rows.Count ArrX(n, 1) = rng(n, j) Next n For I = 1 To rng.columns.Count For n = 1 To rng.rows.Count ArrY(n, 1) = rng(n, I) Next n If I = j Then CMtx(I, j) = 1 'the diagonal of the matrix should contain 1's Else ' correlation is computed in this part of the code CMtx(I, j) = SpearmanRho(ArrX, ArrY) End If Next I Next j RhoMatrix = CMtx 'output End Function 'Spearman's Rho ranked correlation Function SpearmanRho(x As Variant, y As Variant) As Variant SpearmanRho = Correlation(RankIndex(x), RankIndex(y)) End Function 'Determination of ranks of data Function RankIndex(x As Variant) As Variant Dim I As Integer, j As Integer, n As Integer Dim test As Variant Dim Rank As Variant If TypeName(x) = "Range" Then n = x.rows.Count

-7-

C:\Users\chanmu\Desktop\vba\kbasket\Algebra.bas

Else n = UBound(x, 1) End If ReDim Rank(1 To n, 1 To 1) 'The algorithm that I use is the following: 'each row in the output matrix is assigned a single value - the number of input rows 'and then each element goes through a pair-wise comparison and each time there is an element 'which is larger, the rank assigned to this element is being reduced by 1. For I = 1 To n Rank(I, 1) = n test = x(I, 1) For j = 1 To n If test < x(j, 1) Then Rank(I, 1) = Rank(I, 1) - 1 End If Next j Next I RankIndex = Rank End Function 'KENDALL's TAU function Public Function KendallTau(xRange As range, yRange As range) As Double Dim n As Long Dim xRange_Array() As Double Dim yRange_Array() As Double n = xRange.rows.Count ReDim xRange_Array(1 To n) ReDim yRange_Array(1 To n) Dim I As Long, j As Long, acc As Double acc = 0 For I = 1 To n For j = I To n If I = 1 Then xRange_Array(j) = xRange.Cells(j) yRange_Array(j) = yRange.Cells(j) End If acc = acc + Sgn((xRange_Array(I) - xRange_Array(j)) * (yRange_Array(I) yRange_Array(j))) Next j Next I KendallTau = acc * 2 / n / (n - 1) End Function 'returns the maximum of 2 values Function MAXIMUM(a As Variant, B As Variant) As Variant If a > B Then MAXIMUM = a Else

-8-

C:\Users\chanmu\Desktop\vba\kbasket\Algebra.bas

MAXIMUM = B End If End Function 'linear interpolation which will be used to interpolate the discount factors 'when calculating the CDO leg premiums. Function interpolateDF(y As Variant, x As Variant) As Variant Dim I As Integer Dim B As Integer Dim n As Integer If x = 0 Then interpolateDF = 0 Exit Function End If If TypeName(y) = "Range" Then n = y.rows.Count Else n = UBound(y, 1) End If

For I = 1 To n If x < I And x > 0 Then If I = 1 Then interpolateDF = 1 Exit Function ElseIf I > 1 Then interpolateDF = y(I - 1) + (y(I) - y(I - 1)) * (CDbl(x) - (I - 1)) Exit Function End If End If Next I End Function

-9-

- Var - Monte Carlo SimulationUploaded byKomang62
- PCA BiologyUploaded byRashedul Islam
- Revision for Java Programming I BasicUploaded byRyan Tang
- Engineering Models 2 Homework 1Uploaded byBill Williams
- Sigma 6 PlentyUploaded bySathish Mano
- Sieg Ram08Uploaded byAna-Maria Crisan
- Computing Applications in Mechanical Systems (MATLAB Simulink)Uploaded bylawlaw
- Vibration 2Uploaded byAnonymous DKv8vp
- From Algorithms to Z-ScoresUploaded bybooklover2
- Blindly Combined Energy Detection for Spectrum Sensing in Cognitive RadioUploaded byMohamed Tharwat Waheed
- Case Study- Risk & ReturnUploaded bySomya
- PROJECT SYNOPSIS Team anDroid-1.docxUploaded byNisha Pandita
- Cpp ProgramsUploaded byKimmi Sharma
- 31 MultivariateAnalysis.pdfUploaded byIJAERS JOURNAL
- Lab 5 - Elementary Statistics With R_matlab_numerical MeasuresUploaded bySam Caunca Ortiz
- rpp transformasiUploaded byAlvin MaviaLover
- proj09Uploaded byumeshamy
- A Study on Suicide problem using Combined Overlap Block Neutrosophic Cognitive MapsUploaded byAnonymous 0U9j6BLllB
- Syllabus IitUploaded byVishwesh Ravi Shrimali
- OutputUploaded byNienies Nurika
- 01144013Uploaded byBhargav Bikkani
- Z88 Aurora Theory GuideUploaded bybacabacabaca
- Counting in Two WaysUploaded bysrinu27
- ArticleMoments and Semi-Moments for fuzzy portfolios selectionUploaded bycrinaus2003
- Mathematics Specialist 3C3D Calc Assumed Ratified Marking Key 2015(1)Uploaded byDark Lord
- 2-D Array WS APCSUploaded by'Amir Dailamy
- IJETTCS-2012-11-20-018Uploaded byAnonymous vQrJlEN
- UCLA Math 33A syllabusUploaded byzmy8686
- JEE Syllabus n CAT SyllabusUploaded byvenkat6299
- p557-mckay[1]Uploaded bynewcastle74

- Financial Daily -- FDsetia_20150720txxwbr.pdfUploaded byJaneang33
- Temp UploadUploaded bymk3chan
- 何振球、古希萍等组织、领导传销活动罪二审刑事裁定书Uploaded bymk3chan
- 12345Uploaded bymk3chan
- Measurement System Software Callibration TablesUploaded bymk3chan
- Content ServerUploaded bymk3chan
- Canada Yield CurveUploaded bymk3chan
- Market Based Estimates of Expected Future UK Output GrowthUploaded bymk3chan
- Settings (Awesome Note) - Do Not Delete This NoteUploaded bymk3chan
- WeibayesUploaded bymk3chan
- Notes on the Bofe Uk Yield Curvesv2Uploaded bymk3chan
- AugustinUploaded bymk3chan
- BDS dataUploaded bymk3chan
- Create or Review a QuizUploaded bymk3chan
- Is This Answer Reasonable_ _ Financial Exam Help 123Uploaded bymk3chan
- Wilmott Forums puzzle Why copula.pdfUploaded bymk3chan
- 01 Capital BudgetingUploaded byUsama Farooq
- Nial Fuller LearnToTradeTheMarket.com New Course NoviceUploaded bymk3chan
- Singapore Jobs _ Securities Settlement Analyst_3 Months Contract - Morgan McKinley _ JobsDB SingaporeUploaded bymk3chan
- Invesment BankerrrrrUploaded bymk3chan
- Bootstrapping Default Probabilities From CDS QuotesUploaded bymk3chan
- The Elevator Speech & Networking.pdfUploaded bymk3chan
- bbac投资组合管理bloomberg说明dffdfdddddUploaded bymk3chan
- MT19937.pdfUploaded bymk3chan
- Default.pdfUploaded bymk3chan
- bbac bu.xlsfsssssUploaded bymk3chan
- AlanWire QualityManual Rev BUploaded byMuhammad Sadiq
- Timothy Crack PaperUploaded bymk3chan
- Ch12HullOFOD7thEd.pptUploaded bymk3chan
- Section4.11measuretheoryUploaded bymk3chan

- resume online - caitlin gibbonsUploaded byapi-216990272
- Flame Ionization Detector - Wikipedia, The Free EncyclopediaUploaded byandrew jachson labito
- Self-management SkillsUploaded byHudda Riaz
- Civil3D2016ProPack3README.docxUploaded byOscar Rodríguez
- Sensorless Control of 5 Phase BLCD MotorUploaded byschlemihl69
- Brahms.emu.Edu.tr Rza Is515week7Uploaded byTâmThiệnNguyễn
- Expression of Serum Exosomal MicroRNA-21 in Human Hepatocellular Carcinoma.pdfUploaded bySorop Andrei
- Gold Star Chilli CaseUploaded byavd_desai20056286
- MD/TOTCO INDEX_221900_rev BUploaded byTun Zaw Thein
- 5Uploaded byapi-3700286
- Elaboration of maps for climatic and seismic actions for structural design with the EurocodesUploaded byEnmus
- nov 18 to nov layoutUploaded byapi-239208620
- Zero Suicide Workplan Template 12.6.17Uploaded bySara Bote González
- Friends Union for Energising Lives (FUEL) _ SamhitaUploaded byKhalid Mohd
- Hierarchical Recurrent Neural Hashing for Image Retrieval With Hierarchical Convolutional FeaturesUploaded byLeMenizInfotech
- Gui Example n1Uploaded bykkitouchi
- g2 c09 reteach enrichUploaded byapi-292935504
- 2007_SPIE_absUploaded byFabrizio Ravegnani
- Workshop Tennis Racket[1]Uploaded bytaniapaloma
- S030-30-K010 DTC CODE Z010Uploaded byPHÁT NGUYỄN THẾ
- AIOU Computer Sciences (First Year)Uploaded byAli Prince
- Memory Facilities Within the FederationUploaded byDamon Bradley
- [MS-BCP]Uploaded byficken66
- WO06039455A1Uploaded byhollowstix
- NCHRP 03-115 - Uninterrupted Flow - WP 4 - Freeway Method Integration - 20140415Uploaded byAkhilesh Kumar Maurya
- Statistics for ManagementUploaded byNehaCruise
- ABD701Uploaded bysumit
- MGMT1001-Finals-Notes.pdfUploaded byYansong Liu
- project management planUploaded byapi-282948612
- Erlang in ProductionUploaded byDhanesh Kumar Kasinathan