~ubuntu-branches/ubuntu/trusty/fportfolio/trusty

« back to all changes in this revision

Viewing changes to R/PortfolioClassExtractors.R

  • Committer: Bazaar Package Importer
  • Author(s): Dirk Eddelbuettel
  • Date: 2008-12-04 11:36:54 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20081204113654-gr832nfs44blp5ph
Tags: 280.74-1
* New upstream release
* Finally uploading as r-cran-rglpk is out of NEW after five weeks

* debian/control: Updated (Build-)Depends: and Suggests:

* debian/control: Set (Build-)Depends: to current R version
* debian/control: Set Standards-Version: to current version

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
# This library is free software; you can redistribute it and/or
3
 
# modify it under the terms of the GNU Library General Public
4
 
# License as published by the Free Software Foundation; either
5
 
# version 2 of the License, or (at your option) any later version.
6
 
#
7
 
# This library is distributed in the hope that it will be useful,
8
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR Description. See the 
10
 
# GNU Library General Public License for more details.
11
 
#
12
 
# You should have received a copy of the GNU Library General 
13
 
# Public License along with this library; if not, write to the 
14
 
# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
15
 
# MA 02111-1307 USA
16
 
 
17
 
# Copyrights (C)
18
 
# for this R-port: 
19
 
#   1999 - 2007, Diethelm Wuertz, GPL
20
 
#   Diethelm Wuertz <wuertz@itp.phys.ethz.ch>
21
 
#   info@rmetrics.org
22
 
#   www.rmetrics.org
23
 
# for the code accessed (or partly included) from other R-ports:
24
 
#   see R's copyright and license files
25
 
# for the code accessed (or partly included) from contributed R-ports
26
 
# and other sources
27
 
#   see Rmetrics's copyright file
28
 
 
29
 
 
30
 
################################################################################
31
 
# FUNCTION:                     PORTFOLIO DATA EXTRACTORS:
32
 
#  getData                       Extracts data slot from a 'fPORTFOLIO' object
33
 
#   getSeries                    Extracts series from a 'fPORTFOLIO' object
34
 
#   getStatistics                Extracts statistics from a 'fPORTFOLIO' object
35
 
#   getNumberOfAssets            Extracts number of assets from a 'fPORTFOLIO' object
36
 
#  getSpec                      PORTFOLIO SPEC EXTRACTORS:
37
 
#   getType
38
 
#   getEstimator
39
 
#   getParams 
40
 
#   getSolver
41
 
#   getTrace
42
 
#  getConstraints               PORTFOLIO CONSTRAINTS EXTRACTORS:
43
 
#  getPortfolio
44
 
#   getWeights
45
 
#   getTargetReturn
46
 
#   getTargetRisk
47
 
#   getTargetAlpha
48
 
#   getRiskFreeRate
49
 
#   getNFrontierPoints
50
 
#   getStatus
51
 
# FUNCTION:                     GENERAL EXTRACTORS:
52
 
#  getFrontier
53
 
#  getCovRiskBudgets
54
 
#  getTailRiskBudgets
55
 
################################################################################
56
 
 
57
 
 
58
 
################################################################################
59
 
# fPORTFOLIO - S4
60
 
 
61
 
    #   call = "call",
62
 
    #   data = "list",
63
 
    #   specification = "list",
64
 
    #   constraints = "character",
65
 
    #   portfolio = "list",
66
 
    #   title = "character",
67
 
    #   description = "character")  
68
 
 
69
 
 
70
 
# ------------------------------------------------------------------------------
71
 
 
72
 
 
73
 
getData.fPORTFOLIO =
74
 
function(object)
75
 
{   # A function implemented by Rmetrics
76
 
 
77
 
    # Description:
78
 
    #   Extracts data slot from a 'fPORTFOLIO' object
79
 
    
80
 
    # Arguments:
81
 
    #   object - an object of S4 class fPORTFOLIO as returned by the
82
 
    #       functions *Portfolio().
83
 
    
84
 
    # FUNCTION:
85
 
    
86
 
    # Get Specification:
87
 
    ans = object@data$data
88
 
    
89
 
    # Return Value:
90
 
    ans  
91
 
}
92
 
 
93
 
 
94
 
