Importance Sampling - Value at Risk in Matlab - matlab

I would like to integrate importance sampling in monte carlo to speed up the calculation.
Here I have created a simple example in matlab:
a=randn(10,10000);
a_sum=sum(a,1);
quantile(a_sum, 0.01)
The value at risk will amount to -7.3159 and around 100 scenarios are above the value at risk. So using a shift of -1. I can have more scenarios above value at risk:
b=a-1;
b_sum=sum(b,1);
A common way to calculate the value at risk is to use the likelihood ratios. Since I have shifted each random number by -1, I can calculate for each shift the likelihood ratio. But can I combine these likelihood ratios? And how can I calculate the value at risk?

Related

Explain the intuition for the tol paramer in scipy differential evolution

I am using the differential evolution optimizer in scipy and I don't understand the intuition behind the tol argument. Specifically is say in the documentation:
tol: float, optional
When the mean of the population energies, multiplied by tol, divided
by the standard deviation of the population energies is greater than 1
the solving process terminates:
convergence = mean(pop) * tol / stdev(pop) > 1
What does setting tol represent from a user perspective?
Maybe the formula in the documentation is easier to understand in the following form (see lines 508 and 526 in the code):
std(population_energies) / mean(population_energies) < tol
It means that convergence is reached when the standard deviation of the energies for each individual in the population, normed by the average, is smaller than the given tolerance value.
The optimization algorithm is iterative. At every iteration a better solution is found. The tolerance parameters is used to define a stopping condition. The stopping condition is actually that all the individuals (parameter sets) have approximately the same energy, i.e. the same cost function value. Then, the parameter set giving the lowest energy is returned as a solution.
It also implies that all the individuals are relatively close to each other in the parameter space. So, no better solution can be expected on the following generations.

How to avoid infinity as output value?

I have written the following code to calculate required transmission power based on distance between the sender and receiver and SNR threshold at the receiver. However I get huge values for required Intensity(Req_I) and Required Transmission Power (Req_Pt). Please Suggest the solution if I am making any mistake in the technique to calculate the transmission power or in the code itself.
Best Regards
Pt=12; %Transmit power in watts
spreading=1.5; %Spreading factor
f=10; %Frequency in Kilo Hz.
d=0.5; %Distance in Kilo Meters.
NL=47.69; %Noise Level in db
DI=0; %Directivity Index
pi=3.14159265359;
SNRth=17; %SNR threshold in db
%absorption=10^((0.002+0.11*(f^2/((1+f^2)+0.011*f^2)))/10); %Absorption factor
absorption=10^((0.11*(f^2/(1+f^2))+44*(f^2/(4100+f^2))+2.75*10^(-4)*f^2+0.003)/10);
TL=(d^spreading)*(absorption^d); %Transmission Loss
Req_SL=SNRth+TL+NL+DI; %Required Source Level
Req_I=((10^Req_SL)/10)*(0.67*10^(-18)); %Required Intensity
Req_Pt=Req_I*4*pi; %Required Transmission Power
The calculation of your TL factor is probably wrong, maybe you forgot to take the logarithm of it?
I don't know where your formulas come from, nor your specific application. If you don't have the correct formulas, you can take a look at this pdf, which provides expressions for the TL factor due to attenuation and spreading.
Check your units.
The original paper gives an expression (3) for low frequency propagation, the one you have used, but requires the input to be in kHz, not Hz. Either you should use
f = 10*1e-3; %frequency in kHz
or you should be using formula (2). Also note that the the attenuation is in dB/km, so you should convert your distance too, unless you actually are interested in propagating 500 km.

How to model scalar values with a neural network if besides direction the magnitude matters too

Say you want to predict temperature changes based on some input data. Temperature changes are positive or negative scalars with a mean of zero. If only the direction matters one could just use tanh as an activation function in the output layer. But say for delta-temperatures predicting the magnitude of the change is also important, not just the sign.
How would you model this output. Tanh doesn't seem to be a good choice because it gives values between -1 and 1. And say temperature changes have a gaussian, or some other weird distribution, so hovering around the center quasi-linear domain of tanh(+-0) would be difficult to learn for a neural network. I'm worried that the sign would be good but the magnitude output would be useless.
How about having the network output one-hot vectors of length N, treat the argmax of this output vector as a temperature change on a pre-defined window. Say the window is -30 - +30 degrees, using N=60 long one-hot vector, if argmax( output )=45 that means the prediction is about 15 degrees.
I was actually not sure how to search for this topic.

Simulink model is not seems to work at high frequency [Just a representation mistake ?]

I have a model of the form:
All I am doing here is mixing the two sine waveforms based on the random bits.
I set the signal frequency of first sign wave to:
Second sign wave:
The output is :
But it works well when the signals are of low frequency.
How can I make it to work even at high frequencies ?
80*10^3*t and 12*10^4*t are both always an integer for all values of t=0:0.01:100. Hence the sin is always being evaluated at an integer multiple of 2*pi. Hence the value of the plot is always zero (or near enough to it down in the 10^-8 or 10^-9 range.
You need to change the sample rate so that you get points where sin is not zero.

How does number of points change a FFT in MATLAB

When taking fft(signal, nfft) of a signal, how does nfft change the outcome and why? Can I have a fixed value for nfft, say 2^18, or do I need to go 2^nextpow2(2*length(signal)-1)?
I am computing the power spectral density(PSD) of two signals by taking the FFT of the autocorrelation, and I want to compare the the results. Since the signals are of different lengths, I am worried if I don't fix nfft, it would make the comparison really hard!
There is no inherent reason to use a power-of-two (it just might make the processing more efficient in some circumstances).
However, to make the FFTs of two different signals "commensurate", you will indeed need to zero-pad one or other (or both) signals to the same lengths before taking their FFTs.
However, I feel obliged to say: If you need to ask this, then you're probably not at a point on the DSP learning curve where you're going to be able to do anything useful with the results. You should get yourself a decent book on DSP theory, e.g. this.
Most modern FFT implementations (including MATLAB's which is based on FFTW) now rarely require padding a signal's time series to a length equal to a power of two. However, nearly all implementations will offer better, and sometimes much much better, performance for FFT's of data vectors w/ a power of 2 length. For MATLAB specifically, padding to a power of 2 or to a length with many low prime factors will give you the best performance (N = 1000 = 2^3 * 5^3 would be excellent, N = 997 would be a terrible choice).
Zero-padding will not increase frequency resolution in your PSD, however it does reduce the bin-size in the frequency domain. So if you add NZeros to a signal vector of length N the FFT will now output a vector of length ( N + NZeros )/2 + 1. This means that each bin of frequencies will now have a width of:
Bin width (Hz) = F_s / ( N + NZeros )
Where F_s is the signal sample frequency.
If you find that you need to separate or identify two closely space peaks in the frequency domain, you need to increase your sample time. You'll quickly discover that zero-padding buys you nothing to that end - and intuitively that's what we'd expect. How can we expect more information in our power spectrum w/o adding more information (longer time series) in our input?
Best,
Paul