monte_carlo_local
- circuit_analyzer.all.monte_carlo_local(circuit_model, wavelengths, samples, parallel=False, parallel_option='number', save_path=None, seed=None)
- Run Monte Carlo simulations where each occurrence is modified. - The changes are done on each occurence (hence ‘local’), as opposed to globally, where the same parameter is modified for all occurences on the circuit. This is based on a description provided by _parameter_distribution in the CircuitModelView. - This requires the CircuitModel to have a ‘_parameter_distribution’ method. That method returns a number of samples taken from a distribution, for each parameter in the CompactModel. - Parameters:
- circuit_model: CircuitModel
- IPKISS CircuitModel used in the simulation. 
- wavelengths: array-like
- An array of wavelengths, for instance: np.linspace(1.54, 1.56, 1001). 
- samples: int
- Number of simulations. 
- parallel: bool, optional
- Parallelize the calculations to potentially reduce the simulation time. 
- parallel_option: str
- String of what to parallelize, either over ‘wavelengths’ or over the ‘number’ of simulations. Options are ‘wavelengths’ or ‘number’. 
- save_path: str/path, optional
- Path where the save file will be stored, for instance: save_path = “simulation_results.data”. 
- seed: int or None
- A seed value for the random number generator to ensure reproducibility. Defaults to None (no seed). 
 
- Returns:
- an array of S matrices, one S matrix per sampling.
 
 - Examples - This example shows how to use the function to generate parameter samples for a given number. - >>> import ipkiss3.all as i3 >>> from circuit_analyzer.statistical_modeling import monte_carlo_local >>> >>> >>> class Cell(i3.PCell): >>> class CircuitModel(i3.CircuitModelView): >>> insertion_loss = i3.NumberProperty(default=1.23) >>> bandwidth = i3.PositiveNumberProperty(default=2.23) >>> center_wavelength = i3.PositiveNumberProperty(default=1.55) >>> >>> def _parameter_distributions(self, number: int, seed: None): >>> '''Returns a dict with a `number` of samples for each parameter of the CompactModel. >>> >>> This is a required method for Statistical CircuitModels!!! >>> ''' >>> from scipy import stats >>> import numpy as np >>> >>> insertion_loss = np.asarray([1.22, 1.23, 1.21, 1.20, 1.211, 1.266, 1.25]) >>> bandwidth = np.asarray([1.22, 1.23, 1.21, 1.20, 1.211, 1.266, 1.25]) + 2 >>> center_wavelength = np.asarray([1.50, 1.53, 1.55, 1.56, 1.57, 1.55, 1.54]) >>> >>> dist = stats.gaussian_kde([insertion_loss, bandwidth, center_wavelength]) >>> samples = dist.resample(number, seed=seed) >>> >>> result = { >>> "insertion_loss": samples[0], >>> "bandwidth": samples[1], >>> "center_wavelength": samples[2], >>> } >>> return result >>> >>> def _generate_model(self): >>> return SomeModel( >>> insertion_loss=self.insertion_loss, >>> bandwidth=self.bandwidth, >>> center_wavelength=self.center_wavelength, >>> ) >>> >>> # We can now use this statistical CircuitModel to run Monte Carlo simulations: >>> smatrices = monte_carlo_local( >>> circuit_model=Cell().CircuitModel(), >>> wavelengths=np.linspace(1.5, 1.6, 200), >>> parallel=False, >>> samples=100, >>> ) >>>