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-2012 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax.                                     # 
  6  #                                                                             # 
  7  # relax 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 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax 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 relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Module docstring. 
 24  """The monte_carlo user function definitions for Monte Carlo simulations.""" 
 25   
 26  # relax module imports. 
 27  from generic_fns import monte_carlo 
 28  from graphics import WIZARD_IMAGE_PATH 
 29  from user_functions.data import Uf_info; uf_info = Uf_info() 
 30  from user_functions.objects import Desc_container 
 31   
 32   
 33  # Generic description document, used in all user functions. 
 34  monte_carlo_desc = Desc_container("Monte Carlo Simulation Overview") 
 35  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:") 
 36  monte_carlo_desc.add_item_list_element("1", "The measured data set together with the corresponding error set should be loaded into relax.") 
 37  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.") 
 38  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.") 
 39  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.") 
 40  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).") 
 41  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.") 
 42  monte_carlo_desc.add_item_list_element("7", "Failed simulations are removed using the techniques of model elimination.") 
 43  monte_carlo_desc.add_item_list_element("8", "The model parameter errors are calculated from the distribution of simulation parameters.") 
 44  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.") 
 45  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:") 
 46  monte_carlo_desc.add_prompt("relax> grid_search(inc=11)                                       # Step 2.") 
 47  monte_carlo_desc.add_prompt("relax> minimise('newton')                                        # Step 2.") 
 48  monte_carlo_desc.add_prompt("relax> monte_carlo.setup(number=500)                             # Step 3.") 
 49  monte_carlo_desc.add_prompt("relax> monte_carlo.create_data(method='back_calc')               # Step 4.") 
 50  monte_carlo_desc.add_prompt("relax> monte_carlo.initial_values()                              # Step 5.") 
 51  monte_carlo_desc.add_prompt("relax> minimise('newton')                                        # Step 6.") 
 52  monte_carlo_desc.add_prompt("relax> eliminate()                                               # Step 7.") 
 53  monte_carlo_desc.add_prompt("relax> monte_carlo.error_analysis()                              # Step 8.") 
 54  monte_carlo_desc.add_paragraph("An example for reduced spectral density mapping is:") 
 55  monte_carlo_desc.add_prompt("relax> calc()                                                    # Step 2.") 
 56  monte_carlo_desc.add_prompt("relax> monte_carlo.setup(number=500)                             # Step 3.") 
 57  monte_carlo_desc.add_prompt("relax> monte_carlo.create_data(method='back_calc')               # Step 4.") 
 58  monte_carlo_desc.add_prompt("relax> calc()                                                    # Step 6.") 
 59  monte_carlo_desc.add_prompt("relax> monte_carlo.error_analysis()                              # Step 8.") 
 60   
 61   
 62  # The user function class. 
 63  uf_class = uf_info.add_class('monte_carlo') 
 64  uf_class.title = "Class containing the functions for Monte Carlo and related simulations." 
 65  uf_class.menu_text = "&monte_carlo" 
 66  uf_class.gui_icon = "oxygen.actions.roll-relax-blue" 
 67   
 68   
 69  # The monte_carlo.create_data user function. 
 70  uf = uf_info.add_uf('monte_carlo.create_data') 
 71  uf.title = "Create the Monte Carlo simulation data." 
 72  uf.title_short = "Data creation." 
 73  uf.add_keyarg( 
 74      name = "method", 
 75      default = "back_calc", 
 76      py_type = "str", 
 77      desc_short = "method", 
 78      desc = "The simulation method.", 
 79      wiz_element_type = "combo", 
 80      wiz_combo_choices = ["Monte Carlo", "Bootstrapping"], 
 81      wiz_combo_data = ["back_calc", "direct"], 
 82      wiz_read_only = True 
 83  ) 
 84  # Description. 
 85  uf.desc.append(Desc_container()) 
 86  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. ") 
 87  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.") 
 88  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.") 
 89  uf.desc.append(monte_carlo_desc) 
 90  uf.backend = monte_carlo.create_data 
 91  uf.menu_text = "&create_data" 
 92  uf.gui_icon = "oxygen.actions.list-add-relax-blue" 
 93  uf.wizard_height_desc = 500 
 94  uf.wizard_size = (1000, 750) 
 95  uf.wizard_apply_button = False 
 96  uf.wizard_image = WIZARD_IMAGE_PATH + 'monte_carlo.png' 
 97   
 98   
 99  # The monte_carlo.error_analysis user function. 