# ------------------------------------------------------------------------------
95
 
 
96
 
 
97
 
getSeries.fPORTFOLIO =
98
 
function(object)
99
 
{   # A function implemented by Rmetrics
100
 
 
101
 
    # Description:
102
 
    #   Extracts series from a 'fPORTFOLIO' object
103
 
    
104
 
    # Arguments:
105
 
    #   object - an object of S4 class fPORTFOLIO as returned by the
106
 
    #       functions *Portfolio().
107
 
    
108
 
    # FUNCTION:
109
 
    
110
 
    # Get Specification:
111
 
    ans = getSeries(getData(object))
112
 
    
113
 
    # Return Value:
114
 
    ans  
115
 
}
116
 
 
117
 
 
118
 
# ------------------------------------------------------------------------------
119
 
 
120
 
 
121
 
getStatistics.fPORTFOLIO =
122
 
function(object)
123
 
{   # A function implemented by Rmetrics
124
 
 
125
 
    # Description:
126
 
    #   Extracts statistics from a 'fPORTFOLIO' object
127
 
    
128
 
    # Arguments:
129
 
    #   object - an object of S4 class fPORTFOLIO as returned by the
130
 
    #       functions *Portfolio().
131
 
    
132
 
    # FUNCTION:
133
 
    
134
 
    # Get Specification:
135
 
    ans = getStatistics(getData(object))
136
 
    
137
 
    # Return Value:
138
 
    ans  
139
 
}
140
 
 
141
 
 
142
 
# ------------------------------------------------------------------------------
143
 
 
144
 
 
145
 
getNumberOfAssets.fPORTFOLIO =
146
 
function(object)
147
 
{   # A function implemented by Rmetrics
148
 
 
149
 
    # Description:
150
 
    #   Extracts number of assets from a 'fPORTFOLIO' object
151
 
    
152
 
    # Arguments:
153
 
    #   object - an object of S4 class fPORTFOLIO as returned by the
154
 
    #       functions *Portfolio().
155
 
    
156
 
    # FUNCTION:
157
 
    
158
 
    # Get Specification:
159
 
    ans = getNumberOfAssets(getData(object))
160
 
    
161
 
    # Return Value:
162
 
    ans  
163
 
}
164
 
 
165
 
 
166
 
# ------------------------------------------------------------------------------
167
 
 
168
 
 
169
 
getSpec.fPORTFOLIO =
170
 
function(object)
171
 
{   # A function implemented by Rmetrics
172
 
 
173
 
    # Description:
174
 
    #   Extracts the specification slot from a 'fPORTFOLIO' object
175
 
    
176
 
    # Arguments:
177
 
    #   object - an object of S4 class fPORTFOLIO as returned by the
178
 
    #       functions *Portfolio().
179
 
    
180
 
    # FUNCTION:
181
 
    
182
 
    # Get Specification:
183
 
    ans = object@spec$spec
184
 
    
185
 
    # Return Value:
186
 
    ans  
187
 
}
188
 
 
189
 
 
190
 
# ------------------------------------------------------------------------------
191
 
 
192
 
 
193
 
getType.fPORTFOLIO =
194
 
function(object)
195
 
{   # A function implemented by Rmetrics
196
 
 
197
 
    # Description:
198
 
    #   Extracts the specification-type slot from a 'fPORTFOLIO' object
199
 
    
200
 
    # Arguments:
201
 
    #   object - an object of S4 class fPORTFOLIO as returned by the
202
 
    #       functions *Portfolio().
203
 
    
204
 
    # FUNCTION:
205
 
    
206
 
    # Get Specification:
207
 
    ans = getType(getSpec(object))
208
 
    
209
 
    # Return Value:
210
 
    ans  
211
 
}
212
 
 
213
 
 
214
 
