Apply uniroot on vector












1















I would like to find the strike for a given delta using black-scholes. I am able to do this using uniroot for a single spot value and volatility value but would like for this to work across a vector of spot values and vector of volatility values. My below example is simplified, once it works I will expand to much bigger vectors...



I have seen there is a previous post using sapply on uniroot for a different problem. Have tried applying here and not getting much luck.



Would you be able to help me figure out how to do this?



Many thanks!



library(rootSolve)
library(ragtop)

CallPut <- 1 # Call
Spot <- c(110, 112, 114)
Rate <- 0 # For simplicity
T <- 0.5 # half a year
Vol <- c(0.07, 0.08, 0.09)
TargetDelta <- 0.5
Lower <- c(100, 102, 104)
Upper <- c(120, 122, 124)

solve.for.strike <- function(Strike) {
BS <- blackscholes(CallPut, Spot, Strike, Rate, T, Vol)
TargetDelta - BS$Delta
}

test <- uniroot(solve.for.strike, lower = Lower, upper = Upper)


Have also tried using uniroot.all but not working:



test <- uniroot.all(solve.for.strike, c(100, 120), lower = 100, upper = 120)


Have now added libraries.



If any guidance would be much appreciated.










share|improve this question




















  • 1





    Function blackscholes comes from what package? Not in base R so you should start the script with library(pkgname).

    – Rui Barradas
    Nov 22 '18 at 10:21
















1















I would like to find the strike for a given delta using black-scholes. I am able to do this using uniroot for a single spot value and volatility value but would like for this to work across a vector of spot values and vector of volatility values. My below example is simplified, once it works I will expand to much bigger vectors...



I have seen there is a previous post using sapply on uniroot for a different problem. Have tried applying here and not getting much luck.



Would you be able to help me figure out how to do this?



Many thanks!



library(rootSolve)
library(ragtop)

CallPut <- 1 # Call
Spot <- c(110, 112, 114)
Rate <- 0 # For simplicity
T <- 0.5 # half a year
Vol <- c(0.07, 0.08, 0.09)
TargetDelta <- 0.5
Lower <- c(100, 102, 104)
Upper <- c(120, 122, 124)

solve.for.strike <- function(Strike) {
BS <- blackscholes(CallPut, Spot, Strike, Rate, T, Vol)
TargetDelta - BS$Delta
}

test <- uniroot(solve.for.strike, lower = Lower, upper = Upper)


Have also tried using uniroot.all but not working:



test <- uniroot.all(solve.for.strike, c(100, 120), lower = 100, upper = 120)


Have now added libraries.



If any guidance would be much appreciated.










share|improve this question




















  • 1





    Function blackscholes comes from what package? Not in base R so you should start the script with library(pkgname).

    – Rui Barradas
    Nov 22 '18 at 10:21














1












1








1








I would like to find the strike for a given delta using black-scholes. I am able to do this using uniroot for a single spot value and volatility value but would like for this to work across a vector of spot values and vector of volatility values. My below example is simplified, once it works I will expand to much bigger vectors...



I have seen there is a previous post using sapply on uniroot for a different problem. Have tried applying here and not getting much luck.



Would you be able to help me figure out how to do this?



Many thanks!



library(rootSolve)
library(ragtop)

CallPut <- 1 # Call
Spot <- c(110, 112, 114)
Rate <- 0 # For simplicity
T <- 0.5 # half a year
Vol <- c(0.07, 0.08, 0.09)
TargetDelta <- 0.5
Lower <- c(100, 102, 104)
Upper <- c(120, 122, 124)

solve.for.strike <- function(Strike) {
BS <- blackscholes(CallPut, Spot, Strike, Rate, T, Vol)
TargetDelta - BS$Delta
}

test <- uniroot(solve.for.strike, lower = Lower, upper = Upper)


Have also tried using uniroot.all but not working:



test <- uniroot.all(solve.for.strike, c(100, 120), lower = 100, upper = 120)


Have now added libraries.



If any guidance would be much appreciated.










share|improve this question
















