2013-03-31 4 views
8

यह स्क्रिप्ट नीचे याहू डेटा को क्वांटमोड में एक फ़ंक्शन के माध्यम से खींचती है, फिर आरजीएल लाइब्रेरी के साथ एक 3 डी ग्राफ को फोरमेट करने के लिए डेटा को मालिश करता है, संलग्न डेटा को दिखाने के लिए एक ggplot है जिसे मैं कोशिश कर रहा हूं अलग लाइन geoms में एक सतह बनाने के लिए। मुद्दा यह है कि 3 डी ग्राफ बहुत बदसूरत लग रहा है और फ्रंट महीने की समाप्ति पर सीमित मात्रा में अंक की वजह से कटौती करता है .. क्या कोई मुझे बता सकता है कि यहां क्या हो रहा है, मैं इसे ठीक करने के लिए क्या कर सकता हूं .. क्या मुझे चिकनी करने की ज़रूरत है प्रत्येक समाप्ति की रेखा तब interpolate .... ?? volsurface http://img15.imageshack.us/img15/7338/surface.png ggplot2_smile http://img402.imageshack.us/img402/1272/volatilitysmilegoog.pngआर प्रोग्रामिंग में अस्थिरता सतह ग्राफ पर इंटरपोलेशन फिक्सिंग

library(RQuantLib) 
library(quantmod) 
library(rgl) 
library(akima) 
library(ggplot2) 
library(plyr) 

GetIV <- function(type, value, 
        underlying, strike,dividendYield, riskFreeRate, maturity, volatility, 
        timeSteps=150, gridPoints=151) { 

    AmericanOptionImpliedVolatility(type, value, 
            underlying, strike,dividendYield, riskFreeRate, maturity, volatility, 
            timeSteps=150, gridPoints=151)$impliedVol 
} 


GetDelta <- function(type, underlying, strike, 
        dividendYield, riskFreeRate, maturity, volatility, 
        timeSteps=150, gridPoints=149, engine="CrankNicolson") { 

    AmericanOption(type,underlying, strike, dividendYield, riskFreeRate, maturity, volatility, 
        timeSteps=150, gridPoints=149, engine="CrankNicolson")$delta 
} 
# set what symbol you want vol surface for 
underlying <- 'GOOG' 
# set what your volatility forcast or assumption is 
volforcast <- .25 
# Get symbols current price 
underlying.price <- getQuote(underlying,what=yahooQF("Last Trade (Price Only)"))$Last 

OC <- getOptionChain(underlying, NULL) 
#check data 
head(OC) 
lputs <- lapply(OC, FUN = function(x) x$puts[grep("[A-Z]\\d{6}[CP]\\d{8}$", rownames(x$puts)), ]) 
head(lputs) #check for NA values, yahoo returns all NA values sometimes 
puts <- do.call('rbind', lputs) 
#check data 
head(puts,5) 

symbols <- as.vector(unlist(lapply(lputs, rownames))) 
expiries <- unlist(lapply(symbols, FUN = function(x) regmatches(x=x, regexpr('[0-9]{6}', x)))) 
puts$maturity <- as.numeric((as.Date(expiries, "%y%m%d") - Sys.Date())/365) 

puts$IV <- mapply(GetIV, value = puts$Ask, strike = puts$Strike, maturity = puts$maturity, 
        MoreArgs= list(type='put', underlying= underlying.price, 
           dividendYield=0, riskFreeRate = 0.01, 
           volatility = volforcast), SIMPLIFY=TRUE) 

puts$delta <- mapply(GetDelta, strike = puts$Strike, volatility = puts$IV, 
        maturity = puts$maturity, MoreArgs= list(type='put', 
                   underlying=underlying.price, dividendYield=0, 
                   riskFreeRate = 0.01), SIMPLIFY=TRUE) 

# subset out itm puts 
puts <- subset(puts, delta < -.09 & delta > -.5) 

expiries.formated <- format(as.Date(levels(factor(expiries)), format = '%y%m%d'), "%B %d, %Y") 

fractionofyear.levels <- levels(factor(puts$maturity)) 

xyz <- with(puts, interp(x=maturity, y=delta*100, z=IV*100, 
         xo=sort(unique(maturity)), extrap=FALSE)) 

