cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
New to using JMP? Hit the ground running with the Early User Edition of Discovery Summit. Register now, free of charge.
Register for our Discovery Summit 2024 conference, Oct. 21-24, where you’ll learn, connect, and be inspired.
Choose Language Hide Translation Bar
Djtjhin
Level IV

How do I automate random sampling generation from different distribution types?

Let's say I have a table with 5 rows & 2 columns. The first column is for distribution types (e.g. Normal, Lognormal, Weibull, etc) and the second column is for the respective parameters. I'd like to make a for loop which will create a new data table for each of the 5 rows, where in that new data table, a new column will be added with a random sampling function based on the parameters of the row. 

e.g. row 1 --> Normal, (0.1,0.02); Create a new data table with a column that has a formula of 

randomnormal(0.1,0.02)

My current challenge is that different distribution types require varying number of parameters. Appreciate some insights on how I should approach this.

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: How do I automate random sampling generation from different distribution types?

Here is a simple example of how to do what you requested

Names Default To Here( 1 );
dt = New Table( "Sample",
	Add Rows( 5 ),
	New Column( "Types",
		Character,
		Set Values(
			{"Normal", "Lognormal", "Weibull", "Uniform", "Johnson Su"}
		),
		Set Display Width( 79 )
	),
	New Column( "Parameters",
		Character,
		Set Values(
			{"(0.1,0.02)", "(0.2,.005)", "(0.3,0.005)", "(0.1,0.5)",
			"(0.5, 1, 1, 1)"}
		),
		Set Display Width( 101 )
	)
);

For( i = 1, i <= N Rows( dt ), i++,
	Eval(
		Substitute(
				Expr(
					New Table( dt:Types[i],
						add rows( 20 ),
						New Column( "Random " || dt:Types[i],
							set each value( __formula__ )
						)
					)
				),
			Expr( __formula__ ),
				Parse( "Random " || dt:Types[i] || dt:Parameters[i] )
		)
	)
);

Please take the time to fully understand how the script works, and if you do not understand it, please respond back so the Community can provide you with the additional information you may need.

Jim

View solution in original post

5 REPLIES 5
Thierry_S
Super User

Re: How do I automate random sampling generation from different distribution types?

Hi,
A couple thoughts:
1) If possible, I would split the parameters across columns to make the scripting of the formula easier
2) I would use the MATCH formula to look for the name of the distribution and specifically call the Random function for the corresponding distribution

Match( :Distribution,
	"Normal", Random Normal( :Parameter 1, :Parameter 2 ),
	"LogNormal", Random Lognormal( :Parameter 1, :Parameter 2 ),
	.
)
Thierry R. Sornasse
Djtjhin
Level IV

Re: How do I automate random sampling generation from different distribution types?

Thanks Thierry, any suggestions if I want to limit the number of columns for the parameters ?

Another concern that I have is, I believe the Match() formula is case sensitive. I can try to control this by putting a List Check on the column. Are there other ways to control / manage that ?

Thierry_S
Super User

Re: How do I automate random sampling generation from different distribution types?

Hi,

 

For the case sensitive issue, here is the fix:

Match( Uppercase( :Distribution ),
	"NORMAL", Random Normal( :Parameter 1, :Parameter 2 ),
	"LOGNORMAL", Random Lognormal( :Parameter 1, :Parameter 2 ),
	.
)

About limiting the number of columns, I will get back to you because it is a little bit trickier.

Best,

TS

Thierry R. Sornasse
txnelson
Super User

Re: How do I automate random sampling generation from different distribution types?

Here is a simple example of how to do what you requested

Names Default To Here( 1 );
dt = New Table( "Sample",
	Add Rows( 5 ),
	New Column( "Types",
		Character,
		Set Values(
			{"Normal", "Lognormal", "Weibull", "Uniform", "Johnson Su"}
		),
		Set Display Width( 79 )
	),
	New Column( "Parameters",
		Character,
		Set Values(
			{"(0.1,0.02)", "(0.2,.005)", "(0.3,0.005)", "(0.1,0.5)",
			"(0.5, 1, 1, 1)"}
		),
		Set Display Width( 101 )
	)
);

For( i = 1, i <= N Rows( dt ), i++,
	Eval(
		Substitute(
				Expr(
					New Table( dt:Types[i],
						add rows( 20 ),
						New Column( "Random " || dt:Types[i],
							set each value( __formula__ )
						)
					)
				),
			Expr( __formula__ ),
				Parse( "Random " || dt:Types[i] || dt:Parameters[i] )
		)
	)
);

Please take the time to fully understand how the script works, and if you do not understand it, please respond back so the Community can provide you with the additional information you may need.

Jim
Djtjhin
Level IV

Re: How do I automate random sampling generation from different distribution types?

It works Jim! Thanks