# ------------------------------------------------------------------------------
215
 
 
216
 
 
217
 
getEstimator.fPORTFOLIO =
218
 
function(object)
219
 
{   # A function implemented by Rmetrics
220
 
 
221
 
    # Description:
222
 
    #   Extracts the specification-estimator slot from a 'fPORTFOLIO' object
223
 
    
224
 
    # Arguments:
225
 
    #   object - an object of S4 class fPORTFOLIO as returned by the
226
 
    #       functions *Portfolio().
227
 
    
228
 
    # FUNCTION:
229
 
    
230
 
    # Get Specification:
231
 
    ans = getEstimator(getSpec(object))
232
 
    
233
 
    # Return Value:
234
 
    ans  
235
 
}
236
 
 
237
 
 
238
 
# ------------------------------------------------------------------------------
239
 
 
240
 
 
241
 
getParams.fPORTFOLIO =
242
 
function(object)
243
 
{   # A function implemented by Rmetrics
244
 
 
245
 
    # Description:
246
 
    #   Extracts the specification-params slot from a 'fPORTFOLIO' object
247
 
    
248
 
    # Arguments:
249
 
    #   object - an object of S4 class fPORTFOLIO as returned by the
250
 
    #       functions *Portfolio().
251
 
    
252
 
    # FUNCTION:
253
 
    
254
 
    # Get Specification:
255
 
    ans = getParams(getSpec(object))
256
 
    
257
 
    # Return Value:
258
 
    ans  
259
 
}
260
 
 
261
 
 
262
 
# ------------------------------------------------------------------------------
263
 
 
264
 
 
265
 
getSolver.fPORTFOLIO =
266
 
function(object)
267
 
{   # A function implemented by Rmetrics
268
 
 
269
 
    # Description:
270
 
    #   Extracts the specification-solver slot from a 'fPORTFOLIO' object
271
 
    
272
 
    # Arguments:
273
 
    #   object - an object of S4 class fPORTFOLIO as returned by the
274
 
    #       functions *Portfolio().
275
 
    
276
 
    # FUNCTION:
277
 
    
278
 
    # Get Specification:
279
 
    ans = getSolver(getSpec(object))
280
 
    
281
 
    # Return Value:
282
 
    ans  
283
 
}
284
 
 
285
 
 
286
 
# ------------------------------------------------------------------------------
287
 
 
288
 
 
289
 
getTrace.fPORTFOLIO =
290
 
function(object)
291
 
{   # A function implemented by Rmetrics
292
 
 
293
 
    # Description:
294
 
    #   Extracts the specification-trace slot from a 'fPORTFOLIO' object
295
 
    
296
 
    # Arguments:
297
 
    #   object - an object of S4 class fPORTFOLIO as returned by the
298
 
    #       functions *Portfolio().
299
 
    
300
 
    # FUNCTION:
301
 
    
302
 
    # Get Specification:
303
 
    ans = getTrace(getSpec(object))
304
 
    
305
 
    # Return Value:
306
 
    ans  
307
 
}
308
 
 
309
 
 
310
 
# ------------------------------------------------------------------------------
311
 
 
312
 
 
313
 
getConstraints.fPORTFOLIO =
314
 
function(object)
315
 
{   # A function implemented by Rmetrics
316
 
 
317
 
    # Description:
318
 
    #   Extracts the statistics from a 'fPORTFOLIO' object
319
 
    
320
 
    # Arguments:
321
 
    #   object - an object of S4 class fPORTFOLIO as returned by the
322
 
    #       functions *Portfolio().
323
 
    
324
 
    # FUNCTION:
325
 
    
326
 
    # Get Portfolio:
327
 
    ans = object@constraints
328
 
  
329
 
    # Return Value:
330
 
    ans  
331
 
}
332
 
 
333
 
 
334
 
################################################################################
335
 
 
336
 
 
337
 
getPortfolio.fPORTFOLIO =
338
 
function(object)
339
 