with(xyz, persp3d(x,y,z, col=heat.colors(length(z))[rank(z)], xlab='maturity', 
        ylab='delta', zlab='IV', main='IV Surface')) 

putsplot <- ggplot(puts, aes(delta, IV, group = factor(maturity), color = factor(maturity))) + 
    labs(x = "Delta", y = "Implied Volatilty", title="Volatility Smile", color = "GooG \nExpiration") + 
    scale_colour_discrete(breaks=c(fractionofyear.levels), 
          labels=c(expiries.formated)) + 
    geom_line() + 
    geom_point() 

putsplot 
+0

इस पर कोई मदद नहीं: यहाँ कि कैसा दिखेगा क्या है? – cdcaveman

+0

क्या कोई जानता है कि यहां के महीनों में इंटरपोलेशन को कैसे ठीक किया जाए? – cdcaveman

+0

क्या आपको पता है कि क्या मैं अंक को किसी प्रकार की लाइन ऑब्जेक्ट्स में बदल सकता हूं और को – cdcaveman

उत्तर

3

akima पैकेज वास्तव में आप क्या जरूरत है, लेकिन मैं आपको कमी अपने y- अक्ष में अंतर्वेशित अंकों की संख्या, delta चर करने की जरूरत है लगता है। जिस तरह से आपने इसे अभी सेट किया है वह डिफ़ॉल्ट 40-बिंदु ग्रिड का उपयोग करता है।

# No interpolation on x-axis, but uses the default 40 point grid on the y-axis 
xyz <- with(puts, interp(x=maturity, y=delta*100, z=IV*100, 
      xo=sort(unique(maturity)), extrap=FALSE)) 
# By setting to use less points, it will "stretch" the surface over those points. 
xyz <- with(puts, interp(x=maturity, y=delta*100, z=IV*100, 
      xo=sort(unique(maturity)), 
      yo=seq(min(delta*100), max(delta*100), length = 15), extrap=FALSE)) 

Surface smoothed by y-axis

आप seq समारोह में लंबाई चर के साथ खेल सकते चिकनाई के भिन्न स्तरों मिलता है।


मैं अभी भी पूरी तरह से समझ में नहीं आता कि आप क्या चाहते हैं, लेकिन हो सकता है आप maturity द्वारा चमकीला बनाना चाहते हैं?

# This smooths just by x. 
xyz <- with(puts, interp(x=maturity, y=delta*100, z=IV*100, 
      xo=seq(min(maturity), max(maturity), length = 5), 
      , extrap=FALSE)) 

with(xyz, persp3d(x,y,z, col=heat.colors(length(z))[rank(z)], xlab='maturity', 
        ylab='delta', zlab='IV', main='IV Surface')) 

enter image description here

+0

हाँ पर .. मैं वास्तव में केवल उन्हें समाप्त होने के डेल्टा मूल्यों को सुगम बनाना चाहता हूं लेकिन समाप्ति के बीच नहीं – cdcaveman

+0

@cdcaveman एचएम, मैं आपके विशेष डोमेन से अपरिचित हूं। क्या आप 'डेल्टा', 'परिपक्वता' और 'IV' के संदर्भ में जो कुछ भी चिकना नहीं करना चाहते हैं उसका वर्णन कर सकते हैं? मुझे वास्तव में यह नहीं मिलता कि कौन सा "समाप्ति" है। साथ ही, मुझे लगता है कि 'डेल्टा' एकमात्र चीज है जो यहां चिकनी है। – nograpes

+0

अलग-अलग परिपक्वतायां हैं ..... डेल्टा मान किसी विशेष स्ट्राइक मूल्य पर विकल्पों की कीमत से प्राप्त किए जाते हैं .. इसलिए मैं अपने स्वयं के समाप्ति पर विकल्प डेल्टा को सुगम बनाना चाहता हूं ... विचार आपकी कोशिश है डेल्टा द्वारा परिपक्वता के बीच मूल्य निर्धारण अलग करें .. इसलिए आप परिपक्वता के बीच चिकनी नहीं करना चाहते हैं .. मैं केवल प्रत्येक परिपक्वता डेल्टा मानों को अन्य परिपक्वता नहीं बल्कि चिकना करना चाहता हूं .. इस तरह आप डेल्टा द्वारा कैलेंडर जोखिम देख सकते हैं .. क्या यह करता है समझ? – cdcaveman

संबंधित मुद्दे