I would like to find the strike for a given delta using black-scholes. I am able to do this using uniroot for a single spot value and volatility value but would like for this to work across a vector of spot values and vector of volatility values. My below example is simplified, once it works I will expand to much bigger vectors...



I have seen there is a previous post using sapply on uniroot for a different problem. Have tried applying here and not getting much luck.



Would you be able to help me figure out how to do this?



Many thanks!



library(rootSolve)
library(ragtop)

CallPut <- 1 # Call
Spot <- c(110, 112, 114)
Rate <- 0 # For simplicity
T <- 0.5 # half a year
Vol <- c(0.07, 0.08, 0.09)
TargetDelta <- 0.5
Lower <- c(100, 102, 104)
Upper <- c(120, 122, 124)

solve.for.strike <- function(Strike) {
BS <- blackscholes(CallPut, Spot, Strike, Rate, T, Vol)
TargetDelta - BS$Delta
}

test <- uniroot(solve.for.strike, lower = Lower, upper = Upper)


Have also tried using uniroot.all but not working:



test <- uniroot.all(solve.for.strike, c(100, 120), lower = 100, upper = 120)


Have now added libraries.



If any guidance would be much appreciated.







r






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 10:42







Arthur

















asked Nov 22 '18 at 10:17









ArthurArthur

114




114








  • 1





    Function blackscholes comes from what package? Not in base R so you should start the script with library(pkgname).

    – Rui Barradas
    Nov 22 '18 at 10:21














  • 1





    Function blackscholes comes from what package? Not in base R so you should start the script with library(pkgname).

    – Rui Barradas
    Nov 22 '18 at 10:21








1




1





Function blackscholes comes from what package? Not in base R so you should start the script with library(pkgname).

– Rui Barradas
Nov 22 '18 at 10:21





Function blackscholes comes from what package? Not in base R so you should start the script with library(pkgname).

– Rui Barradas
Nov 22 '18 at 10:21












1 Answer
1






active

oldest

votes


















0














So I have played around and have come up with the following which loops across the vectors. BUT my question is really whether there is a way to apply a solve to the vector to avoid having to loop across each element in the vector?



If there is any more efficient or elegant solution to this than what I have come up with, am very interested.



CallPut <- 1 # Call
Spot <- c(110, 112, 114)
Rate <- 0 # For simplicity
T <- 0.5
Vol <- c(0.07, 0.08, 0.09)

solve.for.strike <- function(Strike, S, V, D) {
BS <- blackscholes(CallPut, S, Strike, 0, T, V)
D - BS$Delta
}

loop.uniroot <- function(i, delta) {
uniroot(solve.for.strike, lower=Spot[i]*0.95, upper=Spot[i]*1.05, S=Spot[i], V=Vol[i], D=delta)$root
}

test3 <- mapply(loop.uniroot, 1:3, delta = 0.5)
test4 <- sapply(1:3, loop.uniroot, delta = 0.5)





