Package user_functions :: Module monte_carlo
[hide private]
[frames] | no frames]

Source Code for Module user_functions.monte_carlo

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2004-2013 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  6  #                                                                             # 
  7  # This program is free software: you can redistribute it and/or modify        # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation, either version 3 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # This program is distributed in the hope that it will be useful,             # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 19  #                                                                             # 
 20  ############################################################################### 
 21   
 22  # Module docstring. 
 23  """The monte_carlo user function definitions for Monte Carlo simulations.""" 
 24   
 25  # relax module imports. 
 26  from graphics import WIZARD_IMAGE_PATH 
 27  from pipe_control import monte_carlo 
 28  from user_functions.data import Uf_info; uf_info = Uf_info() 
 29  from user_functions.objects import Desc_container 
 30   
 31   
 32  # Generic description document, used in all user functions. 
 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> grid_search(inc=11)                                       # Step 2.") 
 46  monte_carlo_desc.add_prompt("relax> minimise('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('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> calc()                                                    # 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> calc()                                                    # Step 6.") 
 58  monte_carlo_desc.add_prompt("relax> monte_carlo.error_analysis()                              # Step 8.") 
 59   
 60   
 61  # The user function class. 
 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  # The monte_carlo.create_data user function. 
 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  # Description. 
 84  uf.desc.append(Desc_container()) 
 85  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. ") 
 86  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.") 
 87  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.") 
 88  uf.desc.append(monte_carlo_desc) 
 89  uf.backend = monte_carlo.create_data 
 90  uf.menu_text = "&create_data" 
 91  uf.gui_icon = "oxygen.actions.list-add-relax-blue" 
 92  uf.wizard_height_desc = 500 
 93  uf.wizard_size = (1000, 750) 
 94  uf.wizard_apply_button = False 
 95  uf.wizard_image = WIZARD_IMAGE_PATH + 'monte_carlo.png' 
 96   
 97   
 98  # The monte_carlo.error_analysis user function. 
 99  uf = uf_info.add_uf('monte_carlo.error_analysis') 
100  uf.title = "Calculate parameter errors from the Monte Carlo simulations." 
101  uf.title_short = "Error calculation." 
102  # Description. 
103  uf.desc.append(Desc_container()) 
104  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.") 
105  uf.desc.append(monte_carlo_desc) 
106  uf.backend = monte_carlo.error_analysis 
107  uf.menu_text = "&error_analysis" 
108  uf.gui_icon = "oxygen.actions.roll-relax-blue" 
109  uf.wizard_height_desc = 620 
110  uf.wizard_size = (1000, 750) 
111  uf.wizard_apply_button = False 
112  uf.wizard_image = WIZARD_IMAGE_PATH + 'monte_carlo.png' 
113   
114   
115  # The monte_carlo.initial_values user function. 
116  uf = uf_info.add_uf('monte_carlo.initial_values') 
117  uf.title = "Set the initial simulation parameter values." 
118  uf.title_short = "Initial value setting." 
119  # Description. 
120  uf.desc.append(Desc_container()) 
121  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 functions 'grid_search' and 'minimise' will only effect the simulations and not the model parameters.") 
122  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.") 
123  uf.desc.append(monte_carlo_desc) 
124  uf.backend = monte_carlo.initial_values 
125  uf.menu_text = "&initial_values" 
126  uf.gui_icon = "oxygen.actions.list-add-relax-blue" 
127  uf.wizard_height_desc = 620 
128  uf.wizard_size = (1000, 750) 
129  uf.wizard_apply_button = False 
130  uf.wizard_image = WIZARD_IMAGE_PATH + 'monte_carlo.png' 
131   
132   
133  # The monte_carlo.off user function. 
134  uf = uf_info.add_uf('monte_carlo.off') 
135  uf.title = "Turn the Monte Carlo simulations off.""" 
136  uf.title_short = "Simulations off.""" 
137  # Description. 
138  uf.desc.append(Desc_container()) 
139  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.") 
140  uf.desc.append(monte_carlo_desc) 
141  uf.backend = monte_carlo.off 
142  uf.menu_text = "o&ff" 
143  uf.gui_icon = "oxygen.actions.dialog-cancel" 
144  uf.wizard_height_desc = 620 
145  uf.wizard_size = (1000, 750) 
146  uf.wizard_apply_button = False 
147  uf.wizard_image = WIZARD_IMAGE_PATH + 'monte_carlo.png' 
148   
149   
150  # The monte_carlo.on user function. 
151  uf = uf_info.add_uf('monte_carlo.on') 
152  uf.title = "Turn the Monte Carlo simulations on.""" 
153  uf.title_short = "Simulations on.""" 
154  # Description. 
155  uf.desc.append(Desc_container()) 
156  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.") 
157  uf.desc.append(monte_carlo_desc) 
158  uf.backend = monte_carlo.on 
159  uf.menu_text = "o&n" 
160  uf.gui_icon = "oxygen.actions.dialog-ok" 
161  uf.wizard_height_desc = 620 
162  uf.wizard_size = (1000, 750) 
163  uf.wizard_apply_button = False 
164  uf.wizard_image = WIZARD_IMAGE_PATH + 'monte_carlo.png' 
165   
166   
167  # The monte_carlo.setup user function. 
168  uf = uf_info.add_uf('monte_carlo.setup') 
169  uf.title = "Set up the Monte Carlo simulations." 
170  uf.title_short = "Simulation setup." 
171  uf.add_keyarg( 
172      name = "number", 
173      default = 500, 
174      py_type = "int", 
175      min = 3, 
176      max = 100000, 
177      desc_short = "number of Monte Carlo simulations", 
178      desc = "The number of Monte Carlo simulations." 
179  ) 
180  # Description. 
181  uf.desc.append(Desc_container()) 
182  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.") 
183  uf.desc.append(monte_carlo_desc) 
184  uf.backend = monte_carlo.setup 
185  uf.menu_text = "&setup" 
186  uf.gui_icon = "oxygen.actions.document-edit" 
187  uf.wizard_height_desc = 570 
188  uf.wizard_size = (1000, 750) 
189  uf.wizard_apply_button = False 
190  uf.wizard_image = WIZARD_IMAGE_PATH + 'monte_carlo.png' 
191