{   # A function implemented by Rmetrics
340
 
 
341
 
    # Description:
342
 
    #   Extracts the statistics from a 'fPORTFOLIO' object
343
 
    
344
 
    # Arguments:
345
 
    #   object - an object of S4 class fPORTFOLIO as returned by the
346
 
    #       functions *Portfolio().
347
 
    
348
 
    # FUNCTION:
349
 
    
350
 
    # Get Portfolio:
351
 
    ans = object@portfolio
352
 
  
353
 
    # Return Value:
354
 
    ans  
355
 
}
356
 
 
357
 
 
358
 
# ------------------------------------------------------------------------------
359
 
 
360
 
 
361
 
getWeights.fPORTFOLIO =
362
 
function(object)
363
 
{   # A function implemented by Rmetrics
364
 
 
365
 
    # Description:
366
 
    #   Extracts the weights from a 'fPORTFOLIO' object
367
 
    
368
 
    # Arguments:
369
 
    #   object - an object of S4 class fPORTFOLIO as returned by the
370
 
    #       functions *Portfolio().
371
 
    
372
 
    # FUNCTION:
373
 
    
374
 
    # Get Portfolio:
375
 
    ans = object@portfolio$weights
376
 
  
377
 
    # Return Value:
378
 
    ans  
379
 
}
380
 
 
381
 
 
382
 
# ------------------------------------------------------------------------------
383
 
 
384
 
 
385
 
getTargetReturn.fPORTFOLIO =
386
 
function(object)
387
 
{   # A function implemented by Rmetrics
388
 
 
389
 
    # Description:
390
 
    #   Extracts the target Return from a 'fPORTFOLIO' object
391
 
    
392
 
    # Arguments:
393
 
    #   object - an object of S4 class fPORTFOLIO as returned by the
394
 
    #       functions *Portfolio().
395
 
    
396
 
    # FUNCTION:
397
 
    
398
 
    # Get Portfolio:
399
 
    ans = object@portfolio$targetReturn
400
 
  
401
 
    # Return Value:
402
 
    ans  
403
 
}
404
 
 
405
 
 
406
 
# ------------------------------------------------------------------------------
407
 
 
408
 
 
409
 
getTargetRisk.fPORTFOLIO =
410
 
function(object)
411
 
{   # A function implemented by Rmetrics
412
 
 
413
 
    # Description:
414
 
    #   Extracts the target Risk from a 'fPORTFOLIO' object
415
 
    
416
 
    # Arguments:
417
 
    #   object - an object of S4 class fPORTFOLIO as returned by the
418
 
    #       functions *Portfolio().
419
 
    
420
 
    # FUNCTION:
421
 
    
422
 
    # Get Portfolio:
423
 
    ans = object@portfolio$targetRisk
424
 
  
425
 
    # Return Value:
426
 
    ans  
427
 
}
428
 
 
429
 
 
430
 
# ------------------------------------------------------------------------------
431
 
 
432
 
 
433
 
getTargetAlpha.fPORTFOLIO =
434
 
function(object)
435
 
{   # A function implemented by Rmetrics
436
 
 
437
 
    # Description:
438
 
    #   Extracts the target Alpha from a 'fPORTFOLIO' object
439
 
    
440
 
    # Arguments:
441
 
    #   object - an object of S4 class fPORTFOLIO as returned by the
442
 
    #       functions *Portfolio().
443
 
    
444
 
    # FUNCTION:
445
 
    
446
 
    # Get Portfolio:
447
 
    ans = object@portfolio$targetAlpha
448
 
  
449
 
    # Return Value:
450
 
    ans  
451
 
}
452
 
 
453
 
 
454
 
# ------------------------------------------------------------------------------
455
 
 
456
 
 
457
 
getRiskFreeRate.fPORTFOLIO =
458
 
function(object)
459
 