share|improve this answer
























  • Check out the function vapply - it is the most efficient of the apply family (something like vapply(1:3, loop.uniroot, numeric(1), delta = 0.5)

    – nate
    Nov 22 '18 at 12:51











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428656%2fapply-uniroot-on-vector%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














So I have played around and have come up with the following which loops across the vectors. BUT my question is really whether there is a way to apply a solve to the vector to avoid having to loop across each element in the vector?



If there is any more efficient or elegant solution to this than what I have come up with, am very interested.



CallPut <- 1 # Call
Spot <- c(110, 112, 114)
Rate <- 0 # For simplicity
T <- 0.5
Vol <- c(0.07, 0.08, 0.09)

solve.for.strike <- function(Strike, S, V, D) {
BS <- blackscholes(CallPut, S, Strike, 0, T, V)
D - BS$Delta
}

loop.uniroot <- function(i, delta) {
uniroot(solve.for.strike, lower=Spot[i]*0.95, upper=Spot[i]*1.05, S=Spot[i], V=Vol[i], D=delta)$root
}

test3 <- mapply(loop.uniroot, 1:3, delta = 0.5)
test4 <- sapply(1:3, loop.uniroot, delta = 0.5)





share|improve this answer
























  • Check out the function vapply - it is the most efficient of the apply family (something like vapply(1:3, loop.uniroot, numeric(1), delta = 0.5)

    – nate
    Nov 22 '18 at 12:51
















0














So I have played around and have come up with the following which loops across the vectors. BUT my question is really whether there is a way to apply a solve to the vector to avoid having to loop across each element in the vector?



If there is any more efficient or elegant solution to this than what I have come up with, am very interested.



CallPut <- 1 # Call
Spot <- c(110, 112, 114)
Rate <- 0 # For simplicity
T <- 0.5
Vol <- c(0.07, 0.08, 0.09)

solve.for.strike <- function(Strike, S, V, D) {
BS <- blackscholes(CallPut, S, Strike, 0, T, V)
D - BS$Delta
}

loop.uniroot <- function(i, delta) {
uniroot(solve.for.strike, lower=Spot[i]*0.95, upper=Spot[i]*1.05, S=Spot[i], V=Vol[i], D=delta)$root
}

test3 <- mapply(loop.uniroot, 1:3, delta = 0.5)
test4 <- sapply(1:3, loop.uniroot, delta = 0.5)





share|improve this answer
























  • Check out the function vapply - it is the most efficient of the apply family (something like vapply(1:3, loop.uniroot, numeric(1), delta = 0.5)

    – nate
    Nov 22 '18 at 12:51














0












0








0







So I have played around and have come up with the following which loops across the vectors. BUT my question is really whether there is a way to apply a solve to the vector to avoid having to loop across each element in the vector?



If there is any more efficient or elegant solution to this than what I have come up with, am very interested.



CallPut <- 1 # Call
Spot <- c(110, 112, 114)
Rate <- 0 # For simplicity
T <- 0.5
Vol <- c(0.07, 0.08, 0.09)

solve.for.strike <- function(Strike, S, V, D) {
BS <- blackscholes(CallPut, S, Strike, 0, T, V)
D - BS$Delta
}

loop.uniroot <- function(i, delta) {
uniroot(solve.for.strike, lower=Spot[i]*0.95, upper=Spot[i]*1.05, S=Spot[i], V=Vol[i], D=delta)$root
}

test3 <- mapply(loop.uniroot, 1:3, delta = 0.5)
test4 <- sapply(1:3, loop.uniroot, delta = 0.5)





share|improve this answer













So I have played around and have come up with the following which loops across the vectors. BUT my question is really whether there is a way to apply a solve to the vector to avoid having to loop across each element in the vector?



If there is any more efficient or elegant solution to this than what I have come up with, am very interested.



CallPut <- 1 # Call
Spot <- c(110, 112, 114)
Rate <- 0 # For simplicity
T <- 0.5
Vol <- c(0.07, 0.08, 0.09)

solve.for.strike <- function(Strike, S, V, D) {
BS <- blackscholes(CallPut, S, Strike, 0, T, V)
D - BS$Delta
}

loop.uniroot <- function(i, delta) {
uniroot(solve.for.strike, lower=Spot[i]*0.95, upper=Spot[i]*1.05, S=Spot[i], V=Vol[i], D=delta)$root
}

test3 <- mapply(loop.uniroot, 1:3, delta = 0.5)
test4 <- sapply(1:3, loop.uniroot, delta = 0.5)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 12:29









ArthurArthur

114




114













  • Check out the function vapply - it is the most efficient of the apply family (something like vapply(1:3, loop.uniroot, numeric(1), delta = 0.5)

    – nate
    Nov 22 '18 at 12:51



















  • Check out the function vapply - it is the most efficient of the apply family (something like vapply(1:3, loop.uniroot, numeric(1), delta = 0.5)

    – nate
    Nov 22 '18 at 12:51

















Check out the function vapply - it is the most efficient of the apply family (something like vapply(1:3, loop.uniroot, numeric(1), delta = 0.5)

– nate
Nov 22 '18 at 12:51





Check out the function vapply - it is the most efficient of the apply family (something like vapply(1:3, loop.uniroot, numeric(1), delta = 0.5)

– nate
Nov 22 '18 at 12:51


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428656%2fapply-uniroot-on-vector%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin