I've just found out the "wireframe" function in R to plot 3D-surface graphs.
I wish to implement it by plotting the Black&Scholes Call Option price against two sequence of data: Time to Maturity and Strike Price. So, first of all here follows my script so far:
S=100 #My stock Price
K=90 #Initial Strike Price
T=1 #Initial Time to Maturity (1 year here)
RF=0.03 #Risk free rate
SIGMA=0.2 #Volatility
d1=(log(S/K) + (RF + 0.5*SIGMA^2)*T)/SIGMA*sqrt(T) #initial d(1)
d2=d1-SIGMA*sqrt(T) #initial d(2)
Then I tried to prepare a grid for my surface/3D plot:
K=seq(80,120,1)
T=seq(0,1,0.1)
table=expand.grid(K,T)
Last step, I add a new column variable for computing my Call price according to every single combination:
table$CALL= S*pnorm(d1) - K*exp(-RF*T)*pnorm(d2)
names(table)= c("K","T","CALL")
Finally the surface/3D plot:
wireframe(CALL ~ K * T, scales = list( arrows = FALSE),aspect = c(1, .6),data=table,
drape=T,shade=T)
So, it plots an apparently reliable graph (according to my finance study) but...I don't know, it looks a bit "scale-step" graph. As I'm a newbye in "wireframe" function, I don't know if I properly used all input data. I'd love an opinion to someone who already used to plot B&S formula in a 3D plot. I'm interested because I'd do the same to plot Greeks and Implied Volatility in the future.
Thanks in advance
Related
I have a problem with plotting ECDF. I try to reverse the x axis value like 1-(the function).
Because I wanna have smaller in the beginning of the graph and decreasing like in my reference graph.
load("91-20.RData")
ts <- data.frame(dat91,dat92,dat93,dat94,dat95,dat96,dat97,
dat98,dat99,dat00,dat11,dat12,dat12,dat13,
dat14,dat15,dat16,dat17,dat18,dat19,dat20)
ts
tsclean <- na.omit(ts)
#--------------------------------------------------------
ggplot(tsclean, aes(tsclean$dat91)) +
stat_ecdf(geom = "step")
This graph what i have, but i wanna duplicate like the reference
load("91-20.RData")
ts <- data.frame(dat91,dat92,dat93,dat94,dat95,dat96,dat97,
dat98,dat99,dat00,dat11,dat12,dat12,dat13,
dat14,dat15,dat16,dat17,dat18,dat19,dat20)
ts
tsclean <- na.omit(ts)
I think the graph you're looking for is called an "exceedance" graph. A web search finds some resources; try a web search for "R exceedance graph".
EDIT: This is more suitable as a comment than an answer, but my web browser is being unhelpful at the moment; sorry for the distraction.
<Disclaimer(s) - (1) This is my first post, so please be gentle, specifically regarding formatting and (2) I did try to dig as much as I could on this topic before posting the question here>
I have a simple data vector containing returns of 40 portfolios on the same day:
Year Return
Now -17.39862061
Now -12.98954582
Now -12.98954582
Now -12.86928749
Now -12.37044334
Now -11.07007504
Now -10.68971539
Now -10.07578182
Now -9.984867096
Now -8.764036179
Now -8.698093414
Now -8.594026566
Now -8.193638802
Now -7.818599701
Now -7.622627735
Now -7.535216808
Now -7.391239166
Now -7.331315517
Now -5.58059597
Now -5.579797268
Now -4.525201797
Now -3.735909224
Now -2.687532902
Now -2.65363884
Now -2.177522898
Now -1.977644682
Now -1.353205681
Now -0.042584345
Now 0.096564181
Now 0.275416046
Now 0.638839543
Now 1.959529042
Now 3.715519428
Now 4.842819691
Now 5.475946426
Now 6.380955219
Now 6.535937309
Now 8.421762466
Now 8.556800842
Now 10.39185524
I am trying to plot these returns to compare versus other days (so the rest of my history e.g.). I tried to use stat_density_ridges as per the code block below
ggplot(data = data.plot, aes(x = Return, y = Year, fill = factor(..quantile..))) +
stat_density_ridges(geom = "density_ridges_gradient",calc_ecdf = TRUE,
quantiles = c(0.025, 0.5, 0.975),
quantile_lines = TRUE)
As you can see - the "year" in this case is the same i.e. there is no height parameter, yet I get a nice ridg(y) chart. While the chart is beautiful to behold, and very very awesome, I am at a loss to determine how the plotting function is computing the density in this case, specially the height.
This is the output chart I get (I have omitted the formatting code here since it doesn't make a difference to my question):
Portfolio Return Distribution Plots - US versus Europe
I tried digging into the code of the function itself, but came up with a total blank. The documentation didn't help (except perhaps give me a hint that the function plots continous distributions).
Any help, or guidance, or even a nudge in the right direction would be extremely helpful.
I am trying to produce a scatter plot of some data. I do so in two different ways, as shown in code below (most of the code is just arranging data, the only graphing part is at the bottom). One uses a direct reference to the variables in the workspace, and the other arranges the data into an xts object first and then uses column indices to refer to them.
The resulting scatter plots are different, even though I have checked that the source data is the same in both ways.
I am wondering why these plots are different, thanks in advance.
# Get data
# =============
library('quantmod')
# Set monthly time interval
StartPeriod = paste0("1980-01")
EndPeriod = paste0("2014-07")
DateString = paste0(StartPeriod,"/", EndPeriod)
# CPI (monthly)
getSymbols("CPIAUCSL", src="FRED")
# QoQ growth, Annualized
CPIAUCSL = ((CPIAUCSL/lag(CPIAUCSL))^4-1)*100
CPIAUCSL = CPIAUCSL[DateString]
# Oil prices (monthly)
getSymbols(c("MCOILWTICO"), src="FRED")
# QoQ growth, annualized
MCOILWTICO = ((MCOILWTICO/lag(MCOILWTICO))^4-1)*100
MCOILWTICO = MCOILWTICO[DateString]
# Produce plots
# ===============
library('lattice')
# Method 1, direct reference
xyplot(CPIAUCSL~lag(MCOILWTICO,1), ylim=c(-5,6),
ylab="CPI",
xlab="Oil Price, 1 month lag",
main="Method 1: Inflation vs. Lagged Oil Price",
grid=TRUE)
# Method 2, refer to column indices of xts object
basket = merge(CPIAUCSL, MCOILWTICO)
xyplot(basket[ ,1] ~ lag(basket[ ,2],1), ylim=c(-5, 6),
ylab="CPI",
xlab="Oil Price, 1 month lag",
main="Method 2: Inflation vs. Lagged Oil Price",
grid=TRUE)
# Double check data fed into plots is the same
View(merge(CPIAUCSL, lag(MCOILWTICO,1)))
View(merge(basket[ ,1], lag(basket[ ,2],1))) # yes, matches
Method 1 is definitely incorrect as it will pair points 6 years apart! For instance, CPIAUCSL[3] is the data for 1980-03-01, while lag(MCOILWTICO,1)[3] corresponds to 1986-03-01 - however, on the scatterplot they will be paired! In contrast, basket[ ,1][3] and basket[ ,2][3] both belong to 1980-03-01.
(Your double check didn't show the problem, because there you used merge - as opposed to Method 1! - which solves the problem.)
I know how to produce the plots using leveragePlot(), but I can not find a way to produce a statistic for leverage for each observation like in megastat output.
I think you're looking for the hat values.
Use hatvalues(fit). The rule of thumb is to examine any observations 2-3 times greater than the average hat value. I don't know of a specific function or package off the top of my head that provides this info in a nice data frame but doing it yourself is fairly straight forward. Here's an example:
fit <- lm(hp ~ cyl + mpg, data=mtcars) #a fake model
hatvalues(fit)
hv <- as.data.frame(hatvalues(fit))
mn <-mean(hatvalues(fit))
hv$warn <- ifelse(hv[, 'hatvalues(fit)']>3*mn, 'x3',
ifelse(hv[, 'hatvalues(fit)']>2*mn, 'x3', '-' ))
hv
For larger data sets you could use subset and/or orderto look at just certain values ranges for the hat values:
subset(hv, warn=="x3")
subset(hv, warn%in%c("x2", "x3"))
hv[order(hv['hatvalues(fit)']), ]
I actually came across a nice plot function that does this in the book R in Action but as this is a copyrighted book I will not display Kabacoff's intellectual property. But that plot would work even better for mid sized data sets.
Here is a decent hat plot though that you may also want to investigate:
plot(hatvalues(fit), type = "h")
consider the included example in the np-package for r,
page 21 of the Vignettes for np package.
npcdens returns a conditional density object and is able to plot 2d-pdf and 2d-cdf, as shown. I wanted to know if I can somehow extract the 1-D information (pdf / cdf) from the object if I were to specify one of the two parameters, like in a vector or something ?? I am new to R and was not able to find out the format of the object.
Thanks for the help.
-Egon.
Here is the code as requested:
require(np)
data("Italy")
attach(Italy)
bw <- npcdensbw(formula=gdp~ordered(year), tol=.1, ftol=.1)
fhat <- npcdens(bws=bw)
summary(fhat)
npplot(bws=bw)
npplot(bws=bw, cdf=TRUE)
detach(Italy)
The fhat object contains all the needed info plus a whole lot more. To see what all is in there, do a str( fhat ) to see the structure.
I believe the values you are interested in are xeval, yeval, and condens (PDF density).
There are lots of ways to get at the values but I tend to like data frames. I'd pop the three vectors in a single data frame:
denDf <- cbind( year=as.character( fhat$xeval[,1] ), fhat$yeval, fhat$condens )
## had to do a dance around the year variable because it's a factor
then I'd select the values I want with a subset():
subset( denDf, year==1951 & gdp > 8 & gdp < 8.2)
since gdp is a floating point value it's very hard to select with a == operator.
The method suggested by JD Long will only extract density for data points in the existing training set. If you want the density at other points (conditioning or conditional variables) you will need to use the predict()
function. The following code extracts and plots the 1-D density distribution conditioned on year ==1999, a value not contained in the original data set.
First construct a data frame with the same components as the Italy data set, with gdp regularly spaced and with "1999" an ordered factor.
yr1999<- rep("1999", 100)
gdpVals <-seq(1,35, length.out=100)
nD1999 <- data.frame(year = ordered(yr1999), gdp = gdpVals)
Next use the predict function to extract the densities.
gdpDens1999 <-predict(fhat,newdata = nD1999)
The following code plots the density.
plot(gdpVals, gdpDens1999, type='l', col='red', xlab='gdp', ylab = 'p(gdp|yr = 1999)')