{   # A function implemented by Rmetrics
460
 
 
461
 
    # Description:
462
 
    #   Extracts the risk free rate from a 'fPORTFOLIO' object
463
 
    
464
 
    # Arguments:
465
 
    #   object - an object of S4 class fPORTFOLIO as returned by the
466
 
    #       functions *Portfolio().
467
 
    
468
 
    # FUNCTION:
469
 
    
470
 
    # Get Portfolio:
471
 
    ans = object@portfolio$riskFreeRate
472
 
  
473
 
    # Return Value:
474
 
    ans  
475
 
}
476
 
 
477
 
 
478
 
# ------------------------------------------------------------------------------
479
 
 
480
 
 
481
 
getNFrontierPoints.fPORTFOLIO =
482
 
function(object)
483
 
{   # A function implemented by Rmetrics
484
 
 
485
 
    # Description:
486
 
    #   Extracts the Number of Frontier Points from a 'fPORTFOLIO' object
487
 
    
488
 
    # Arguments:
489
 
    #   object - an object of S4 class fPORTFOLIO as returned by the
490
 
    #       functions *Portfolio().
491
 
    
492
 
    # FUNCTION:
493
 
    
494
 
    # Get Portfolio:
495
 
    ans = object@portfolio$nFrontierPoints
496
 
  
497
 
    # Return Value:
498
 
    ans  
499
 
}
500
 
 
501
 
# ------------------------------------------------------------------------------
502
 
 
503
 
 
504
 
getStatus.fPORTFOLIO =
505
 
function(object)
506
 
{   # A function implemented by Rmetrics
507
 
 
508
 
    # Description:
509
 
    #   Extracts the status from a 'fPORTFOLIO' object
510
 
    
511
 
    # Arguments:
512
 
    #   object - an object of S4 class fPORTFOLIO as returned by the
513
 
    #       functions *Portfolio().
514
 
    
515
 
    # FUNCTION:
516
 
    
517
 
    # Get Portfolio:
518
 
    ans = object@portfolio$status
519
 
  
520
 
    # Return Value:
521
 
    ans  
522
 
}
523
 
 
524
 
 
525
 
################################################################################
526
 
 
527
 
 
528
 
getFrontier.fPORTFOLIO =
529
 
function(object, frontier = c("both", "lower", "upper"), doplot = FALSE, ...)
530
 
{   # A function implemented by Rmetrics
531
 
 
532
 
    # Description:
533
 
    #   Extracts the efficient frontier from a 'fPORTFOLO' object
534
 
    
535
 
    # Arguments:
536
 
    #   object - an object of S4 class fPORTFOLIO as returned by the
537
 
    #       functions *Portfolio().
538
 
    
539
 
    # FUNCTION:
540
 
    
541
 
    # Settings:
542
 
    frontier = match.arg(frontier)
543
 
    
544
 
    # Get Efficient Frontier:
545
 
    Type = getType(object)
546
 
    targetRisk = getTargetRisk(object)[ ,1] 
547
 
    targetReturn = getTargetReturn(object)[ , 1]
548
 
    
549
 
    #if (Type == "MV") {
550
 
    #    ans = cbind(Risk = targetRisk, Return = targetReturn)
551
 
    #} else if (Type == "CVaR") {
552
 
    #    if (is.matrix(targetRisk)) {
553
 
    #        Risk = targetRisk[, 1]
554
 
    #    } else {
555
 
    #        Risk = targetRisk[1]
556
 
    #    }
557
 
        ans = cbind(Risk = targetRisk, Return = targetReturn)
558
 
    #}
559
 
    #rownames(ans) = NULL
560
 
 
561
 
    # Extract upper part of frontier
562
 
    if(frontier == "upper"){
563
 
        index = 1:length(ans[, 1])
564
 
        test = c(-1, diff(ans[, 1]))
565
 
        index = index[test > 0]
566
 
        ans = ans[index, ]
567
 
    } else if(frontier == "lower"){
568
 
        index = 1:length(ans[, 1])
569
 
        test = c(-1, diff(ans[, 1]))
570
 
        index = index[test < 0]
571
 
        if (length(index) == 1) {
572
 
            ans = matrix(ans[index, ], ncol = 2)
573
 
        } else {
574
 
            ans = ans[index, ]
575
 
        }         
576
 
    }
577
 
    
578
 
    # Add colnames:
579
 
    colnames(ans) = c("targetRisk", "targetReturn")
580
 
  
581
 
    # Plot:
582
 
    if(doplot) plot(ans, ...)
583
 
    
584
 
    # Return Value:
585
 
    ans  
586
 
}
587
 
 
588
 
 
589
 
