1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """The monte_carlo user function definitions for Monte Carlo simulations."""
24
25
26 from graphics import WIZARD_IMAGE_PATH
27 from pipe_control import error_analysis
28 from user_functions.data import Uf_info; uf_info = Uf_info()
29 from user_functions.objects import Desc_container
30
31
32
33 monte_carlo_desc = Desc_container("Monte Carlo Simulation Overview")
34 monte_carlo_desc.add_paragraph("For proper error analysis using Monte Carlo simulations, a sequence of function calls is required for running the various simulation components. The steps necessary for implementing Monte Carlo simulations are:")
35 monte_carlo_desc.add_item_list_element("1", "The measured data set together with the corresponding error set should be loaded into relax.")
36 monte_carlo_desc.add_item_list_element("2", "Either minimisation is used to optimise the parameters of the chosen model, or a calculation is run.")
37 monte_carlo_desc.add_item_list_element("3", "To initialise and turn on Monte Carlo simulations, the number of simulations, n, needs to be set.")
38 monte_carlo_desc.add_item_list_element("4", "The simulation data needs to be created either by back calculation from the fully minimised model parameters from step 2 or by direct calculation when values are calculated rather than minimised. The error set is used to randomise each simulation data set by assuming Gaussian errors. This creates a synthetic data set for each Monte Carlo simulation.")
39 monte_carlo_desc.add_item_list_element("5", "Prior to minimisation of the parameters of each simulation, initial parameter estimates are required. These are taken as the optimised model parameters. An alternative is to use a grid search for each simulation to generate initial estimates, however this is extremely computationally expensive. For the case where values are calculated rather than minimised, this step should be skipped (although the results will be unaffected if this is accidentally run).")
40 monte_carlo_desc.add_item_list_element("6", "Each simulation requires minimisation or calculation. The same techniques as used in step 2, excluding the grid search when minimising, should be used for the simulations.")
41 monte_carlo_desc.add_item_list_element("7", "Failed simulations are removed using the techniques of model elimination.")
42 monte_carlo_desc.add_item_list_element("8", "The model parameter errors are calculated from the distribution of simulation parameters.")
43 monte_carlo_desc.add_paragraph("Monte Carlo simulations can be turned on or off using functions within this class. Once the function for setting up simulations has been called, simulations will be turned on. The effect of having simulations turned on is that the functions used for minimisation (grid search, minimise, etc) or calculation will only affect the simulation parameters and not the model parameters. By subsequently turning simulations off using the appropriate function, the functions used in minimisation will affect the model parameters and not the simulation parameters.")
44 monte_carlo_desc.add_paragraph("An example for model-free analysis using the prompt UI mode which includes only the functions required for implementing the above steps is:")
45 monte_carlo_desc.add_prompt("relax> minimise.grid_search(inc=11) # Step 2.")
46 monte_carlo_desc.add_prompt("relax> minimise.execute('newton') # Step 2.")
47 monte_carlo_desc.add_prompt("relax> monte_carlo.setup(number=500) # Step 3.")
48 monte_carlo_desc.add_prompt("relax> monte_carlo.create_data(method='back_calc') # Step 4.")
49 monte_carlo_desc.add_prompt("relax> monte_carlo.initial_values() # Step 5.")
50 monte_carlo_desc.add_prompt("relax> minimise.execute('newton') # Step 6.")
51 monte_carlo_desc.add_prompt("relax> eliminate() # Step 7.")
52 monte_carlo_desc.add_prompt("relax> monte_carlo.error_analysis() # Step 8.")
53 monte_carlo_desc.add_paragraph("An example for reduced spectral density mapping is:")
54 monte_carlo_desc.add_prompt("relax> minimise.calculate() # Step 2.")
55 monte_carlo_desc.add_prompt("relax> monte_carlo.setup(number=500) # Step 3.")
56 monte_carlo_desc.add_prompt("relax> monte_carlo.create_data(method='back_calc') # Step 4.")
57 monte_carlo_desc.add_prompt("relax> minimise.calculate() # Step 6.")
58 monte_carlo_desc.add_prompt("relax> monte_carlo.error_analysis() # Step 8.")
59
60
61
62 uf_class = uf_info.add_class('monte_carlo')
63 uf_class.title = "Class containing the functions for Monte Carlo and related simulations."
64 uf_class.menu_text = "&monte_carlo"
65 uf_class.gui_icon = "oxygen.actions.roll-relax-blue"
66
67
68
69 uf = uf_info.add_uf('monte_carlo.create_data')
70 uf.title = "Create the Monte Carlo simulation data."
71 uf.title_short = "Data creation."
72 uf.add_keyarg(
73 name = "method",
74 default = "back_calc",
75 py_type = "str",
76 desc_short = "method",
77 desc = "The simulation method.",
78 wiz_element_type = "combo",
79 wiz_combo_choices = ["Monte Carlo", "Bootstrapping"],
80 wiz_combo_data = ["back_calc", "direct"],
81 wiz_read_only = True
82 )
83 uf.add_keyarg(
84 name = "distribution",
85 default = "measured",
86 py_type = "str",
87 desc_short = "distribution",
88 desc = "The error distribution method.",
89 wiz_element_type = "combo",
90 wiz_combo_choices = ["Measured error", "Reduced chi2", "Fixed error"],
91 wiz_combo_data = ["measured", "red_chi2", "fixed"],
92 wiz_read_only = True
93 )
94 uf.add_keyarg(
95 name = "fixed_error",
96 py_type = "float",
97 default = None,
98 desc_short = "fixed error value for fixed error distribution.",
99 desc = "The fixed value to use when distribution is set to 'fixed'.",
100 can_be_none = True
101 )
102
103 uf.desc.append(Desc_container())
104 uf.desc[-1].add_paragraph("The method can either be set to back calculation (Monte Carlo) or direct (bootstrapping), the choice of which determines the simulation type. If the values or parameters are calculated rather than minimised, this option will have no effect. Errors should only be propagated via Monte Carlo simulations if errors have been measured. ")
105 uf.desc[-1].add_paragraph("For error analysis, the method should be set to back calculation which will result in proper Monte Carlo simulations. The data used for each simulation is back calculated from the minimised model parameters and is randomised using Gaussian noise where the standard deviation is from the original error set. When the method is set to back calculation, this function should only be called after the model is fully minimised.")
106 uf.desc[-1].add_paragraph("The simulation type can be changed by setting the method to direct. This will result in bootstrapping simulations which cannot be used in error analysis (and which are no longer Monte Carlo simulations). However, these simulations are required for certain model selection techniques (see the documentation for the model selection user function for details), and can be used for other purposes. Rather than the data being back calculated from the fitted model parameters, the data is generated by taking the original data and randomising using Gaussian noise with the standard deviations set to the original error set.")
107 uf.desc[-1].add_paragraph("The errors generated per simulation can either be generated indidual per datapoint and drawn from a gauss distrubtion described by the standard deviation of the indidual point, or it can be generated from a overall gauss distribution described by the standard deviation of the goodness of fit, where SD_fit = sqrt(chi2/(N-p)). The last possibility is to supply a fixed value of the standard deviation, from which gauss distribution to draw errors from.")
108 uf.desc.append(monte_carlo_desc)
109 uf.backend = error_analysis.monte_carlo_create_data
110 uf.menu_text = "&create_data"
111 uf.gui_icon = "oxygen.actions.list-add-relax-blue"
112 uf.wizard_height_desc = 500
113 uf.wizard_size = (1000, 750)
114 uf.wizard_apply_button = False
115 uf.wizard_image = WIZARD_IMAGE_PATH + 'monte_carlo.png'
116
117
118
119 uf = uf_info.add_uf('monte_carlo.error_analysis')
120 uf.title = "Calculate parameter errors from the Monte Carlo simulations."
121 uf.title_short = "Error calculation."
122
123 uf.desc.append(Desc_container())
124 uf.desc[-1].add_paragraph("Parameter errors are calculated as the standard deviation of the distribution of parameter values. This function should never be used if parameter values are obtained by minimisation and the simulation data are generated using the method 'direct'. The reason is because only true Monte Carlo simulations can give the true parameter errors.")
125 uf.desc.append(monte_carlo_desc)
126 uf.backend = error_analysis.monte_carlo_error_analysis
127 uf.menu_text = "&error_analysis"
128 uf.gui_icon = "oxygen.actions.roll-relax-blue"
129 uf.wizard_height_desc = 620
130 uf.wizard_size = (1000, 750)
131 uf.wizard_apply_button = False
132 uf.wizard_image = WIZARD_IMAGE_PATH + 'monte_carlo.png'
133
134
135
136 uf = uf_info.add_uf('monte_carlo.initial_values')
137 uf.title = "Set the initial simulation parameter values."
138 uf.title_short = "Initial value setting."
139
140 uf.desc.append(Desc_container())
141 uf.desc[-1].add_paragraph("This only effects where minimisation occurs and can therefore be skipped if the values or parameters are calculated rather than minimised. However, if accidentally run in this case, the results will be unaffected. It should only be called after the model or run is fully minimised. Once called, the user functions minimise.grid_search and minimise.execute will only effect the simulations and not the model parameters.")
142 uf.desc[-1].add_paragraph("The initial values of the parameters for each simulation is set to the minimised parameters of the model. A grid search can be undertaken for each simulation instead, although this is computationally expensive and unnecessary. The minimisation function should be executed for a second time after running this function.")
143 uf.desc.append(monte_carlo_desc)
144 uf.backend = error_analysis.monte_carlo_initial_values
145 uf.menu_text = "&initial_values"
146 uf.gui_icon = "oxygen.actions.list-add-relax-blue"
147 uf.wizard_height_desc = 620
148 uf.wizard_size = (1000, 750)
149 uf.wizard_apply_button = False
150 uf.wizard_image = WIZARD_IMAGE_PATH + 'monte_carlo.png'
151
152
153
154 uf = uf_info.add_uf('monte_carlo.off')
155 uf.title = "Turn the Monte Carlo simulations off."""
156 uf.title_short = "Simulations off."""
157
158 uf.desc.append(Desc_container())
159 uf.desc[-1].add_paragraph("This will turn off the Monte Carlo simulations so that subsequent optimisation will operate directly on the model parameters and not on the simulations.")
160 uf.desc.append(monte_carlo_desc)
161 uf.backend = error_analysis.monte_carlo_off
162 uf.menu_text = "o&ff"
163 uf.gui_icon = "oxygen.actions.dialog-cancel"
164 uf.wizard_height_desc = 620
165 uf.wizard_size = (1000, 750)
166 uf.wizard_apply_button = False
167 uf.wizard_image = WIZARD_IMAGE_PATH + 'monte_carlo.png'
168
169
170
171 uf = uf_info.add_uf('monte_carlo.on')
172 uf.title = "Turn the Monte Carlo simulations on."""
173 uf.title_short = "Simulations on."""
174
175 uf.desc.append(Desc_container())
176 uf.desc[-1].add_paragraph("This will turn on the Monte Carlo simulations so that subsequent optimisation will operate on the simulations rather than on the real model parameters.")
177 uf.desc.append(monte_carlo_desc)
178 uf.backend = error_analysis.monte_carlo_on
179 uf.menu_text = "o&n"
180 uf.gui_icon = "oxygen.actions.dialog-ok"
181 uf.wizard_height_desc = 620
182 uf.wizard_size = (1000, 750)
183 uf.wizard_apply_button = False
184 uf.wizard_image = WIZARD_IMAGE_PATH + 'monte_carlo.png'
185
186
187
188 uf = uf_info.add_uf('monte_carlo.setup')
189 uf.title = "Set up the Monte Carlo simulations."
190 uf.title_short = "Simulation setup."
191 uf.add_keyarg(
192 name = "number",
193 default = 500,
194 py_type = "int",
195 min = 3,
196 max = 100000,
197 desc_short = "number of Monte Carlo simulations",
198 desc = "The number of Monte Carlo simulations."
199 )
200
201 uf.desc.append(Desc_container())
202 uf.desc[-1].add_paragraph("This must be called prior to any of the other Monte Carlo functions. The effect is that the number of simulations will be set and that simulations will be turned on.")
203 uf.desc.append(monte_carlo_desc)
204 uf.backend = error_analysis.monte_carlo_setup
205 uf.menu_text = "&setup"
206 uf.gui_icon = "oxygen.actions.document-edit"
207 uf.wizard_height_desc = 570
208 uf.wizard_size = (1000, 750)
209 uf.wizard_apply_button = False
210 uf.wizard_image = WIZARD_IMAGE_PATH + 'monte_carlo.png'
211