Jump to content

Gaussian Function Help


TheWind

Recommended Posts

I've been working to learn about the Gaussian Function, Probability Density Functions, and Cumulative Distribution Functions.

https://en.wikipedia.org/wiki/Gaussian_function

https://en.wikipedia.org/wiki/Probability_density_function

https://en.wikipedia.org/wiki/Cumulative_distribution_function

From my understanding you have the Gaussian function:

{\displaystyle f(x)=ae^{-{\frac {(x-b)^{2}}{2c^{2}}}}}

and the integral of the Gaussian function is only equal to 1 when

a = 1/(c * sqrt(2pi))

In this case, it can be used as a probability density function with normally distributed values. (This form is used for statistical analysis)

b = mu (mean)

c^2 = sigma^2 (standard deviation squared or the variance)

The above is the relation of the variables of the regular Gaussian function to that of the Normal Distribution (as it's used in statistics).

g(x)={\frac {1}{\sigma {\sqrt {2\pi }}}}e^{-{\frac {1}{2}}\left({\frac {x-\mu }{\sigma }}\right)^{2}}.

I've never taken a statistics class, so forgive me if I'm wrong on something. Most of the information here is from the wiki page or other resources I was reading to learn from.

 

My goal is to write a function which utilizes the normalized Gaussian function to make a method that returns a random Gaussian number. By doing so it would return numbers closer to the mean more often than those that are farther away.

What I tried to do was write a function for the standard Gaussian function, another function for the normalized Gaussian function and then a function that returns a random Gaussian number:

/**
	 * 
	 * @param a - Absolute maximum
	 * @param b - Mean or center point
	 * @param c - Standard deviation or width
	 * @param x - x-value along the gaussian function
	 * @return corresponding y-value along the gaussian function
	 */
	public static double gaussian(double a, double b, double c, double x) {
		return a * Math.exp( -Math.pow(x-b, 2) / (2 * Math.pow(c, 2)) );
	}
	
	/**
	 * 
	 * @param mu - Mean or center point
	 * @param sigma - Standard deviation or width
	 * @param x - x-value along the gaussian function
	 * @return corresponding y-value along the gaussian function
	 */
	public static double gaussianNormalized(double mu, double sigma, double x) {
		return gaussian( 1 / (sigma * Math.sqrt(2 * Math.PI)), mu, sigma, x);
	}

My issue is that the numbers it provides will always be distributed in an equal fashion to that of the Java Random class which is not what I'm looking for.

How can I go about returning numbers that conform to a normalized Gaussian function distribution? am I going about this in the wrong way?

Link to comment
Share on other sites

4 hours ago, HunterRS said:

Correct me if I am wrong, you are trying to write Random.nextGaussian() by only using java's normal random generator?

Yes, something like that. From reading through a few more articles it appears the easiest way to achieve something like this is to simply use the Random.nextGaussian() method and adjust the output based on the parameters you want the function to follow.

Although I would prefer to solve it the other way using a random double or int

Edited by TheWind
Link to comment
Share on other sites

11 hours ago, TheWind said:

I've been working to learn about the Gaussian Function, Probability Density Functions, and Cumulative Distribution Functions.

https://en.wikipedia.org/wiki/Gaussian_function

https://en.wikipedia.org/wiki/Probability_density_function

https://en.wikipedia.org/wiki/Cumulative_distribution_function

From my understanding you have the Gaussian function:

{\displaystyle f(x)=ae^{-{\frac {(x-b)^{2}}{2c^{2}}}}}

and the integral of the Gaussian function is only equal to 1 when

a = 1/(c * sqrt(2pi))

In this case, it can be used as a probability density function with normally distributed values. (This form is used for statistical analysis)

b = mu (mean)

c^2 = sigma^2 (standard deviation squared or the variance)

The above is the relation of the variables of the regular Gaussian function to that of the Normal Distribution (as it's used in statistics).

g(x)={\frac {1}{\sigma {\sqrt {2\pi }}}}e^{-{\frac {1}{2}}\left({\frac {x-\mu }{\sigma }}\right)^{2}}.

I've never taken a statistics class, so forgive me if I'm wrong on something. Most of the information here is from the wiki page or other resources I was reading to learn from.

 

My goal is to write a function which utilizes the normalized Gaussian function to make a method that returns a random Gaussian number. By doing so it would return numbers closer to the mean more often than those that are farther away.

What I tried to do was write a function for the standard Gaussian function, another function for the normalized Gaussian function and then a function that returns a random Gaussian number:


/**
	 * 
	 * @param a - Absolute maximum
	 * @param b - Mean or center point
	 * @param c - Standard deviation or width
	 * @param x - x-value along the gaussian function
	 * @return corresponding y-value along the gaussian function
	 */
	public static double gaussian(double a, double b, double c, double x) {
		return a * Math.exp( -Math.pow(x-b, 2) / (2 * Math.pow(c, 2)) );
	}
	
	/**
	 * 
	 * @param mu - Mean or center point
	 * @param sigma - Standard deviation or width
	 * @param x - x-value along the gaussian function
	 * @return corresponding y-value along the gaussian function
	 */
	public static double gaussianNormalized(double mu, double sigma, double x) {
		return gaussian( 1 / (sigma * Math.sqrt(2 * Math.PI)), mu, sigma, x);
	}

My issue is that the numbers it provides will always be distributed in an equal fashion to that of the Java Random class which is not what I'm looking for.

How can I go about returning numbers that conform to a normalized Gaussian function distribution? am I going about this in the wrong way?

 

Consider having a read of the Java Random.nextGaussian method:

https://docs.oracle.com/javase/8/docs/api/java/util/Random.html#nextGaussian--

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...