100  uf = uf_info.add_uf('monte_carlo.error_analysis') 
101  uf.title = "Calculate parameter errors from the Monte Carlo simulations." 
102  uf.title_short = "Error calculation." 
103  # Description. 
104  uf.desc.append(Desc_container()) 
105  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.") 
106  uf.desc.append(monte_carlo_desc) 
107  uf.backend = monte_carlo.error_analysis 
108  uf.menu_text = "&error_analysis" 
109  uf.gui_icon = "oxygen.actions.roll-relax-blue" 
110  uf.wizard_height_desc = 620 
111  uf.wizard_size = (1000, 750) 
112  uf.wizard_apply_button = False 
113  uf.wizard_image = WIZARD_IMAGE_PATH + 'monte_carlo.png' 
114   
115   
116  # The monte_carlo.initial_values user function. 
117  uf = uf_info.add_uf('monte_carlo.initial_values') 
118  uf.title = "Set the initial simulation parameter values." 
119  uf.title_short = "Initial value setting." 
120  # Description. 
121  uf.desc.append(Desc_container()) 
122  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.") 
123  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.") 
124  uf.desc.append(monte_carlo_desc) 
125  uf.backend = monte_carlo.initial_values 
126  uf.menu_text = "&initial_values" 
127  uf.gui_icon = "oxygen.actions.list-add-relax-blue" 
128  uf.wizard_height_desc = 620 
129  uf.wizard_size = (1000, 750) 
130  uf.wizard_apply_button = False 
131  uf.wizard_image = WIZARD_IMAGE_PATH + 'monte_carlo.png' 
132   
133   
134  # The monte_carlo.off user function. 
135  uf = uf_info.add_uf('monte_carlo.off') 
136  uf.title = "Turn the Monte Carlo simulations off.""" 
137  uf.title_short = "Simulations off.""" 
138  # Description. 
139  uf.desc.append(Desc_container()) 
140  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.") 
141  uf.desc.append(monte_carlo_desc) 
142  uf.backend = monte_carlo.off 
143  uf.menu_text = "o&ff" 
144  uf.gui_icon = "oxygen.actions.dialog-cancel" 
145  uf.wizard_height_desc = 620 
146  uf.wizard_size = (1000, 750) 
147  uf.wizard_apply_button = False 
148  uf.wizard_image = WIZARD_IMAGE_PATH + 'monte_carlo.png' 
149   
150   
151  # The monte_carlo.on user function. 
152  uf = uf_info.add_uf('monte_carlo.on') 
153  uf.title = "Turn the Monte Carlo simulations on.""" 
154  uf.title_short = "Simulations on.""" 
155  # Description. 
156  uf.desc.append(Desc_container()) 
157  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.") 
158  uf.desc.append(monte_carlo_desc) 
159  uf.backend = monte_carlo.on 
160  uf.menu_text = "o&n" 
161  uf.gui_icon = "oxygen.actions.dialog-ok" 
162  uf.wizard_height_desc = 620 
163  uf.wizard_size = (1000, 750) 
164  uf.wizard_apply_button = False 
165  uf.wizard_image = WIZARD_IMAGE_PATH + 'monte_carlo.png' 
166   
167   
168  # The monte_carlo.setup user function. 
169  uf = uf_info.add_uf('monte_carlo.setup') 
170  uf.title = "Set up the Monte Carlo simulations." 
171  uf.title_short = "Simulation setup." 
172  uf.add_keyarg( 
173      name = "number", 
174      default = 500, 
175      py_type = "int", 
176      min = 3, 
177      max = 100000, 
178      desc_short = "number of Monte Carlo simulations", 
179      desc = "The number of Monte Carlo simulations." 
180  ) 
181  # Description. 
182  uf.desc.append(Desc_container()) 
183  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.") 
184  uf.desc.append(monte_carlo_desc) 
185  uf.backend = monte_carlo.setup 
186  uf.menu_text = "&setup" 
187  uf.gui_icon = "oxygen.actions.document-edit" 
188  uf.wizard_height_desc = 570 
189  uf.wizard_size = (1000, 750) 
190  uf.wizard_apply_button = False 
191  uf.wizard_image = WIZARD_IMAGE_PATH + 'monte_carlo.png' 
192