Lognormal Stock Price Distribution in R [closed] - r

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
My goal is to obtain a lognormal distribution of stock prices, that I can then use to calculate the expected utilty an agent would receive from holding such stocks. But I am a bit stuck on how to achieve this. The distribution of stock prices is lognormal with volatility σ and expected returns r obtained from the capital asset pricing model.
For example, σ = 0.01825838 and r = 0.13053162. I tried to generate the distribution of stock prices with:
dist <- rlnorm(1000, 0.13053162, 0.01825838)
However, this distribution is normally distributed, and not lognormally distributed. How can I generate this distribution that I subsequently can use to calculate the expected utility assuming the agent have constant relative risk aversion:

I infer from this post on Mathematics StacksExchange that the lognormal approximates the normal for small sigma. So you are sampling from lognormal with the code you provide, but since you have a small sigma, it can be approximated by the normal.
You can see the approximation of the normal visually by varying sigma
hist(rlnorm(1000, meanlog = 0.1305, sdlog = 0.500)
hist(rlnorm(1000, meanlog = 0.1305, sdlog = 0.018)

I don't really understand the second part of your question, but to create a lognormal distribution, you can use the property that if X follows a log-normal distribution and Y = ln(X), then Y follows a normal distribution.
https://en.wikipedia.org/wiki/Log-normal_distribution
So something like:
set.seed(1234)
dist <- rnorm(1000, 1, .5)
ldist <- exp(dist)
hist(ldist)
This looks log-normal to me.

Related

normally distributed population, calculating in R the probability of negative or zero readings occurring [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
In R, how do you calculate the probability of negative or zero readings occurring?
μ and σ are giving.
You can use the distribution function of the gaussian distribution:
pnorm(0,μ,σ)
(I guess you are speaking about gaussian distribution)
edit
The pnorm is the cumulative density function. Its values are between 0 and 1, and its value at x gives the area under the gaussian curve from -inf to x. In my example below, the value at 0 of pnorm give the area in pink under the gaussian curve, so the probability you are looking for, i.e. the probability of sampling a value following the corresponding gausian distribution with a value below or equal to 0.

doing likelihood plot in R for binomial model [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Can anybody tell me how do i plot the maximum likelihood values L(ˆθM, M) versus M for a suitable range of M values for the count data provided in frogs and then estimate the total number of frogs living in the pond and the probability of appearance in R?
These were the questions asked:
questions and i have answered a and b
I have my pmf of my module and finded likelihood and log likelihood of my binomial model and you can see how much code i have written so far please help!
solutions to a,b and c so far
# importing the necessary modules
library(tidyverse)
library(ggplot2)
# loading the data
load("~/Statistical Modelling and Inference/aut2020.RData")
# Assigning a variable to the data
data <- frogs
# Assigning n to the length of the data
n <- length(frogs$counts)
n
theta_hat <- sum(frogs$counts)/M
loglik <- function(theta, y, M, data){
# Computes the log_likelihood for binomial model
sum_y <- sum(data$counts)
M <- sum_y / n
sum(log(dbinom(M,y))) + sum(y)*log(theta) + n*M - sum(y)*log(1-theta)}
Data looks like this:
in r script
when readed
Since you have already found the likelihood function in your answer (a), you can see that it is a function of M and theta - both unknown.
After estimating theta you have the MLE estimator - let's call it theta_hat.
In the dataframe frogs you have all the count observations y_i (known). So, using the known data and the ML estimate theta_hat that means the likelihood can be plotted for some (reasonable) range of values of M (you might need to try different ranges). So plot L(theta_hat, M) as a function of M. Bear in mind though that the estimate theta_hat will change as you change M so take that into account. The point where L(theta_hat, M) is maximized are you ML estimates for theta and M.

How to construct a CDF if you don't have the data but you know the slope and 50% point? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a question regarding the CDF function. In a study, the researchers fitted a CDF of the normal distribution on their data and presented the median (= -8.4 dB) and the slope (=18.7). How can I recreate this function (in R) so I can find out what percentage (y-axis) is expected for e.g. 9 dB on the x-axis? When I look at the formula of the CDF, I'm not sure where to insert the slope and median statistics...
Many thanks in advance!
You need additional hypotheses on the distribution in order to recreate the full CDF function. Unless you have counter-arguments (from the data interpretation), let's assume it is normally distributed.
By definition, the derivative (slope) of a CDF function is its PDF function. Thus what you are given is the median and the PDF value at the median. For the normal distribution:
the median (50% point) equals the mean
PDF(mu) is 1/sqrt(2*pi*sd), where mu is the mean and sd is the standard deviation
Thus, with the additional hypothesis that the sought distribution is normal, its CDF will be the normal CDF (pnorm in R) with parameters:
mean = midpoint
sd = 1/(2*pi * slope^2)

distribution from percentage with R [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have distribution of parameter (natural gas mixture composition) expressed in percents. How to test such data for distribution parameters (it should be gamma, normal or lognormal distribution) and generate random composition based on that parameters in R?
This might be a better question for CrossValidated, but:
it is not generally a good idea to choose from among a range of possible distributions according to goodness of fit. Instead, you should choose according to the qualitative characteristics of your data, something like this:
Frustratingly, this chart doesn't actually have the best choice for your data (composition, continuous, bounded between 0 and 1 [or 0 and 100]), which is a Beta distribution (although there are technical issues if you have values of exactly 0 or 100 in your sample).
In R:
## some arbitrary data
z <- c(2,8,40,45,56,58,70,89)
## fit (beta values must be in (0,1), not (0,100), so divide by 100)
(m <- MASS::fitdistr(z/100,"beta",start=list(shape1=1,shape2=1)))
## sample 1000 new values
z_new <- 100*rbeta(n=1000,shape1=m$estimate["shape1"],
shape2=m$estimate["shape2"])

Calculating confidence intervall for quantiles first by hand (than in R) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
It would be great if someone can check whether my approach is correct or not.
Question in short will be, if the error calculation is the correct way.
lets assume i have the following data.
data = c(23.7,25.47,25.16,23.08,24.86,27.89,25.9,25.08,25.08,24.16,20.89)
Furthermore i want to check if my data follows a normal distribution.
Edit: I know that there are tests etc. but i will concentrate on constructing the qqplot with confidence lines. I know that there is a method in the car package, but i want to understand the building of these lines.
So i calculate the percentiles for my sample data as well as for my theoretical distribution (with estimated mu = 24.6609and sigma = 1.6828. So i end up with these two vectors containing the percentiles.
percentileReal = c(23.08,23.7,24.16,24.86,25.08,25.08,25.16,25.47,25.90)
percentileTheo = c(22.50,23.24,23.78,24.23,24.66,25.09,25.54,26.08,26.82)
Now i want to calculate the confidence intervall for alpha=0.05 for the theoretical percentiles. If i rembember myself correct, the formula is given by
error = z*sigma/sqrt(n),
value = +- error
with n=length(data) and z=quantil of the normal distribution for the given p.
So in order to get the confidence intervall for the 2nd percentile i'll do the following:
error = (qnorm(20+alpha/2,mu,sigma)-qnorm(20-alpha/2,mu,sigma))*sigma/sqrt(n)
Insert the values:
error = (qnorm(0.225,24.6609,1.6828)-qnorm(0.175,24.6609,1.6828)) * 1.6828/sqrt(11)
error = 0.152985
confidenceInterval(for 2nd percentil) = [23.24+0.152985,23.24-0.152985]
confidenceInterval(for 2nd percentil) = [23.0870,23.3929]
Finally i have
percentileTheoLower = c(...,23.0870,.....)
percentileTheoUpper = c(...,23.3929,.....)
same for the rest....
So what do you think, can i go with it?
If your goal is to test if the data follows a normal distribution, use the shapiro.wilk test:
shapiro.test(data)
# Shapiro-Wilk normality test
# data: data
# W = 0.9409, p-value = 0.5306
1-p is the probability that the distribution is non-normal. So, since p>0.05 we cannot assert that the distribution is non-normal. A crude interpretation is that "there is a 53% chance that the distribution is normal."
You can also use qqplot(...). The more nearly linear this plot is, the more likely it is that your data is normally distributed.
qqnorm(data)
Finally, there is the nortest package in R which has, among other things, the Pearson Chi-Sq test for normality:
library(nortest)
pearson.test(data)
# Pearson chi-square normality test
# data: data
# P = 3.7273, p-value = 0.2925
This (more conservative) test suggest that there is only a 29% chance that the distribution is normal. All these tests are fully explained in the documentation.

Resources