Search code examples
c++statisticsmontecarlo

How would I create a function with an input of probability that outputs z-scores? (C++)


Very new to C++ here (~2 weeks of experience), and mildly noobish in statistics. I also rarely ask questions here so I'm unfamiliar with 99% of the formatting options, which means I'm not sure how to display subscripts and superscripts, so if somebody with stackoverflow experience wants to edit some of my stuff to make things easier on the eyes, feel free to do so.

I've been playing around trying to run a Montecarlo simulation to generate future stock price estimates and comparing them to historical data.

The key formula to generate my data is:

s_(i+1) = s_i * e^(drift + σ * NORMSINV(r))

  • s_(i+1) = tomorrow's price
  • s_i = today's price
  • drift = mean - variance/2
  • σ = standard deviation
  • NORMSINV(x) is an Microsoft Excel function that calculates the inverse of the standard cumulative normal distribution, and x is a probability. In other words, NORMSINV outputs a z-score given a probability
  • r = a randomly generated real number between 0 and 1

My current issue is implementing the NORMSINV() function in C++. Currently, I have an excel file to handle that, then read the file and input it into my computer. I've looked all over online and there doesn't seem to be a possible analytical method to calculate NORMSINV(), and I haven't seen anybody ask how to implement NORMSINV() in C++ on this forum, so I decided to post this.

If somebody could demonstrate how to implement NORMSINV() in C++ or point me to a library that can do it, I'd really appreciate it :)


Solution

  • You wanted to be pointed to a library.

    This may help you: Implementation of normsinv

    Read the text, then copy an paste it in your IDE. Compile it and remove warnings and errors.

    Then use it. Hope this helps.