# ------------------------------------------------------------------------------
590
 
 
591
 
 
592
 
getCovRiskBudgets.fPORTFOLIO = 
593
 
function (object) 
594
 
{   # A function implemented by Rmetrics
595
 
 
596
 
    # Description:
597
 
    #   Extracts risk budgets from a portfolio object
598
 
    
599
 
    # FUNCTION:
600
 
    
601
 
    # Covariance Risk Budgets:
602
 
    weights = object@portfolio$weights
603
 
    ans = NA
604
 
    Sigma = object@data$data@statistics$Sigma
605
 
    if (is.null(dim(weights))) {
606
 
        # Single Portfolio ...
607
 
        ans1 = as.vector(weights %*% Sigma %*% weights)
608
 
        ans2 = as.vector(weights * Sigma %*% weights)
609
 
        ans = round(ans2/ans1, digits = 4)
610
 
        names(ans) = names(weights)
611
 
    } else {
612
 
        # Frontier ...
613
 
        Names = colnames(weights)
614
 
        ans = NULL
615
 
        for (i in 1:(dim(weights)[1])) {
616
 
            ans1 = as.vector(weights[i, ] %*% Sigma %*% weights[i, ])
617
 
            ans2 = as.vector(weights[i, ] * Sigma %*% weights[i, ])
618
 
            ans = rbind(ans, ans2/ans1)
619
 
        }
620
 
        colnames(ans) = Names
621
 
    }
622
 
    
623
 
    # Return Value:
624
 
    ans
625
 
}
626
 
 
627
 
 
628
 
# ------------------------------------------------------------------------------
629
 
 
630
 
 
631
 
getTailRiskBudgets.fPORTFOLIO = 
632
 
function (object) 
633
 
{   # A function implemented by Rmetrics
634
 
 
635
 
    # Description:
636
 
    #   Extracts tail risk budgets from a portfolio object
637
 
    
638
 
    # Arguments:
639
 
    #   object - an object of S4 class fPORTFOLIO as returned by the
640
 
    #       functions *Portfolio().
641
 
    
642
 
    # FUNCTION:
643
 
    
644
 
    # Check if available:
645
 
    Lambda = object@spec$spec@model$tailRisk$lower
646
 
    if (is.null(Lambda)) return(NA)
647
 
    
648
 
    # Tail Risk Budgets:
649
 
    weights = getWeights(object)
650
 
    ans = NA
651
 
    if (is.null(dim(weights))) {
652
 
        ans1 = as.vector(weights %*% Lambda %*% weights)
653
 
        ans2 = as.vector(weights * Lambda %*% weights)
654
 
        ans1 = 1
655
 
        ans = round(ans2/ans1, digits = 4)
656
 
        names(ans) = names(weights)
657
 
    }
658
 
    else {
659
 
        Names = colnames(weights)
660
 
        ans = NULL
661
 
        for (i in 1:(dim(weights)[1])) {
662
 
            ans1 = as.vector(weights[i, ] %*% Lambda %*% weights[i, ])
663
 
            ans2 = as.vector(weights[i, ] * Lambda %*% weights[i, ])
664
 
            ans1 = 1
665
 
            ans = rbind(ans, ans2/ans1)
666
 
        }
667
 
        colnames(ans) = Names
668
 
    }
669
 
    
670
 
    # Return Value:
671
 
    ans
672
 
}
673
 
 
674
 
 
675
 
################################################################################
676