# Make and save plots of the Gaussian density with two standard deviation # File: plotting.R for the class # https://www.math.nyu.edu/faculty/goodman/teaching/MathFin2019/MathFinance.html # see the handout Plotting.pdf posted there. n = 150 # number of data point in the plot a = -1. # left endpoint of plotting interval b = 5. # plot interval is [a,b] mu = 1.5 # mean of gaussian to be plotted sig1 = .5 # two standard deviations sig2 = .6 # standard deviation p = function(x, mu, sig){ # normal density p = ( 1/sqrt(2*pi*sig^2 ) ) * exp( -(x-mu)^2/(2*sig^2)) return(p) } dx = (b-a)/(n-1) # distance between plot points x = 1:n # will be x coordinates y1 = 1:n # will be y coordinates for first curve y2 = 1:n # will be y coordinates, second curve for ( i in 1:n){ x[i] = a + (i-1)*dx y1[i] = p( x[i], mu, sig1) # the same p() function with different sigma y2[i] = p( x[i], mu, sig2) } title = sprintf("Gaussians with mean %6.2f", mu) subtitle = sprintf("Using %4d points", n) color1 = "black" # colors of the lines color2 = "darkgreen" # there are lots of colors type1 = 1 # line types, the first is solid (type = 1), type2 = 4 # the second line is a pattern legand1 = sprintf("standard deviation =%6.2f", sig1) legand2 = sprintf("standard deviation =%6.2f", sig2) legends = c( legand1, legand2) # put the values into arrays ... colors = c( color1, color2) # ... for the legend() function types = c( type1, type2) # Plot commands #------------ copy from here ------------------------------------------------ plot( x, y1, main = title, # title at the top of the plot sub = subtitle, # subtitle at the bottom xlab = "X axis", # label of the horizontal axis ylab = "probability density", # label of the vertical axis type = "l", # get rid of the little circles lty = type1, # line style col = color1, # line color panel.first = grid()) # make a grid on the plot lines( x, y2, # plot the second curve, lty = type2, col = color2) # line type and color legend( "topright", # where the legend box goes. legends, # the text of the legends col = colors, # corresponding line color and type lty = types) #------------- copy to here ---------------------------------------------------- # make a file with an image of the plot pdf("fancyPlot.pdf") # tell R to create a .pdf file for the plot # Plot commands, copy and paste from above #------------ copy from here ------------------------------------------------ plot( x, y1, main = title, # title at the top of the plot sub = subtitle, # subtitle at the bottom xlab = "X axis", # label of the horizontal axis ylab = "probability density", # label of the vertical axis type = "l", # get rid of the little circles lty = type1, # line style col = color1, # line color panel.first = grid()) # make a grid on the plot lines( x, y2, # plot the second curve, lty = type2, col = color2) # line type and color legend( "topright", # where the legend box goes. legends, # the text of the legends col = colors, # corresponding line color and type lty = types) #------------- copy to here ---------------------------------------------------- dev.off() # tell R you're done.